gulpTasks-tests.js 11 KB

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