tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*global describe, before, it, after*/
  2. "use strict";
  3. var fs = require("fs");
  4. var path = require("path");
  5. var colors = require("colors");
  6. var cwd = process.cwd();
  7. var chai = require('chai');
  8. var expect = chai.expect;
  9. var nexpect = require("nexpect");
  10. var tests = "adownload, ambientlight, apng, appearance, applicationcache, arrow, atobbtoa, audio, audioautoplay, audioloop, audiopreload, backdropfilter, backgroundblendmode, backgroundcliptext, backgroundsize, batteryapi, bdi, beacon, bgpositionshorthand, bgpositionxy, bgrepeatround, bgrepeatspace, bgsizecover, blobconstructor, bloburls, blobworkers, borderimage, borderradius, boxdecorationbreak, boxshadow, boxsizing, canvas, canvasblending, canvastext, canvaswinding, capture, checked, classlist, connectioneffectivetype, contains, contenteditable, contextmenu, cookies, cors, createelement-attrs, createelementattrs, cryptography, cssall, cssanimations, csscalc, csschunit, csscolumns, cssescape, cssexunit, cssfilters, cssgradients, cssgrid, cssgridlegacy, csshyphens, cssinvalid, cssmask, csspointerevents, csspositionsticky, csspseudoanimations, csspseudotransitions, cssreflections, cssremunit, cssresize, cssscrollbar, csstransforms, csstransforms3d, csstransformslevel2, csstransitions, cssvalid, cssvhunit, cssvmaxunit, cssvminunit, cssvwunit, cubicbezierrange, customelements, customevent, customproperties, customprotocolhandler, dart, datachannel, datalistelem, dataset, datauri, dataview, dataworkers, details, devicemotion, deviceorientation, directory, display-runin, displaytable, documentfragment, ellipsis, emoji, es5, es5array, es5date, es5function, es5object, es5string, es5syntax, es5undefined, es6array, es6collections, es6math, es6number, es6object, es6string, eventlistener, eventsource, exiforientation, fetch, fileinput, filereader, filesystem, flash, flexbox, flexboxlegacy, flexboxtweener, flexwrap, focuswithin, fontdisplay, fontface, forcetouch, formattribute, formvalidation, framed, fullscreen, gamepads, generatedcontent, generators, geolocation, getrandomvalues, getusermedia, hairline, hashchange, hidden, hiddenscroll, history, hovermq, hsla, htmlimports, ie8compat, imgcrossorigin, indexeddb, indexeddbblob, inlinesvg, input, inputformaction, inputformenctype, inputformmethod, inputformnovalidate, inputformtarget, inputsearchevent, inputtypes, intersectionobserver, intl, jpeg2000, jpegxr, json, lastchild, ligatures, localizednumber, localstorage, lowbandwidth, lowbattery, matchmedia, mathml, mediaqueries, mediasource, messagechannel, meter, microdata, multiplebgs, mutationobserver, notification, nthchild, objectfit, olreversed, oninput, opacity, outputelem, overflowscrolling, pagevisibility, passiveeventlisteners, peerconnection, performance, picture, placeholder, pointerevents, pointerlock, pointermq, postmessage, preserve3d, progressbar, promises, proximity, publicKeyCredential, queryselector, quotamanagement, regions, requestanimationframe, requestautocomplete, rgba, ruby, sandbox, scriptasync, scriptdefer, scrollsnappoints, seamless, serviceworker, sessionstorage, shapes, sharedworkers, siblinggeneral, sizes, smil, softhyphens, softhyphensfind, speechrecognition, speechsynthesis, srcdoc, srcset, strictmode, stylescoped, subpixelfont, supports, svg, svgasimg, svgclippaths, svgfilters, svgforeignobject, target, template, templatestrings, textalignlast, textareamaxlength, textdecoration, textshadow, texttrackapi, time, todataurljpeg, todataurlpng, todataurlwebp, touchevents, track, transferables, typedarrays, unicode, unicoderange, unknownelements, urlparser, urlsearchparams, userdata, userselect, variablefonts, vibrate, video, videoautoplay, videocrossorigin, videoloop, videopreload, vml, webanimations, webaudio, webgl, webglextensions, webintents, webp, webp-lossless, webpalpha, webpanimation, webplossless, websockets, websocketsbinary, websqldatabase, webworkers, willchange, wrapflow, xdomainrequest, xhr2, xhrresponsetype, xhrresponsetypearraybuffer, xhrresponsetypeblob, xhrresponsetypedocument, xhrresponsetypejson, xhrresponsetypetext";
  11. var testArray = tests.split(", ");
  12. var cli = path.join(cwd, "bin", "customizr");
  13. var settingsPath = path.join(cwd, "test", "settings");
  14. var settings = {
  15. cache: path.join(settingsPath, "cache-invalidate.json"),
  16. custom: path.join(settingsPath, "custom.json"),
  17. exclude: path.join(settingsPath, "exclude.json"),
  18. prefixed: path.join(settingsPath, "prefixed.json"),
  19. select: path.join(settingsPath, "select.json")
  20. };
  21. describe("customizr", function () {
  22. var testsLength = testArray.length,
  23. existingBuild = path.join(cwd, "build", "modernizr-custom.js");
  24. it("should find all available tests in project", function (done) {
  25. process.stdout.write("\n\n");
  26. nexpect.spawn(cli, [
  27. "--config", settings.custom
  28. ], {
  29. stripColors: true
  30. })
  31. .wait("Looking for Modernizr references")
  32. .wait(">> " + testsLength + " matches in")
  33. .expect(">> " + tests)
  34. .expect(">> " + testsLength + " matches in")
  35. .expect(">> " + tests)
  36. .expect(">> " + testsLength + " matches in")
  37. .expect(">> " + tests)
  38. .expect(">> Ready to build using these settings:")
  39. .wait("Building your customized Modernizr").wait("OK")
  40. .expect(">> Success! Saved file to build/modernizr-custom.js")
  41. .run(function (err) {
  42. if (!err) {
  43. done();
  44. } else {
  45. throw err;
  46. }
  47. });
  48. });
  49. it("should avoid re-building a cached Modernizr build", function (done) {
  50. process.stdout.write("\n\n");
  51. nexpect.spawn(cli, [
  52. "--config", settings.custom
  53. ], {
  54. stripColors: true
  55. })
  56. .wait("Looking for Modernizr references")
  57. .wait(">> " + testsLength + " matches in")
  58. .expect(">> " + tests)
  59. .expect(">> " + testsLength + " matches in")
  60. .expect(">> " + tests)
  61. .expect(">> " + testsLength + " matches in")
  62. .expect(">> " + tests)
  63. .expect("No config or test changes detected")
  64. .expect(">> The build step has been bypassed. Use `--force` to override.")
  65. .expect(">> Your current file can be found in build/modernizr-custom.js")
  66. .run(function (err) {
  67. if (!err) {
  68. done();
  69. } else {
  70. throw err;
  71. }
  72. });
  73. });
  74. it("should force re-building a cached Modernizr build", function (done) {
  75. process.stdout.write("\n\n");
  76. nexpect.spawn(cli, [
  77. "--config", settings.custom,
  78. "--force"
  79. ], {
  80. stripColors: true
  81. })
  82. .wait("Looking for Modernizr references")
  83. .wait(">> " + testsLength + " matches in")
  84. .expect(">> " + tests)
  85. .expect(">> " + testsLength + " matches in")
  86. .expect(">> " + tests)
  87. .expect(">> " + testsLength + " matches in")
  88. .expect(">> " + tests)
  89. .expect(">> Ready to build using these settings:")
  90. .wait("Building your customized Modernizr").wait("OK")
  91. .expect(">> Success! Saved file to build/modernizr-custom.js")
  92. .run(function (err) {
  93. if (!err) {
  94. done();
  95. } else {
  96. throw err;
  97. }
  98. });
  99. });
  100. it("should invalidate a cached Modernizr build", function (done) {
  101. process.stdout.write("\n\n");
  102. nexpect.spawn(cli, [
  103. "--config", settings.cache
  104. ], {
  105. stripColors: true
  106. })
  107. .wait("Looking for Modernizr references")
  108. .wait(">> " + testsLength + " matches in")
  109. .expect(">> " + tests)
  110. .expect(">> " + testsLength + " matches in")
  111. .expect(">> " + tests)
  112. .expect(">> " + testsLength + " matches in")
  113. .expect(">> " + tests)
  114. .expect(">> Ready to build using these settings:")
  115. .wait("Building your customized Modernizr").wait("OK")
  116. .expect(">> Success! Saved file to build/modernizr-custom.js")
  117. .run(function (err) {
  118. if (!err) {
  119. done();
  120. } else {
  121. throw err;
  122. }
  123. });
  124. });
  125. describe("should include all tests", function () {
  126. var contents;
  127. testArray.forEach(function (test) {
  128. it(test, function (done) {
  129. contents = contents || fs.readFileSync(existingBuild, "utf8");
  130. expect(contents.indexOf(test)).to.not.equal(-1);
  131. done();
  132. });
  133. });
  134. });
  135. });
  136. describe("custom builds", function () {
  137. describe("declared tests", function () {
  138. it("should only build declared tests", function (done) {
  139. process.stdout.write("\n\n");
  140. nexpect.spawn(cli, [
  141. "--config", settings.select
  142. ], {
  143. stripColors: true
  144. })
  145. .wait(">> Explicitly including these tests:")
  146. .expect(">> siblinggeneral, svg, webintents")
  147. .expect("Skipping file traversal")
  148. .wait("Building your customized Modernizr").wait("OK")
  149. .expect(">> Success! Saved file to build/modernizr-select.js")
  150. .run(function (err) {
  151. if (!err) {
  152. done();
  153. } else {
  154. throw err;
  155. }
  156. });
  157. });
  158. describe("should only contain references to bundled tests", function () {
  159. var includedTestArray = [
  160. "webintents",
  161. "siblinggeneral",
  162. "svg"
  163. ];
  164. var testsLength = includedTestArray.length;
  165. var includedTests = includedTestArray.join(" ");
  166. var contents;
  167. includedTestArray.forEach(function (test) {
  168. it(test, function (done) {
  169. contents = contents || fs.readFileSync(path.join(cwd, "build", "modernizr-select.js"), "utf8");
  170. expect(contents.indexOf(test)).to.not.equal(-1);
  171. done();
  172. });
  173. });
  174. describe("should not contain these references", function (done) {
  175. var excludedTestArray = testArray.filter(function (test) {
  176. return includedTestArray.indexOf(test) === -1;
  177. });
  178. excludedTestArray.forEach(function (test) {
  179. it(test, function (done) {
  180. contents = contents || fs.readFileSync(path.join(cwd, "build", "modernizr-select.js"), "utf8");
  181. var testPattern = "addTest('" + test.toLowerCase();
  182. expect(contents.indexOf(testPattern)).to.equal(-1);
  183. done();
  184. });
  185. });
  186. });
  187. });
  188. });
  189. describe("excluded tests", function () {
  190. it("should build without excluded tests", function (done) {
  191. process.stdout.write("\n\n");
  192. nexpect.spawn(cli, [
  193. "--config", settings.exclude
  194. ], {
  195. stripColors: true
  196. })
  197. .wait(">> Explicitly excluding these tests:")
  198. .expect(">> applicationcache, emoji, notification")
  199. .wait("Building your customized Modernizr").wait("OK")
  200. .expect(">> Success! Saved file to build/modernizr-exclude.js")
  201. .run(function (err) {
  202. if (!err) {
  203. done();
  204. } else {
  205. throw err;
  206. }
  207. });
  208. });
  209. describe("should not contain references to excluded tests", function () {
  210. var excludedTestArray = [
  211. "applicationcache",
  212. "emoji",
  213. "notification"
  214. ];
  215. var testsLength = excludedTestArray.length;
  216. var excludedTests = excludedTestArray.join(" ");
  217. var contents;
  218. excludedTestArray.forEach(function (test) {
  219. it(test, function (done) {
  220. contents = contents || fs.readFileSync(path.join(cwd, "build", "modernizr-exclude.js"), "utf8");
  221. expect(contents.indexOf(test)).to.equal(-1);
  222. done();
  223. });
  224. });
  225. });
  226. });
  227. describe("prefix test", function () {
  228. it("should honor the specified prefix", function (done) {
  229. process.stdout.write("\n\n");
  230. nexpect.spawn(cli, [
  231. "--config", settings.prefixed
  232. ], {
  233. stripColors: true
  234. })
  235. .wait("Looking for Modernizr references")
  236. .expect(">> 3 matches in test/css/vanilla.css")
  237. .expect(">> cors, input, smil")
  238. .wait("Building your customized Modernizr").wait("OK")
  239. .expect(">> Success! Saved file to build/modernizr-prefixed.js")
  240. .run(function (err) {
  241. if (!err) {
  242. done();
  243. } else {
  244. throw err;
  245. }
  246. });
  247. });
  248. describe("should only contain references to bundled tests", function () {
  249. var includedTestArray = [
  250. "cors",
  251. "input",
  252. "smil"
  253. ];
  254. var testsLength = includedTestArray.length;
  255. var includedTests = includedTestArray.join(" ");
  256. var contents;
  257. includedTestArray.forEach(function (test) {
  258. it(test, function (done) {
  259. contents = contents || fs.readFileSync(path.join(cwd, "build", "modernizr-prefixed.js"), "utf8");
  260. expect(contents.indexOf(test)).to.not.equal(-1);
  261. done();
  262. });
  263. });
  264. describe("should not contain these references", function (done) {
  265. var excludedTestArray = testArray.filter(function (test) {
  266. return includedTestArray.indexOf(test) === -1;
  267. });
  268. excludedTestArray.forEach(function (test) {
  269. it(test, function (done) {
  270. contents = contents || fs.readFileSync(path.join(cwd, "build", "modernizr-prefixed.js"), "utf8");
  271. var testPattern = "addTest('" + test.toLowerCase();
  272. expect(contents.indexOf(testPattern)).to.equal(-1);
  273. done();
  274. });
  275. });
  276. });
  277. });
  278. });
  279. });