Skip to content

Commit abdc7a1

Browse files
committed
follow pattern of speclj for tests w/o cljsbuild
also don't use paths for cljc, just put everything together
1 parent d2bd511 commit abdc7a1

20 files changed

Lines changed: 313 additions & 60 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pom.xml.asc
99
/.nrepl-port
1010
.DS_Store
1111
/resources/public/xapi_schema.js
12+
.specljs-timestamp

bin/speclj.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Speclj Runner
3+
4+
Usage:
5+
phantomjs resources/public/specs/speclj.js [auto]
6+
7+
auto: will only run specs updated after the last run. (default: false)
8+
9+
Each run produced/touches a timestamp file, .specljs-timestamp
10+
*/
11+
12+
var outputDir = "target/cljs";
13+
var nsPrefix = "speclj";
14+
var runnerFile = "bin/specs.html";
15+
16+
var fs = require("fs");
17+
var p = require('webpage').create();
18+
var system = require('system');
19+
20+
String.prototype.endsWith = function (suffix) {
21+
return this.indexOf(suffix, this.length - suffix.length) !== -1;
22+
};
23+
String.prototype.startsWith = function (str) {
24+
return this.slice(0, str.length) == str;
25+
};
26+
27+
p.onConsoleMessage = function (x) {
28+
fs.write("/dev/stdout", x, "w");
29+
};
30+
31+
var timestampFile = ".specljs-timestamp";
32+
writeTimestamp = function () {
33+
if(fs.lastModified(timestampFile) != null)
34+
fs.remove(timestampFile);
35+
fs.touch(timestampFile);
36+
};
37+
38+
readTimestamp = function () {
39+
return fs.lastModified(timestampFile);
40+
};
41+
42+
allSpecsFilter = function () {
43+
return true;
44+
};
45+
46+
var autoMode = function () {
47+
return system.args[1] == "auto" && readTimestamp() != null;
48+
};
49+
50+
var logList = function (title, list) {
51+
console.log(title + ":");
52+
for(var i in list)
53+
console.log(i);
54+
};
55+
56+
var relativeRoot = outputDir + "/goog/";
57+
findUpdatedSpecs = function (rdeps, deps) {
58+
var minMillis = readTimestamp().getTime();
59+
var updated = {};
60+
for(var ns in rdeps) {
61+
var file = deps.nameToPath[ns];
62+
var path = relativeRoot + file;
63+
if(fs.lastModified(path).getTime() >= minMillis) {
64+
updated[ns] = true;
65+
}
66+
}
67+
return updated;
68+
};
69+
70+
buildReverseDeps = function (deps) {
71+
var rdeps = {};
72+
for(var ns in deps.nameToPath) {
73+
if(ns.startsWith(nsPrefix)) {
74+
var file = deps.nameToPath[ns];
75+
for(var rdep in deps.requires[file]) {
76+
if(rdep.startsWith(nsPrefix)) {
77+
if(!(rdep in rdeps)) {
78+
rdeps[rdep] = {}
79+
}
80+
rdeps[rdep][ns] = true;
81+
}
82+
}
83+
if(!(ns in rdeps)) {
84+
rdeps[ns] = {}
85+
}
86+
}
87+
}
88+
return rdeps;
89+
};
90+
91+
reduceToSpecs = function (affected) {
92+
var result = {};
93+
for(var ns in affected) {
94+
if(ns.endsWith("_spec")) {
95+
result[ns.replace(/_/g, "-")] = true
96+
}
97+
}
98+
return result;
99+
};
100+
101+
findAffectedSpecs = function () {
102+
var deps = p.evaluate(function () {
103+
return goog.dependencies_;
104+
});
105+
var rdeps = buildReverseDeps(deps);
106+
var updated = findUpdatedSpecs(rdeps, deps);
107+
108+
var result = {};
109+
110+
var walkDeps = function (nses) {
111+
for(var ns in nses) {
112+
if(!(ns in result)) {
113+
result[ns] = true;
114+
walkDeps(rdeps[ns])
115+
}
116+
}
117+
};
118+
walkDeps(updated);
119+
120+
return reduceToSpecs(result);
121+
};
122+
123+
p.open(runnerFile, function (status) {
124+
try {
125+
var specs = autoMode() ? findAffectedSpecs() : null;
126+
127+
var result = p.evaluate(function (specSet) {
128+
return runSpecsPhantom(specSet);
129+
}, specs);
130+
131+
writeTimestamp();
132+
phantom.exit(result);
133+
}
134+
catch(e) {
135+
console.error(e);
136+
phantom.exit(-1);
137+
}
138+
139+
});

