gulpTasks-tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Import Dependencies.
  2. var gulp = require("gulp");
  3. var typescript = require("gulp-typescript");
  4. var fs = require("fs");
  5. var karmaServer = require('karma').Server;
  6. var webpack = require('webpack');
  7. var webpackStream = require("webpack-stream");
  8. var rename = require("gulp-rename");
  9. // Read the full config.
  10. var config = require("../config.json");
  11. var relativeRootDir = "../../../";
  12. var rootDir = __dirname + "/" + relativeRootDir;
  13. // Helper functions
  14. var rmDir = function(dirPath) {
  15. try { var files = fs.readdirSync(dirPath); }
  16. catch (e) { return; }
  17. if (files.length > 0)
  18. for (var i = 0; i < files.length; i++) {
  19. var filePath = dirPath + '/' + files[i];
  20. if (fs.statSync(filePath).isFile())
  21. fs.unlinkSync(filePath);
  22. else
  23. rmDir(filePath);
  24. }
  25. fs.rmdirSync(dirPath);
  26. };
  27. /**
  28. * Launches the KARMA validation tests in chrome in order to debug them.
  29. */
  30. gulp.task("tests-validation-karma", function(done) {
  31. var kamaServerOptions = {
  32. configFile: rootDir + "tests/validation/karma.conf.js",
  33. singleRun: false
  34. };
  35. var server = new karmaServer(kamaServerOptions, done);
  36. server.start();
  37. });
  38. /**
  39. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  40. */
  41. gulp.task("tests-validation-virtualscreen", function(done) {
  42. var kamaServerOptions = {
  43. configFile: rootDir + "tests/validation/karma.conf.js",
  44. singleRun: true,
  45. browsers: ['Firefox']
  46. };
  47. var server = new karmaServer(kamaServerOptions, done);
  48. server.start();
  49. });
  50. /**
  51. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  52. */
  53. gulp.task("tests-validation-browserstack", function(done) {
  54. if (!process.env.BROWSER_STACK_USERNAME) {
  55. done();
  56. return;
  57. }
  58. var kamaServerOptions = {
  59. configFile: rootDir + "tests/validation/karma.conf.browserstack.js",
  60. singleRun: true
  61. };
  62. var server = new karmaServer(kamaServerOptions, done);
  63. server.start();
  64. });
  65. /**
  66. * Transpiles typescript unit tests.
  67. */
  68. gulp.task("tests-unit-transpile", function(done) {
  69. var tsProject = typescript.createProject(rootDir + "tests/unit/tsconfig.json");
  70. var tsResult = gulp.src(rootDir + "tests/unit/**/*.ts", { base: relativeRootDir })
  71. .pipe(tsProject());
  72. tsResult.once("error", function() {
  73. tsResult.once("finish", function() {
  74. console.log("Typescript compile failed");
  75. process.exit(1);
  76. });
  77. });
  78. return tsResult.js.pipe(gulp.dest(relativeRootDir));
  79. });
  80. /**
  81. * Launches the KARMA unit tests in Chrome.
  82. */
  83. gulp.task("tests-unit-debug", gulp.series("tests-unit-transpile", function(done) {
  84. var kamaServerOptions = {
  85. configFile: rootDir + "tests/unit/karma.conf.js",
  86. singleRun: false,
  87. browsers: ['Chrome']
  88. };
  89. var server = new karmaServer(kamaServerOptions, done);
  90. server.start();
  91. }));
  92. /**
  93. * Launches the KARMA unit tests in phantomJS.
  94. */
  95. gulp.task("tests-babylon-unit", gulp.series("tests-unit-transpile", function(done) {
  96. var kamaServerOptions = {
  97. configFile: rootDir + "tests/unit/karma.conf.js",
  98. singleRun: true
  99. };
  100. var server = new karmaServer(kamaServerOptions, done);
  101. server.start();
  102. }));
  103. /**
  104. * Transpiles viewer typescript unit tests.
  105. */
  106. gulp.task("tests-viewer-validation-transpile", function() {
  107. let wpBuild = webpackStream(require(relativeRootDir + 'Viewer/webpack.gulp.config.js'), webpack);
  108. // clean the built directory
  109. rmDir(relativeRootDir + "Viewer/tests/build/");
  110. return wpBuild
  111. .pipe(rename(function(path) {
  112. if (path.extname === '.js') {
  113. path.basename = "test";
  114. }
  115. }))
  116. .pipe(gulp.dest("../../Viewer/tests/build/"));
  117. });
  118. /**
  119. * Launches the viewer's KARMA validation tests in chrome in order to debug them.
  120. * (Can only be launch locally.)
  121. */
  122. gulp.task("tests-viewer-validation-karma", gulp.series("tests-viewer-validation-transpile", function(done) {
  123. var kamaServerOptions = {
  124. configFile: rootDir + "Viewer/tests/validation/karma.conf.js",
  125. singleRun: false
  126. };
  127. var server = new karmaServer(kamaServerOptions, done);
  128. server.start();
  129. }));
  130. /**
  131. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  132. * (Can only be launch on any branches.)
  133. */
  134. gulp.task("tests-viewer-validation-virtualscreen", gulp.series("tests-viewer-validation-transpile", function(done) {
  135. var kamaServerOptions = {
  136. configFile: rootDir + "Viewer/tests/validation/karma.conf.js",
  137. singleRun: true,
  138. browsers: ['Firefox']
  139. };
  140. var server = new karmaServer(kamaServerOptions, done);
  141. server.start();
  142. }));
  143. /**
  144. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  145. * (Can only be launch from secure branches.)
  146. */
  147. gulp.task("tests-viewer-validation-browserstack", gulp.series("tests-viewer-validation-transpile", function(done) {
  148. if (!process.env.BROWSER_STACK_USERNAME) {
  149. done();
  150. return;
  151. }
  152. var kamaServerOptions = {
  153. configFile: rootDir + "Viewer/tests/validation/karma.conf.browserstack.js",
  154. singleRun: true
  155. };
  156. var server = new karmaServer(kamaServerOptions, done);
  157. server.start();
  158. }));
  159. /**
  160. * Transpiles viewer typescript unit tests.
  161. */
  162. gulp.task("tests-viewer-transpile", function() {
  163. let wpBuild = webpackStream(require(relativeRootDir + 'Viewer/tests/unit/webpack.config.js'), webpack);
  164. // clean the built directory
  165. rmDir(relativeRootDir + "Viewer/tests/build/");
  166. return wpBuild
  167. .pipe(rename(function(path) {
  168. if (path.extname === '.js') {
  169. path.basename = "test";
  170. }
  171. }))
  172. .pipe(gulp.dest("../../Viewer/tests/build/"));
  173. });
  174. /**
  175. * Launches the KARMA unit tests in chrome.
  176. * (Can be launch on any branches.)
  177. */
  178. gulp.task("tests-viewer-unit-debug", gulp.series("tests-viewer-transpile", function(done) {
  179. var kamaServerOptions = {
  180. configFile: rootDir + "Viewer/tests/karma.conf.js",
  181. singleRun: false,
  182. browsers: ['Chrome']
  183. };
  184. var server = new karmaServer(kamaServerOptions, done);
  185. server.start();
  186. }));
  187. /**
  188. * Launches the KARMA unit tests in phantomJS.
  189. */
  190. gulp.task("tests-viewer-unit", gulp.series("tests-viewer-transpile", function(done) {
  191. var kamaServerOptions = {
  192. configFile: rootDir + "Viewer/tests/karma.conf.js",
  193. singleRun: true
  194. };
  195. var server = new karmaServer(kamaServerOptions, done);
  196. server.start();
  197. }));
  198. /**
  199. * Launches the KARMA unit tests in phantomJS.
  200. */
  201. gulp.task("tests-unit", gulp.series("tests-babylon-unit", "tests-viewer-unit"));
  202. /**
  203. * Launches the KARMA module tests in phantomJS.
  204. */
  205. gulp.task("tests-modules", function() {
  206. let testsToRun = require(relativeRootDir + 'tests/modules/tests.json');
  207. let sequencePromise = Promise.resolve();
  208. testsToRun.tests.forEach(test => {
  209. sequencePromise = sequencePromise.then(() => {
  210. console.log("Running " + test.name);
  211. let basePath = relativeRootDir + 'tests/modules/' + test.name + '/';
  212. rmDir(relativeRootDir + "tests/modules/build/");
  213. let compilePromise = Promise.resolve();
  214. if (test.dependencies) {
  215. compilePromise = new Promise(function(resolve, reject) {
  216. let counter = 0;
  217. let copyTask = gulp.src(test.dependencies.map(dep => config.build.outputDirectory + '/' + dep)).pipe(rename(function(path) {
  218. path.basename = (counter++) + '';
  219. })).pipe(gulp.dest("../../tests/modules/build/dependencies/"))
  220. copyTask.once("finish", resolve);
  221. })
  222. }
  223. // any compilation needed?
  224. if (test.typescript || test.bundler) {
  225. //typescript only
  226. if (test.typescript && !test.bundler) {
  227. compilePromise = compilePromise.then(() => {
  228. return new Promise(function(resolve, reject) {
  229. var tsProject = typescript.createProject(basePath + (test.tsconfig || 'tsconfig.json'));
  230. var tsResult = gulp.src(basePath + '/src/**/*.ts', { base: basePath })
  231. .pipe(tsProject());
  232. let error = false;
  233. tsResult.once("error", function() {
  234. error = true;
  235. });
  236. let jsPipe = tsResult.js.pipe(gulp.dest(relativeRootDir + "tests/modules/"));
  237. jsPipe.once("finish", function() {
  238. if (error)
  239. reject('error compiling test');
  240. else
  241. resolve();
  242. });
  243. });
  244. });
  245. } else {
  246. if (test.bundler === 'webpack') {
  247. console.log("webpack");
  248. compilePromise = compilePromise.then(() => {
  249. return new Promise(function(resolve, reject) {
  250. let wpBuild = webpackStream(require(basePath + '/webpack.config.js'), webpack);
  251. wpBuild = wpBuild
  252. .pipe(rename(function(path) {
  253. if (path.extname === '.js') {
  254. path.basename = "tests-loader";
  255. }
  256. }))
  257. .pipe(gulp.dest("../../tests/modules/build/"));
  258. wpBuild.once("finish", resolve);
  259. })
  260. });
  261. }
  262. }
  263. }
  264. return compilePromise.then(() => {
  265. return new Promise(function(resolve, reject) {
  266. var kamaServerOptions = {
  267. configFile: rootDir + "tests/modules/karma.conf.js",
  268. singleRun: true
  269. };
  270. var server = new karmaServer(kamaServerOptions, resolve);
  271. server.start();
  272. });
  273. })
  274. })
  275. });
  276. return sequencePromise;
  277. });