Skip to content

Commit

Permalink
Include Bedrock in each integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
john-shaffer committed Nov 27, 2021
1 parent 5d8f643 commit 9fce2d3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 58 deletions.
27 changes: 23 additions & 4 deletions integration-tests/clj/wp2static_test/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@
(str "bedrock.sh exited with code " code)
{:code code}))))
bedrock)
:name "Bedrock Initializer"
:name "Bedrock"
:join? true
:open-f (fn [_] (popen ["bash" "bedrock.sh"]))}))
:open-f (fn [_] (popen ["bash" "bedrock.sh"]))

:features {:sitemaps? false}
:paths {:cli "bedrock/web/wp"
:doc-root "bedrock/web"
:plugins "bedrock/web/app/plugins"
:site "/wp/"
:uploads "bedrock/web/app/uploads"
:wp-content "bedrock/web/wp/wp-content"}
:urls {:home "http://localhost:7001"}}))

(defn wordpress []
(shell-process
Expand All @@ -106,9 +115,18 @@
(str "wordpress.sh exited with code " code)
{:code code}))))
wordpress)
:name "WordPress Initializer"
:name "WordPress"
:join? true
:open-f (fn [_] (popen ["bash" "wordpress.sh"]))}))
:open-f (fn [_] (popen ["bash" "wordpress.sh"]))

:features {:sitemaps? true}
:paths {:cli "wordpress"
:doc-root "wordpress"
:plugins "wordpress/wp-content/plugins"
:site "/"
:uploads "wordpress/wp-content/uploads"
:wp-content "wordpress/wp-content"}
:urls {:home "http://localhost:7000"}}))

(defn system-map []
(component/system-map
Expand All @@ -117,6 +135,7 @@
:nginx (component/using (nginx) [:wordpress])
:php-fpm (component/using (php-fpm) [:mariadb])
:wordpress (component/using (wordpress) [:mariadb :php-fpm])
:wordpresses (component/using {} [:bedrock :wordpress])
:wp2static (component/using (wp2static) [:bedrock :wordpress])))

(defonce system (atom nil))
Expand Down
7 changes: 6 additions & 1 deletion integration-tests/clj/wp2static_test/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
result))

(defn wp-cli! [opts & args]
(apply sh! opts "wp" (concat args ["--path=wordpress"])))
(apply sh! opts "wp" (concat args [(str "--path=" (:path opts "wordpress"))])))

(defn test [opts]
;; https://clojureverse.org/t/why-doesnt-my-program-exit/3754/8
Expand All @@ -42,3 +42,8 @@
`(let [~name (main/start!)]
(core/clean-wp2static-cache!)
~@body))

(defmacro testing [[wp string] & body]
`(clojure.test/testing (str "[" (:name ~wp) "] " ~string)
~@body))

