Commit 5ade8a58b2500dabdc82d17e9420dca28b38e2ca

Golmote 2015-10-06T08:25:54

Test runner: Allow to run tests for only some languages

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) {