Skip to content

Commit

Permalink
Fresh tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Mamaev committed Apr 3, 2019
1 parent 85c742e commit c8673d7
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 116 deletions.
27 changes: 15 additions & 12 deletions src/lib/decl-processor.js
Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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;
});
};

Expand All @@ -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
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/type/copy.js
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -73,7 +76,7 @@ const writeFileAsync = (file, dest) => {
* @returns {Promise<String|Undefined>}
*/

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');

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/type/custom.js
Expand Up @@ -9,5 +9,5 @@
* @returns {Promise<String|Undefined>}
*/
module.exports = function getCustomProcessor(asset, dir, options) {
return options.url.apply(null, arguments);
return Promise.resolve().then(() => options.url.apply(null, arguments));
};
6 changes: 3 additions & 3 deletions src/type/inline.js
Expand Up @@ -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);
}
Expand All @@ -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';
Expand Down Expand Up @@ -65,7 +65,7 @@ const inlineProcess = (file, asset, warn, addDependency, options) => {
* @returns {Promise<String|Undefined>}
*/
// 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;
Expand Down
2 changes: 1 addition & 1 deletion src/type/rebase.js
Expand Up @@ -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}`);
};
55 changes: 31 additions & 24 deletions test/lib/get-file.js
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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'
});
});
});
});
2 changes: 1 addition & 1 deletion test/setup.js
Expand Up @@ -42,5 +42,5 @@ function processedCss(fixtures, urlOpts, postcssOpts) {
return postcss()
.use(url(urlOpts))
.process(read(fixtures), postcssOpts)
.css;
.then((res) => res.css);
}
49 changes: 27 additions & 22 deletions test/type/copy.js
Expand Up @@ -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']);
});
});
});
}

0 comments on commit c8673d7

Please sign in to comment.