From c8673d72f61a7c7d19db1b026029a870dc3697cd Mon Sep 17 00:00:00 2001 From: Konstantin Mamaev Date: Wed, 3 Apr 2019 21:28:28 +0300 Subject: [PATCH] Fresh tests --- src/lib/decl-processor.js | 27 +++++++------ src/type/copy.js | 9 ++++- src/type/custom.js | 2 +- src/type/inline.js | 6 +-- src/type/rebase.js | 2 +- test/lib/get-file.js | 55 ++++++++++++++----------- test/setup.js | 2 +- test/type/copy.js | 49 ++++++++++++---------- test/type/inline.js | 85 ++++++++++++++++++++------------------- test/type/rebase.js | 21 +++++----- 10 files changed, 142 insertions(+), 116 deletions(-) diff --git a/src/lib/decl-processor.js b/src/lib/decl-processor.js index 11e23a8..6c40b32 100644 --- a/src/lib/decl-processor.js +++ b/src/lib/decl-processor.js @@ -64,7 +64,7 @@ const wrapUrlProcessor = (urlProcessor, result, decl) => { }); return (asset, dir, option) => - urlProcessor(asset, dir, option, decl, warn, addDependency); + urlProcessor(asset, dir, option, decl, warn, result, addDependency); }; /** @@ -95,21 +95,26 @@ const replaceUrl = (url, dir, options, result, decl) => { return wrappedUrlProcessor(asset, dir, option); }; - let resultPromise; + let resultPromise = Promise.resolve(); if (Array.isArray(matchedOptions)) { - resultPromise = Promise.resolve(); - matchedOptions.forEach((option) => { - resultPromise = resultPromise.then(() => { - return process(option); - }); - }); + for (let i = 0; i < matchedOptions.length; i++) { + resultPromise = resultPromise + .then(() => process(matchedOptions[i])) + .then((newUrl) => { + asset.url = newUrl; + + return newUrl; + }); + } } else { resultPromise = process(matchedOptions); } return resultPromise.then((newUrl) => { asset.url = newUrl; + + return newUrl; }); }; @@ -127,13 +132,11 @@ const declProcessor = (from, to, options, result, decl) => { if (!pattern) return Promise.resolve(); - let id = 0; const promises = []; decl.value = decl.value .replace(pattern, (matched, before, url, after) => { const newUrlPromise = replaceUrl(url, dir, options, result, decl); - const marker = `::id${id++}`; promises.push( newUrlPromise @@ -145,11 +148,11 @@ const declProcessor = (from, to, options, result, decl) => { after = after.slice(1); } - decl.value = decl.value.replace(marker, `${before}${newUrl}${after}`); + decl.value = decl.value.replace(matched, `${before}${newUrl}${after}`); }) ); - return marker; + return matched; }); return Promise.all(promises); diff --git a/src/type/copy.js b/src/type/copy.js index 0f8fa6a..9580f47 100644 --- a/src/type/copy.js +++ b/src/type/copy.js @@ -20,7 +20,10 @@ const getHashName = (file, options) => const createDirAsync = (dirPath) => { return new Promise((resolve, reject) => { mkdirp(dirPath, (err) => { - if (err) reject(err); + if (err) { + reject(err); + } + resolve(); }); }); @@ -73,7 +76,7 @@ const writeFileAsync = (file, dest) => { * @returns {Promise} */ -module.exports = function processCopy(asset, dir, options, decl, warn, addDependency) { +module.exports = function processCopy(asset, dir, options, decl, warn, result, addDependency) { if (!options.assetsPath && dir.from === dir.to) { warn('Option `to` of postcss is required, ignoring'); @@ -82,6 +85,8 @@ module.exports = function processCopy(asset, dir, options, decl, warn, addDepend return getFile(asset, options, dir, warn) .then((file) => { + if (!file) return; + const assetRelativePath = options.useHash ? getHashName(file, options.hashOptions) : asset.relativePath; diff --git a/src/type/custom.js b/src/type/custom.js index 68338dd..d274601 100644 --- a/src/type/custom.js +++ b/src/type/custom.js @@ -9,5 +9,5 @@ * @returns {Promise} */ module.exports = function getCustomProcessor(asset, dir, options) { - return options.url.apply(null, arguments); + return Promise.resolve().then(() => options.url.apply(null, arguments)); }; diff --git a/src/type/inline.js b/src/type/inline.js index f100e07..416ea97 100644 --- a/src/type/inline.js +++ b/src/type/inline.js @@ -13,7 +13,7 @@ const getFile = require('../lib/get-file'); * * @returns {String|Undefined} */ -const processFallback = (originUrl, dir, options) => { +function processFallback(originUrl, dir, options) { if (typeof options.fallback === 'function') { return options.fallback.apply(null, arguments); } @@ -25,7 +25,7 @@ const processFallback = (originUrl, dir, options) => { default: return Promise.resolve(); } -}; +} const inlineProcess = (file, asset, warn, addDependency, options) => { const isSvg = file.mimeType === 'image/svg+xml'; @@ -65,7 +65,7 @@ const inlineProcess = (file, asset, warn, addDependency, options) => { * @returns {Promise} */ // eslint-disable-next-line complexity -module.exports = function(asset, dir, options, decl, warn, addDependency) { +module.exports = function(asset, dir, options, decl, warn, result, addDependency) { return getFile(asset, options, dir, warn) .then((file) => { if (!file) return; diff --git a/src/type/rebase.js b/src/type/rebase.js index 658c2bf..9caa37e 100644 --- a/src/type/rebase.js +++ b/src/type/rebase.js @@ -21,5 +21,5 @@ module.exports = function(asset, dir, options) { path.relative(dest, asset.absolutePath) ); - return Promise.resolve(`${rebasedUrl}${asset.search}${asset.hash}`); + return Promise.resolve().then(() => `${rebasedUrl}${asset.search}${asset.hash}`); }; diff --git a/test/lib/get-file.js b/test/lib/get-file.js index 41112b5..9f04fa2 100644 --- a/test/lib/get-file.js +++ b/test/lib/get-file.js @@ -14,13 +14,15 @@ describe('get-file', () => { pathname: '../pixel.gif', absolutePath: 'test/fixtures/pixel.gif' }; - const file = getFile(asset, {}, dir, warn); - assert.deepEqual(file, { - path: 'test/fixtures/pixel.gif', - contents: fileContent, - mimeType: 'image/gif' - }); + return getFile(asset, {}, dir, warn) + .then((file) => { + assert.deepEqual(file, { + path: 'test/fixtures/pixel.gif', + contents: fileContent, + mimeType: 'image/gif' + }); + }); }); it('should show warn message when can\'t read file', () => { @@ -30,12 +32,13 @@ describe('get-file', () => { }; let warnMessage = false; - getFile(asset, {}, dir, (message) => warnMessage = message); - - assert.equal( - warnMessage, - 'Can\'t read file \'test/fixtures/pixel-no-exists.gif\', ignoring' - ); + return getFile(asset, {}, dir, (message) => warnMessage = message) + .then(() => { + assert.equal( + warnMessage, + 'Can\'t read file \'test/fixtures/pixel-no-exists.gif\', ignoring' + ); + }); }); it('should read file with basePath option', () => { @@ -44,13 +47,15 @@ describe('get-file', () => { pathname: '../pixel.gif', absolutePath: 'test/fixtures/pixel-not-exists.gif' }; - const file = getFile(asset, options, dir, warn); - assert.deepEqual(file, { - path: path.resolve('test/fixtures/pixel.gif'), - contents: fileContent, - mimeType: 'image/gif' - }); + return getFile(asset, options, dir, warn) + .then((file) => { + assert.deepEqual(file, { + path: path.resolve('test/fixtures/pixel.gif'), + contents: fileContent, + mimeType: 'image/gif' + }); + }); }); it('should read file with multiple basePath option', () => { @@ -62,12 +67,14 @@ describe('get-file', () => { pathname: '../pixel.gif', absolutePath: 'test/fixtures/pixel-not-exists.gif' }; - const file = getFile(asset, options, dir, warn); - assert.deepEqual(file, { - path: path.resolve('test/fixtures/pixel.gif'), - contents: fileContent, - mimeType: 'image/gif' - }); + return getFile(asset, options, dir, warn) + .then((file) => { + assert.deepEqual(file, { + path: path.resolve('test/fixtures/pixel.gif'), + contents: fileContent, + mimeType: 'image/gif' + }); + }); }); }); diff --git a/test/setup.js b/test/setup.js index c759a55..47e5a6d 100644 --- a/test/setup.js +++ b/test/setup.js @@ -42,5 +42,5 @@ function processedCss(fixtures, urlOpts, postcssOpts) { return postcss() .use(url(urlOpts)) .process(read(fixtures), postcssOpts) - .css; + .then((res) => res.css); } diff --git a/test/type/copy.js b/test/type/copy.js index 24c9dec..f2e98a3 100644 --- a/test/type/copy.js +++ b/test/type/copy.js @@ -96,49 +96,54 @@ function testCopy(opts, postcssOpts) { describe('should copy asset from the source (`from`) to the assets destination (`to` + `assetsPath`)', () => { it('rebase the url', () => { - const css = processedCss('fixtures/copy', opts, postcssOpts); - - matchAll(css, ['copyPixelPng', 'copyPixelGif']); + return processedCss('fixtures/copy', opts, postcssOpts) + .then((css) => { + matchAll(css, ['copyPixelPng', 'copyPixelGif']); + }); }); it('rebase the url keeping parameters', () => { - const css = processedCss('fixtures/copy-parameters', opts, postcssOpts); - - matchAll(css, [ - 'copyParamsPixelPngHash', - 'copyParamsPixelPngParam', - 'copyParamsPixelGif' - ]); + return processedCss('fixtures/copy-parameters', opts, postcssOpts) + .then((css) => { + matchAll(css, [ + 'copyParamsPixelPngHash', + 'copyParamsPixelPngParam', + 'copyParamsPixelGif' + ]); + }); }); it('rebase the url using a hash name', () => { - const css = processedCss( + return processedCss( 'fixtures/copy-hash', optsWithHash, postcssOpts - ); - - matchAll(css, ['copyXXHashPixel8']); + ) + .then((css) => { + matchAll(css, ['copyXXHashPixel8']); + }); }); it('rebase the url using a hash name keeping parameters', () => { - const css = processedCss( + return processedCss( 'fixtures/copy-hash-parameters', optsWithHash, postcssOpts - ); - - matchAll(css, ['copyXXHashParamsPixel8']); + ) + .then((css) => { + matchAll(css, ['copyXXHashParamsPixel8']); + }); }); it('rebase the url using a hash and prepending the original filename', () => { - const css = processedCss( + return processedCss( 'fixtures/copy-hash', optsWithAppendHash, postcssOpts - ); - - matchAll(css, ['copyXXHashPrependPixel8']); + ) + .then((css) => { + matchAll(css, ['copyXXHashPrependPixel8']); + }); }); }); } diff --git a/test/type/inline.js b/test/type/inline.js index 0c3c215..96c88a2 100644 --- a/test/type/inline.js +++ b/test/type/inline.js @@ -30,29 +30,31 @@ describe('inline', () => { ); it('should inline url from dirname(from)', () => { - const css = processedCss('fixtures/inline-from', opts, postcssOpts); - - assert.ok(css.match(/;base64/)); + return processedCss('fixtures/inline-from', opts, postcssOpts) + .then((css) => { + assert.ok(css.match(/;base64/)); + }); }); it('should not inline big files from dirname(from)', () => { - const css = processedCss( + return processedCss( 'fixtures/inline-from', { url: 'inline', maxSize: 0.0001 }, { from: 'test/fixtures/here' } - ); - - assert.notOk(css.match(/;base64/)); + ).then((css) => { + assert.notOk(css.match(/;base64/)); + }); }); it('SVGs shouldn\'t be encoded in base64', () => { - const css = processedCss( + return processedCss( 'fixtures/inline-svg', { url: 'inline' }, postcssOpts - ); - - assert.notOk(css.match(/;base64/)); + ) + .then((css) => { + assert.notOk(css.match(/;base64/)); + }); }); compareFixtures( @@ -70,7 +72,7 @@ describe('inline', () => { ); it('should inline url of imported files', () => { - postcss() + return postcss() .use(require('postcss-import')()) .use(postcssUrl(opts)) .process(read('fixtures/inline-imported'), { from: 'test/fixtures/here' }) @@ -80,48 +82,49 @@ describe('inline', () => { }); it('should inline files matching the minimatch pattern', () => { - const css = processedCss( + return processedCss( 'fixtures/inline-by-type', { url: 'inline', filter: '**/*.svg' }, postcssOpts - ); - - assert.ok(css.match(/data\:image\/svg\+xml/)); - assert.notOk( - css.match(/data:image\/gif/), - 'shouldn\'t inline files not matching the minimatch pattern' - ); + ).then((css) => { + assert.ok(css.match(/data\:image\/svg\+xml/)); + assert.notOk( + css.match(/data:image\/gif/), + 'shouldn\'t inline files not matching the minimatch pattern' + ); + }); }); it('should inline files matching the regular expression', () => { - const css = processedCss( + return processedCss( 'fixtures/inline-by-type', { url: 'inline', filter: /\.svg$/ }, postcssOpts - ); - - assert.ok(css.match(/data\:image\/svg\+xml/)); - assert.notOk( - css.match(/data:image\/gif/), - 'shouldn\'t inline files not matching the regular expression' - ); + ).then((css) => { + assert.ok(css.match(/data\:image\/svg\+xml/)); + assert.notOk( + css.match(/data:image\/gif/), + 'shouldn\'t inline files not matching the regular expression' + ); + }); }); it('should inline files matching by custom function', () => { - const customFilterFunction = function(asset) { + const customFilterFunction = (asset) => { return /\.svg$/.test(asset.absolutePath); }; - const css = processedCss( + + return processedCss( 'fixtures/inline-by-type', { url: 'inline', filter: customFilterFunction }, postcssOpts - ); - - assert.ok(css.match(/data\:image\/svg\+xml/)); - assert.notOk( - css.match(/data:image\/gif/), - 'shouldn\'t inline files not matching the regular expression' - ); + ).then((css) => { + assert.ok(css.match(/data\:image\/svg\+xml/)); + assert.notOk( + css.match(/data:image\/gif/), + 'shouldn\'t inline files not matching the regular expression' + ); + }); }); describe('function when inline fallback', () => { @@ -151,7 +154,7 @@ describe('inline', () => { }); it('should find files in basePaths', () => { - const css = processedCss( + return processedCss( 'fixtures/inline-by-base-paths', { url: 'inline', @@ -159,8 +162,8 @@ describe('inline', () => { basePath: [path.resolve('test/fixtures/baseDir1'), 'baseDir2'] }, postcssOpts - ); - - assert.equal(css.match(/data:image\/png/g).length, 2); + ).then((css) => { + assert.equal(css.match(/data:image\/png/g).length, 2); + }); }); }); diff --git a/test/type/rebase.js b/test/type/rebase.js index 643dd6d..098b8d9 100644 --- a/test/type/rebase.js +++ b/test/type/rebase.js @@ -5,19 +5,19 @@ describe('rebase', () => { describe('base unit', () => { it('should calc relative path', () => { - const res = rebase({ + return rebase({ absolutePath: '/project/blocks/item/1.png', search: '', hash: '' }, { to: '/project/build' + }).then((res) => { + assert.equal(res, '../blocks/item/1.png'); }); - - assert.equal(res, '../blocks/item/1.png'); }); it('should calc relative path by assetsPath option', () => { - const res = rebase({ + return rebase({ absolutePath: '/project/blocks/item/1.png', search: '', hash: '' @@ -27,18 +27,21 @@ describe('rebase', () => { from: '/project/blocks/item/1.png' }, { assetsPath: '/project/build' - }); - - assert.equal(res, '../blocks/item/1.png'); + }) + .then((res) => { + assert.equal(res, '../blocks/item/1.png'); + }); }); }); it('rebase with empty options', () => { - processedCss( + return processedCss( 'fixtures/copy-hash', undefined, { from: 'test/fixtures/here' } - ).css; + ).then((css) => { + assert(css); + }); }); compareFixtures(