gulpTasks-tests.js 11 KB

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