gulpTasks-tests.js 10 KB

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