bin/specs.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="iso-8859-1" content="text/html" http-equiv="Content-Type"/>
5+
<title>Speclj Specs</title>
6+
<!--<script src="../js/es5-shim.js" type="text/javascript"></script>-->
7+
<script src="../target/cljs/goog/base.js" type="text/javascript"></script>
8+
<script src="../target/specs.js" type="text/javascript"></script>
9+
<script type="text/javascript">
10+
String.prototype.endsWith = function (suffix) {
11+
return this.indexOf(suffix, this.length - suffix.length) !== -1;
12+
};
13+
14+
for(var name in goog.dependencies_.nameToPath) {
15+
if(name.endsWith("_spec")) {
16+
goog.require(name)
17+
}
18+
}
19+
20+
runSpecsConfigured = function (color, reporter) {
21+
speclj.run.standard.armed = true;
22+
return speclj.run.standard.run_specs(
23+
cljs.core.keyword("color"), color
24+
, cljs.core.keyword("reporters"), [reporter]
25+
);
26+
};
27+
28+
runSpecs = function () {
29+
runSpecsConfigured(false, "documentation");
30+
}
31+
32+
runSpecsPhantom = function (affectedSpecs) {
33+
if(affectedSpecs != null) {
34+
console.log("Only running affected specs:");
35+
var descriptionAtom = speclj.config.active_runner().descriptions;
36+
cljs.core.swap_BANG_(descriptionAtom, function (descriptions) {
37+
return cljs.core.filter(function (description) {
38+
return description.ns in affectedSpecs;
39+
}, descriptions);
40+
});
41+
cljs.core.doall(cljs.core.map(function (description) {
42+
console.log(" ", description.ns);
43+
}, cljs.core.deref(descriptionAtom)));
44+
}
45+
runSpecsConfigured(true, "documentation");
46+
}
47+
</script>
48+
</head>
49+
<body>
50+
<h3 style="margin: 1em">Speclj CLJS Specs</h3>
51+
52+
<p style="margin: 1em; width: 400px;">
53+
Typically these specs are run using phantomjs on the command line.
54+
But you can run them here if you like.
55+
That is, assuming all the cljs has been compiled in development.
56+
<br/>
57+
Open up the browser console:
58+
</p>
59+
<pre style="margin: 1em; padding: 1em; width: 400px; border: 1px dotted slategray; background-color: lightgray;">
60+
runSpecs()
61+
</pre>
62+
</body>
63+
</html>

dev/xapi_schema/dev/cljs.clj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(ns xapi-schema.dev.cljs
2+
(:require [cljs.build.api :as api]
3+
[clojure.java.io :as io]))
4+
5+
6+
(def build-options
7+
{:optimizations :none
8+
:output-to "target/specs.js"
9+
:output-dir "target/cljs"
10+
:cache-analysis true
11+
:source-map true
12+
:pretty-print true
13+
:verbose true
14+
:watch-fn (fn [] (println "Success!"))
15+
})
16+
17+
(defn run-specs []
18+
(let [process (.exec (Runtime/getRuntime) "phantomjs bin/speclj.js")
19+
output (.getInputStream process)
20+
error (.getErrorStream process)]
21+
(io/copy output (System/out))
22+
(io/copy error (System/err))
23+
(System/exit (.waitFor process))))
24+
25+
(defn -main [& args]
26+
(api/build "spec" build-options)
27+
(run-specs))

dev/xapi_schema/dev/spec.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(ns xapi-schema.dev.spec
2+
"Run Speclj specs. Use this instead of the plugin to avoind conflicts with other installed versions."
3+
(:require [speclj.cli :as cli]))
4+
5+
(defn -main [& args]
6+
(apply cli/run "-p" "-c" "-f" "documentation" args))

project.clj

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,27 @@
1616
:profiles {:dev {:dependencies [[speclj "3.3.0"]]}}
1717

