Skip to content

Commit

Permalink
[test] Add test for globFollow option
Browse files Browse the repository at this point in the history
  • Loading branch information
adrigzr committed Dec 7, 2017
1 parent bd8e932 commit b317257
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const defaults = require('./defaults');
// Define some common constrains used by all methods.
module.exports = joi.object().keys({
dontCacheBustUrlsMatching: joi.object().type(RegExp),
globFollow: joi.boolean().default(defaults.globFollow),
globIgnores: joi.array().items(joi.string()).default(defaults.globIgnores),
globPatterns: joi.array().items(joi.string()).default(defaults.globPatterns),
globStrict: joi.boolean().default(defaults.globStrict),
manifestTransforms: joi.array().items(joi.func().arity(1)),
maximumFileSizeToCacheInBytes: joi.number().min(1)
.default(defaults.maximumFileSizeToCacheInBytes),
Expand Down
2 changes: 2 additions & 0 deletions packages/workbox-build/src/entry-points/options/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

module.exports = {
globFollow: true,
globIgnores: ['node_modules/**/*'],
globPatterns: ['**/*.{js,css,html}'],
globStrict: true,
// Use a different default for generateSWString.
generateSWStringGlobPatterns: [],
maximumFileSizeToCacheInBytes: 2 * 1024 * 1024,
Expand Down
7 changes: 3 additions & 4 deletions packages/workbox-build/src/lib/get-file-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ const getFileHash = require('./get-file-hash');
module.exports = (globOptions) => {
const {
globDirectory,
globFollow,
globIgnores,
globPattern,
globFollow,
globStrict,
} = globOptions;
let globbedFiles;
try {
globbedFiles = glob.sync(globPattern, {
cwd: globDirectory,
follow: globFollow,
ignore: globIgnores,
follow: typeof globFollow !== 'undefined'? globFollow : true,
strict: typeof globStrict !== 'undefined'? globStrict : true,

strict: globStrict,
});
} catch (err) {
throw new Error(errors['unable-to-glob-files'] + ` '${err.message}'`);
Expand Down
10 changes: 8 additions & 2 deletions packages/workbox-build/src/lib/get-file-manifest-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const getStringDetails = require('./get-string-details');
module.exports = async ({
dontCacheBustUrlsMatching,
globDirectory,
globFollow,
globIgnores,
globPatterns,
globStrict,
manifestTransforms,
maximumFileSizeToCacheInBytes,
modifyUrlPrefix,
Expand All @@ -48,8 +50,10 @@ module.exports = async ({
fileDetails = globPatterns.reduce((accumulated, globPattern) => {
const globbedFileDetails = getFileDetails({
globDirectory,
globPattern,
globFollow,
globIgnores,
globPattern,
globStrict,
});

globbedFileDetails.forEach((fileDetails) => {
Expand All @@ -74,8 +78,10 @@ module.exports = async ({
try {
const globbedFileDetails = getFileDetails({
globDirectory,
globPattern,
globFollow,
globIgnores,
globPattern,
globStrict,
});
return previous.concat(globbedFileDetails);
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions test/workbox-build/node/entry-points/generate-sw-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ describe(`[workbox-build] entry-points/generate-sw-string.js (End to End)`, func
'directoryIndex',
'dontCacheBustUrlsMatching',
'globDirectory',
'globFollow',
'globIgnores',
'globPatterns',
'globStrict',
'ignoreUrlParametersMatching',
'injectionPointRegexp',
'manifestTransforms',
Expand Down
77 changes: 77 additions & 0 deletions test/workbox-build/node/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ describe(`[workbox-build] entry-points/generate-sw.js (End to End)`, function()
'clientsClaim',
'directoryIndex',
'dontCacheBustUrlsMatching',
'globFollow',
'globIgnores',
'globPatterns',
'globStrict',
'ignoreUrlParametersMatching',
'importScripts',
'importWorkboxFromCDN',
Expand Down Expand Up @@ -372,6 +374,81 @@ describe(`[workbox-build] entry-points/generate-sw.js (End to End)`, function()
}]],
}});
});

it(`should use defaults when all the required parameters are present, with symlinks`, async function() {
const swDest = tempy.file();
const globDirectory = tempy.directory();

await fse.ensureSymlink(GLOB_DIR, path.join(globDirectory, 'link'));

const options = Object.assign({}, BASE_OPTIONS, {
globDirectory,
swDest,
});

const {count, size} = await generateSW(options);

expect(count).to.eql(6);
expect(size).to.eql(2421);
await validateServiceWorkerRuntime({swFile: swDest, expectedMethodCalls: {
importScripts: [[WORKBOX_SW_CDN_URL]],
suppressWarnings: [[]],
precacheAndRoute: [[[{
url: 'link/index.html',
revision: '3883c45b119c9d7e9ad75a1b4a4672ac',
}, {
url: 'link/page-1.html',
revision: '544658ab25ee8762dc241e8b1c5ed96d',
}, {
url: 'link/page-2.html',
revision: 'a3a71ce0b9b43c459cf58bd37e911b74',
}, {
url: 'link/styles/stylesheet-1.css',
revision: '934823cbc67ccf0d67aa2a2eeb798f12',
}, {
url: 'link/styles/stylesheet-2.css',
revision: '884f6853a4fc655e4c2dc0c0f27a227c',
}, {
url: 'link/webpackEntry.js',
revision: 'd41d8cd98f00b204e9800998ecf8427e',
}], {}]],
}});
});

it(`should use defaults when all the required parameters are present, with 'globFollow' and symlinks`, async function() {
const swDest = tempy.file();
const globDirectory = tempy.directory();

await fse.ensureSymlink(GLOB_DIR, path.join(globDirectory, 'link'));

const options = Object.assign({}, BASE_OPTIONS, {
globDirectory,
globFollow: false,
swDest,
});

const {count, size} = await generateSW(options);

expect(count).to.eql(4);
expect(size).to.eql(2352);
await validateServiceWorkerRuntime({swFile: swDest, expectedMethodCalls: {
importScripts: [[WORKBOX_SW_CDN_URL]],
suppressWarnings: [[]],
precacheAndRoute: [[[{
url: 'link/index.html',
revision: '3883c45b119c9d7e9ad75a1b4a4672ac',
}, {
url: 'link/page-1.html',
revision: '544658ab25ee8762dc241e8b1c5ed96d',
}, {
url: 'link/page-2.html',
revision: 'a3a71ce0b9b43c459cf58bd37e911b74',
}, {
url: 'link/webpackEntry.js',
revision: 'd41d8cd98f00b204e9800998ecf8427e',
}], {}]],
}});
});
});

describe(`[workbox-build] behavior with 'runtimeCaching'`, function() {
Expand Down
33 changes: 33 additions & 0 deletions test/workbox-build/node/entry-points/get-manifest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const expect = require('chai').expect;
const fse = require('fs-extra');
const path = require('path');
const tempy = require('tempy');

const getManifest = require('../../../../packages/workbox-build/src/entry-points/get-manifest');

Expand All @@ -11,8 +13,10 @@ describe(`[workbox-build] entry-points/get-manifest.js (End to End)`, function()
const REQUIRED_PARAMS = ['globDirectory'];
const SUPPORTED_PARAMS = [
'dontCacheBustUrlsMatching',
'globFollow',
'globIgnores',
'globPatterns',
'globStrict',
'manifestTransforms',
'maximumFileSizeToCacheInBytes',
'modifyUrlPrefix',
Expand Down Expand Up @@ -259,5 +263,34 @@ describe(`[workbox-build] entry-points/get-manifest.js (End to End)`, function()
expect(count).to.eql(2);
expect(size).to.eql(50);
});

it(`should use defaults when all the required parameters are present, with 'globFollow' and symlinks`, async function() {
const globDirectory = tempy.directory();

await fse.ensureSymlink(SRC_DIR, path.join(globDirectory, 'link'));

const options = Object.assign({}, BASE_OPTIONS, {
globDirectory,
globFollow: false,
});

const {count, size, manifestEntries} = await getManifest(options);

expect(manifestEntries).to.deep.equal([{
url: 'link/index.html',
revision: '3883c45b119c9d7e9ad75a1b4a4672ac',
}, {
url: 'link/page-1.html',
revision: '544658ab25ee8762dc241e8b1c5ed96d',
}, {
url: 'link/page-2.html',
revision: 'a3a71ce0b9b43c459cf58bd37e911b74',
}, {
url: 'link/webpackEntry.js',
revision: 'd41d8cd98f00b204e9800998ecf8427e',
}]);
expect(count).to.eql(4);
expect(size).to.eql(2352);
});
});
});
2 changes: 2 additions & 0 deletions test/workbox-build/node/entry-points/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ describe(`[workbox-build] entry-points/inject-manifest.js (End to End)`, functio
];
const SUPPORTED_PARAMS = [
'dontCacheBustUrlsMatching',
'globFollow',
'globIgnores',
'globPatterns',
'globStrict',
'injectionPointRegexp',
'manifestTransforms',
'maximumFileSizeToCacheInBytes',
Expand Down
8 changes: 7 additions & 1 deletion test/workbox-build/node/entry-points/options/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ describe(`[workbox-build] entry-points/options/validate.js`, function() {
const options = validate({}, baseSchema);

expect(options).to.eql({
globFollow: true,
globIgnores: ['node_modules/**/*'],
globPatterns: ['**/*.{js,css,html}'],
globStrict: true,
maximumFileSizeToCacheInBytes: 2097152,
});
});
Expand All @@ -35,9 +37,11 @@ describe(`[workbox-build] entry-points/options/validate.js`, function() {
const options = validate({maximumFileSizeToCacheInBytes}, baseSchema);

expect(options).to.eql({
maximumFileSizeToCacheInBytes,
globFollow: true,
globIgnores: ['node_modules/**/*'],
globPatterns: ['**/*.{js,css,html}'],
globStrict: true,
maximumFileSizeToCacheInBytes,
});
});

Expand All @@ -47,8 +51,10 @@ describe(`[workbox-build] entry-points/options/validate.js`, function() {

expect(options).to.eql({
dontCacheBustUrlsMatching,
globFollow: true,
globIgnores: ['node_modules/**/*'],
globPatterns: ['**/*.{js,css,html}'],
globStrict: true,
maximumFileSizeToCacheInBytes: 2097152,
});
});
Expand Down
42 changes: 0 additions & 42 deletions test/workbox-build/node/lib/get-file-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,4 @@ describe(`[workbox-build] lib/get-file-details.js`, function() {
size: SIZE,
}]);
});

it(`should call sync with follow and strict options by default`, function() {
const getFileDetails = proxyquire(MODULE_PATH, {
'glob': {
sync: (pattern, options) => {
expect(options.follow).to.be.true;
expect(options.strict).to.be.true;

return [FILE1];
},
},
'./get-file-size': (value) => SIZE,
'./get-file-hash': (value) => HASH,
});

getFileDetails({
globDirectory: GLOB_DIRECTORY,
globPattern: GLOB_PATTERN,
});
});

it(`should call sync with follow and strict options with value`, function() {
const getFileDetails = proxyquire(MODULE_PATH, {
'glob': {
sync: (pattern, options) => {
expect(options.follow).to.be.false;
expect(options.strict).to.be.false;

return [FILE1];
},
},
'./get-file-size': (value) => SIZE,
'./get-file-hash': (value) => HASH,
});

getFileDetails({
globDirectory: GLOB_DIRECTORY,
globPattern: GLOB_PATTERN,
globFollow: false,
globStrict: false,
});
});
});

0 comments on commit b317257

Please sign in to comment.