Skip to content

Commit

Permalink
WIP: Bring back workspace activation
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Nov 7, 2024
1 parent 13f00be commit b7f0fc1
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{:lint-as {test-runner.macros/deftest-async clojure.test/deftest}}
{:lint-as {e2e.macros/deftest-async clojure.test/deftest}}
28 changes: 28 additions & 0 deletions e2e-test-ws/.joyride/scripts/workspace_activate.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns workspace-activate
(:require [e2e.db :as db]
["vscode" :as vscode]))

(println "e2e-test-ws workspace activating...")

;; If there is anything you want to happen in the workspace before
;; the tests run, you can set things up here

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(def ws-root (first vscode/workspace.workspaceFolders))

(def question 42)

;; See e2e-test-ws/.joyride/src/tests/ws_ready/example_test.cljs
;; for how we test that the workspace has been properly prepared

;; The test runner will be waiting for this to appear in the state
;; before it starts running tests
(swap! db/!state assoc :ws-activated? true)

(println "e2e-test-ws workspace activated.")

;; E.g. when running the tests locally, you may have created files
;; that you don't want to litter the git repo with.
(defn clean-up! []
(println "Cleaning up e2e-tests workspace")
:nothing-to-clean-up)
6 changes: 6 additions & 0 deletions e2e-test-ws/.joyride/src/a_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns a-test
(:require [cljs.test :refer [deftest testing is]]))

(deftest hello
(testing "Test files can be put anywhere in the `src` directory. They just need to end in `_test.cljs`."
(is (= :foo :foo))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns test-runner.db
(ns e2e.db
(:require [cljs.test]))

(def !state (atom {:running nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns test-runner.macros)
(ns e2e.macros)

(defmacro deftest-async [name opts & body]
(let [[opts body]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(ns test-runner.runner
(ns e2e.test-runner
(:require [clojure.string :as string]
[cljs.test]
[promesa.core :as p]
[test-runner.config :as config]
[test-runner.db :as db]
[e2e.db :as db]
["vscode" :as vscode]))

(defn- write [& xs]
Expand Down Expand Up @@ -67,15 +66,23 @@
(.map (partial file->ns src-path)))]
(mapv symbol nss-strings)))

(defn- run-when-ws-activated [nss-syms running tries]
(if (:ws-activated? @db/!state)
(-> (p/do
(println "Running tests in" nss-syms)
(apply require nss-syms)
(apply cljs.test/run-tests nss-syms)
running)
(p/catch (fn [e]
(p/reject! (:running @db/!state) e))))
(do
(println "Runner: Workspace not activated yet, tries: " tries "- trying again in a jiffy")
(js/setTimeout #(run-when-ws-activated nss-syms running (inc tries)) 10)
running)))

(defn run-all-tests [src-path]
(let [running (p/deferred)]
(swap! db/!state assoc :running running)
(p/let [nss-syms (find-test-nss+ src-path)]
(println "Running tests in" nss-syms)
(-> (p/do
(apply require nss-syms)
(apply cljs.test/run-tests nss-syms)
running)
(p/catch (fn [e]
(p/reject! (:running @db/!state) e)))))))
(run-when-ws-activated nss-syms running 1))))

4 changes: 0 additions & 4 deletions e2e-test-ws/.joyride/src/test_runner/config.cljs

This file was deleted.

6 changes: 0 additions & 6 deletions e2e-test-ws/.joyride/src/tests/foo/bar/b_test.cljs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(ns tests.a-test
(ns tests.smoke-test
(:require ["vscode" :as vscode]
[cljs.test :refer [deftest testing is]]
[promesa.core :as p]
[test-runner.macros :refer [deftest-async]]))
[e2e.macros :refer [deftest-async]]))

(deftest hello
(testing "We can test things"
(is (= :foo :foo))))
(testing "We can test things sync things"
(is (= :foo :foo))
(is (= 1 (count vscode/workspace.workspaceFolders)))))

(deftest-async extension-activation
(testing "The extension activates"
(testing "The extension activates (which is an async operation)"
(p/let [extension (vscode/extensions.getExtension "betterthantomorrow.paste-replaced")
api (.activate extension)]
(is (not= nil? (.getContextValue api))))))

10 changes: 10 additions & 0 deletions e2e-test-ws/.joyride/src/tests/ws_ready/example_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns tests.ws-ready.example-test
(:require [cljs.test :refer [deftest testing is]]
["vscode" :as vscode]
workspace-activate))

; No tests starts before the workspace is activated
(deftest ws-activated
(testing "The workspace is indeed activated"
(is (= 42 workspace-activate/question))
(is (= (first vscode/workspace.workspaceFolders) workspace-activate/ws-root))))
2 changes: 1 addition & 1 deletion e2e-test-ws/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const vscode = require('vscode');
exports.run = async () => {
return vscode.commands.executeCommand(
'joyride.runCode',
"(require '[test-runner.runner :as runner]) (runner/run-all-tests \".joyride/src\")"
"(require '[e2e.test-runner :as runner]) (runner/run-all-tests \".joyride/src\")"
);
};

0 comments on commit b7f0fc1

Please sign in to comment.