gulpTasks-tests.js 13 KB

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