Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .nu/workflows/moriarty.yaml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal workflows won't run in public repos like this, they always error out, so there is no point in keeping them

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.9.1

- Fix regression in `clj-github.test-helpers/build-spec`, restoring its behavior to pre-0.8.0 (return plain data, not quoted forms)
- Fix bug in default `/app/installations` mock

## 0.9.0

- Add function to create releases for repositories
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nubank/clj-github "0.9.0"
(defproject dev.nubank/clj-github "0.9.1"
:description "A Clojure library for interacting with the github developer API"
:url "https://github.com/nubank/clj-github"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
73 changes: 25 additions & 48 deletions src/clj_github/test_helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,29 @@
[org.httpkit.fake :as fake])
(:import (java.util.regex Pattern)))


(defn- spec-type [spec]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplifies cond + defmethods into a simple cond (as this is not open for external extension)

(defn- request-spec [request]
(cond
(-> spec meta :path) :path
(string? spec) :string
(map? spec) :map
(instance? Pattern spec) :pattern
:else :form))

(defmulti spec-builder spec-type)

(defmethod spec-builder :string [request]
(str github-url request))

(defmethod spec-builder :map [request]
(if (:path request)
`(let [request# ~request
path# (:path request#)]
(assoc request# :url (str github-url path#)))
request))

(defmethod spec-builder :form [request]
request)

(defmethod spec-builder :path [form]
`(spec-builder ~form))

(defmethod spec-builder :pattern [pattern]
{:url pattern})

(defn -add-default-content-type
[response]
(assoc-in response [:headers :content-type] "application/json"))

(defn add-default-content-type [response]
`(let [responder# (fake/responder ~response)]
(fn [origin-fn# opts# callback#]
((or callback# identity)
(responder# origin-fn# opts# -add-default-content-type)))))
Comment on lines -37 to -45

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inlined


(defn build-spec [spec]
(reduce (fn [processed-fakes [request response]]
(-> processed-fakes
(conj (spec-builder request))
(conj (add-default-content-type response))))
[(str github-url "app/installations") "{}"]
(partition 2 spec)))
(string? request) (str github-url request)
(instance? Pattern request) {:url request}
(and (map? request) (:path request)) (assoc request :url (str github-url (:path request)))
:else request))

(defn- response-spec [response]
(let [responder (fake/responder response)
force-json #(assoc-in % [:headers :content-type] "application/json")]
(fn [orig-fn opts callback]
((or callback identity)
(responder orig-fn opts force-json)))))

(defn ^:internal build-spec

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns plain data again, instead of quoted forms

"Converts a `with-fake-github`-style spec (a sequence of request/response
pairs) into a value suitable for `org.httpkit.fake/with-fake-http`.

Do not call directly."
[spec]
(into [(str github-url "/app/installations") "{}"]
(mapcat (fn [[req resp]] [(request-spec req) (response-spec resp)]))
(partition 2 spec)))

(defmacro with-fake-github
"A wrapper around `with-fake-http` that sets up some defaults for GitHub access.
Expand All @@ -67,5 +43,6 @@
However, the response Content-Type header is forced to \"application/json\", so the body (if provided)
must be a JSON value encoded as string."
[spec & body]
`(fake/with-fake-http ~(build-spec spec)
~@body))
`(fake/with-fake-http
(build-spec ~(mapv #(if (-> % meta :path) `{:path ~%} %) spec))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve :path as intended (feature from 0.8.0 release)

~@body))
30 changes: 30 additions & 0 deletions test/clj_github/state_flow_helper_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns clj-github.state-flow-helper-test
(:require [cheshire.core :as json]
[clj-github.httpkit-client :as httpkit-client]
[clj-github.state-flow-helper :as sfh]
[clojure.test :refer [deftest is]]
[matcher-combinators.test]
[state-flow.core :as state-flow]))

(defn- init-system []
{:system {:github-client (httpkit-client/new-client {:token-fn (constantly "token")})}})

(defn- run-flow [a-flow]
(state-flow/run* {:init init-system} a-flow))

(deftest mock-github-flow-honors-responses
(let [response-body (json/generate-string {:id 1 :mergeable_state "clean"})
[result _] (run-flow
(sfh/mock-github-flow
{:initial-state {:orgs [{:name "nubank"
:repos [{:name "dummy-lib"
:default_branch "main"}]}]}
:responses [{:path "/repos/nubank/dummy-lib/pulls/1" :method :get}
{:status 200
:body response-body}]}
(sfh/with-github-client
(fn [client]
(httpkit-client/request client {:path "/repos/nubank/dummy-lib/pulls/1"})))))]
(is (match? {:status 200
:body {:id 1 :mergeable_state "clean"}}
result))))
Loading