1818
:cljsbuild {:builds [{:id "dev"
19-
:source-paths ["src/cljc"]
19+
:source-paths ["src" "spec"]
2020
:compiler {:output-to "target/js/xapi_schema_dev.js"
21-
:optimizations :whitespace
22-
:pretty-print true}}
23-
{:id "test"
24-
:source-paths ["src/cljc" "spec/cljc" "spec/clj"]
25-
:compiler {:output-to "target/js/xapi_schema_test.js"
2621
:optimizations :whitespace
2722
:pretty-print true}
28-
:notify-command ["phantomjs" "bin/speclj" "target/js/xapi_schema_test.js"]}
23+
:notify-command ["phantomjs" "bin/speclj" "target/js/xapi_schema_dev.js"]
24+
}
2925
{:id "test-browser"
30-
:source-paths ["src/cljc"]
26+
:source-paths ["src"]
3127
:compiler {:output-to "resources/public/xapi_schema.js"
3228
:optimizations :advanced}}
3329
{:id "release"
34-
:source-paths ["src/cljc"]
30+
:source-paths ["src"]
3531
:compiler {:output-to "target/js/xapi_schema.js"
3632
:optimizations :advanced}}]
37-
:test-commands {"test" ["phantomjs" "bin/speclj" "target/js/xapi_schema_test.js"]}}
38-
:source-paths ["src/cljc"]
33+
:test-commands {"test" ["phantomjs" "bin/speclj" "target/js/xapi_schema_dev.js"]}}
3934
:resource-paths ["resources"]
40-
:test-paths ["spec/cljc" "spec/clj"]
41-
:aliases {"deploy-lib" ["deploy" "clojars"]
42-
"spec-clj" ["do" "clean," "spec"]
43-
"spec-cljs" ["do" "clean," "cljsbuild" "once" "test"]})
35+
:test-paths ["spec" "dev"]
36+
:aliases {"cljs" ["do" "clean," "run" "-m" "xapi-schema.dev.cljs"]
37+
"spec" ["do" "run" "-m" "xapi-schema.dev.spec"]
38+
"ci" ["do" "spec," "cljs"]}
39+
;; :aliases {"deploy-lib" ["deploy" "clojars"]
40+
;; "spec-clj" ["do" "clean," "spec"]
41+
;; "spec-cljs" ["do" "clean," "cljsbuild" "once" "test"]}
42+
)

spec/clj/xapi_schema/support/macros.clj

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
(ns xapi-schema.core-spec
2+
#?(:clj (:require
3+
[speclj.core :refer [describe
4+
it
5+
should
6+
should=
7+
should-not
8+
context
9+
with
10+
should-be-nil
11+
should-not-be-nil
12+
should-throw]]
13+
[speclj.platform]
14+
[speclj.components]
15+
[speclj.run.standard :refer [run-specs]]
16+
[xapi-schema.core :refer :all]
17+
[xapi-schema.support.data :as d :refer [long-statement]]
18+
[cheshire.core :as c]))
219
#?@(:cljs [(:require-macros [speclj.core :refer [describe
3-
it
4-
should
5-
should=
6-
should-not
7-
run-specs
8-
context
9-
with
10-
should-be-nil
11-
should-not-be-nil
12-
should-throw]])
13-
(:require [speclj.core]
14-
[xapi-schema.core :refer [statement-checker
15-
statements-checker
16-
validate-statement
17-
validate-statements
18-
validate-statement-data*
19-
validate-statement-data
20-
validate-statement-data-js]]
21-
[xapi-schema.support.data :as d :refer [long-statement]])])
22-
#?(:clj (:require [speclj.core :refer :all]
23-
[xapi-schema.core :refer :all]
24-
[xapi-schema.support.data :as d :refer [long-statement]]
25-
[cheshire.core :as c])))
20+
it
21+
should
22+
should=
23+
should-not
24+
context
25+
with
26+
should-be-nil
27+
should-not-be-nil
28+
should-throw]])
29+
(:require [speclj.core]
30+
[speclj.platform]
31+
[speclj.components]
32+
[speclj.run.standard :refer [run-specs]]
33+
[xapi-schema.core :refer [statement-checker
34+
statements-checker
35+
validate-statement
36+
validate-statements
37+
validate-statement-data*
38+
validate-statement-data
39+
validate-statement-data-js]]
40+
[xapi-schema.support.data :as d :refer [long-statement]])]))
2641

2742
(describe
2843
"statement-checker"
@@ -101,3 +116,5 @@
101116
(should (aget @js-statement "id")) ;;assert it is a js obj to begin
102117
(should= (long-statement "id")
103118
(aget (validate-statement-data-js @js-statement) "id")))))
119+
120+
(run-specs)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)