Test runner: Allow to run tests for only some languages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
diff --git a/package.json b/package.json
index 721b89d..208d2a5 100644
--- a/package.json
+++ b/package.json
@@ -25,6 +25,7 @@
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"gulp-replace": "^0.5.4",
- "mocha": "^2.2.5"
+ "mocha": "^2.2.5",
+ "yargs": "^3.26.0"
}
}
diff --git a/test-suite.html b/test-suite.html
index 906fe81..b84ee46 100644
--- a/test-suite.html
+++ b/test-suite.html
@@ -26,6 +26,13 @@
<p>Running the test suite is simple: just call <code class="language-bash">npm test</code>.</p>
<p>All test files are run in isolation. A new prism instance is created for each test case. This will slow the test runner a bit down, but we can be sure that nothing leaks into the next test case.</p>
+
+ <section id="running-tests-for-specific-languages">
+ <h2>Running tests for specific languages</h2>
+
+ <p>To run the tests only for one language, you can use the <code>language</code> parameter: <code class="language-bash">npm test -- --language=markup</code>.</p>
+ <p>You can even specify multiple languages: <code class="language-bash">npm test -- --language=markup --language=css</code>.</p>
+ </section>
</section>
<section id="writing-tests">
@@ -139,6 +146,7 @@ This is a comment explaining this test case.</code></pre>
<footer data-src="templates/footer.html" data-type="text/html"></footer>
<script src="prism.js"></script>
+<script src="components/prism-bash.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
diff --git a/tests/helper/test-discovery.js b/tests/helper/test-discovery.js
index 741dc75..622b619 100644
--- a/tests/helper/test-discovery.js
+++ b/tests/helper/test-discovery.js
@@ -25,6 +25,26 @@ module.exports = {
return testSuite;
},
+ /**
+ * Loads the list of available tests that match the given languages
+ *
+ * @param {string} rootDir
+ * @param {string|string[]} languages
+ * @returns {Object.<string, string[]>}
+ */
+ loadSomeTests: function (rootDir, languages) {
+ var testSuite = {};
+ var self = this;
+
+ this.getSomeDirectories(rootDir, languages).forEach(
+ function (language) {
+ testSuite[language] = self.getAllFiles(path.join(rootDir, language));
+ }
+ );
+
+ return testSuite;
+ },
+
/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
@@ -41,6 +61,38 @@ module.exports = {
);
},
+ /**
+ * Returns a list of all (sub)directories (just the directory names, not full paths)
+ * in the given src directory, matching the given languages
+ *
+ * @param {string} src
+ * @param {string|string[]} languages
+ * @returns {Array.<string>}
+ */
+ getSomeDirectories: function (src, languages) {
+ var self = this;
+ return fs.readdirSync(src).filter(
+ function (file) {
+ return fs.statSync(path.join(src, file)).isDirectory() && self.directoryMatches(file, languages);
+ }
+ );
+ },
+
+ /**
+ * Returns whether a directory matches one of the given languages.
+ * @param {string} directory
+ * @param {string|string[]} languages
+ */
+ directoryMatches: function (directory, languages) {
+ if (!Array.isArray(languages)) {
+ languages = [languages];
+ }
+ var dirLanguages = directory.split(/!?\+!?/);
+ return dirLanguages.some(function (lang) {
+ return languages.indexOf(lang) >= 0;
+ });
+ },
+
/**
* Returns a list of all full file paths to all files in the given src directory
diff --git a/tests/run.js b/tests/run.js
index 5e00504..a28350d 100644
--- a/tests/run.js
+++ b/tests/run.js
@@ -3,9 +3,15 @@
var TestDiscovery = require("./helper/test-discovery");
var TestCase = require("./helper/test-case");
var path = require("path");
+var argv = require("yargs").argv;
-// load complete test suite
-var testSuite = TestDiscovery.loadAllTests(__dirname + "/languages");
+var testSuite;
+if (argv.language) {
+ testSuite = TestDiscovery.loadSomeTests(__dirname + "/languages", argv.language);
+} else {
+ // load complete test suite
+ testSuite = TestDiscovery.loadAllTests(__dirname + "/languages");
+}
// define tests for all tests in all languages in the test suite
for (var language in testSuite) {