19 changes: 12 additions & 7 deletions integration-tests/test/wp2static_test/crawl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-crawled-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-crawled-site/" path)))
(defn get-crawled-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-crawled-site/" path)))

(deftest test-crawled-site
(test/with-test-system [_]
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(is (str/includes? (get-crawled-file "index.html") "Welcome to WordPress"))
(is (str/includes? (get-crawled-file "robots.txt") "Sitemap: http://localhost:7000/wp-sitemap.xml"))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(test/testing [wp "Crawling works"]
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(is (str/includes? (get-crawled-file wp "index.html")
"Welcome to WordPress"))
(is (str/includes? (get-crawled-file wp "robots.txt")
(str "Disallow: " (get-in wp [:paths :site]) "wp-admin/")))))))
56 changes: 35 additions & 21 deletions integration-tests/test/wp2static_test/detect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-crawled-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-crawled-site/" path)))
(defn get-crawled-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-crawled-site/" path)))

(defmacro with-robots-txt [wp s & body]
`(let [path# (str (get-in ~wp [:paths :doc-root]) "/robots.txt")]
(try
(spit path# ~s)
(do ~@body)
(finally
(test/sh! {} "rm" "-f" path#)))))

(def robots-sitemap-404
"User-agent: *
Expand All @@ -20,16 +28,21 @@ Sitemap: http://localhost:7000/does-not-exist.xml")
<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><sitemap><loc>http://localhost:7000/does-not-exist.xml</loc></sitemap></sitemapindex>")

(deftest test-robots-404
(testing "robots.txt sitemap URLs that return 404s are ignored"
(test/with-test-system [_]
(try
(spit "wordpress/robots.txt" robots-sitemap-404)
(spit "wordpress/wp-content/sitemap.xml" sitemap-with-404)
(is (zero? (:exit (test/wp-cli!
{:expect-warnings {#".*Got 404 for sitemap.*" 1}}
"wp2static" "detect"))))
(finally
(test/sh! {} "rm" "wordpress/robots.txt" "wordpress/wp-content/sitemap.xml"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(when (get-in wp [:features :sitemaps?])
(test/testing [wp "robots.txt sitemap URLs that return 404s are ignored"]
(with-robots-txt wp robots-sitemap-404
(let [sitemap-path (str (get-in wp [:paths :wp-content]) "/sitemap.xml")]
(try
(spit sitemap-path sitemap-with-404)
(is (zero? (:exit (test/wp-cli!
{:expect-warnings {#".*Got 404 for sitemap.*" 1}
:path (get-in wp [:paths :cli])}
"wp2static" "detect"))))
(finally
(test/sh! {} "rm" "-f" sitemap-path))))))))))

(def robots-sitemap-slashes
"User-agent: *
Expand All @@ -39,12 +52,13 @@ Allow: /wp-admin/admin-ajax.php
Sitemap: http://localhost:7000//wp-sitemap.xml")

(deftest test-robots-sitemap-slashes
(testing "robots.txt sitemap URLs with double slashes are processed"
(test/with-test-system [_]
(try
(spit "wordpress/robots.txt" robots-sitemap-slashes)
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(is (str/includes? (get-crawled-file "wp-sitemap-posts-post-1.xml") "http://localhost:7000/hello-world/"))
(finally
(test/sh! {} "rm" "wordpress/robots.txt"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(when (get-in wp [:features :sitemaps?])
(test/testing [wp "robots.txt sitemap URLs with double slashes are processed"]
(with-robots-txt wp robots-sitemap-slashes
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(is (str/includes? (get-crawled-file wp "wp-sitemap-posts-post-1.xml")
(str (get-in wp [:paths :home] "hello-world/"))))))))))
33 changes: 19 additions & 14 deletions integration-tests/test/wp2static_test/options_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ function processQueueImmediately_filter ( $val ) {
add_filter( 'wp2static_option_processQueueImmediately', 'processQueueImmediately_filter' );")

(deftest test-option-filters
(test/with-test-system [_]
(testing "wp2static_option_* filters work"
(is (= "0" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(try
(test/sh! {} "mkdir" "wordpress/wp-content/plugins/options-test")
(spit "wordpress/wp-content/plugins/options-test/options-test.php"
options-test-filters)
(test/wp-cli! {} "plugin" "activate" "options-test")
(is (= "1" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)
plugins-dir (get-in wp [:paths :plugins])]]
(test/testing [wp "wp2static_option_* filters work"]
(is (= "0" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(test/wp-cli! {} "plugin" "deactivate" "options-test")
(finally
(test/sh! {} "rm" "-rf" "wordpress/wp-content/plugins/options-test"))))))
(try
(test/sh! {} "mkdir" "options-test"
:dir plugins-dir)
(spit (str plugins-dir "/options-test/options-test.php")
options-test-filters)
(wp-cli! "plugin" "activate" "options-test")
(is (= "1" (-> (wp-cli! "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(wp-cli! "plugin" "deactivate" "options-test")
(finally
(test/sh! {} "rm" "-rf" "options-test"
:dir plugins-dir)))))))
26 changes: 15 additions & 11 deletions integration-tests/test/wp2static_test/post_process_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-processed-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-processed-site/" path)))
(defn get-processed-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-processed-site/" path)))

(deftest test-processed-site
(test/with-test-system [_]
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(test/wp-cli! {} "wp2static" "post_process")
(let [index (get-processed-file "index.html")]
(is (str/includes? index "Welcome to WordPress"))
(testing "Rewrites work"
(is (str/includes? index "<a href=\"https://example.com/hello-world/\">"))
(is (str/includes? (get-processed-file "robots.txt") "Sitemap: https://example.com/wp-sitemap.xml"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(test/testing [wp "Post-processing works"]
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(wp-cli! "wp2static" "post_process")
(let [index (get-processed-file wp "index.html")]
(is (str/includes? index "Welcome to WordPress"))
(testing "& URL rewriting works"
(is (str/includes? index "<a href=\"https://example.com/hello-world/\">"))
(when (get-in wp [:features :sitemaps?])
(is (str/includes? (get-processed-file wp "robots.txt") "Sitemap: https://example.com/wp-sitemap.xml")))))))))

0 comments on commit 9fce2d3

Please sign in to comment.