gulpTasks-tests.js 10 KB

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