-
Notifications
You must be signed in to change notification settings - Fork 4
Restore clj-github.test-helpers/build-spec behavior
#40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
95b563b
fea14a9
3f50783
f9f1fe7
8b17771
a8993d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,53 +4,29 @@ | |
| [org.httpkit.fake :as fake]) | ||
| (:import (java.util.regex Pattern))) | ||
|
|
||
|
|
||
| (defn- spec-type [spec] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplifies |
||
| (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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve |
||
| ~@body)) | ||
| 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)))) |
There was a problem hiding this comment.
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