gulpTasks-tests.js 11 KB

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