Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #231 allow square brackets in path #264

Merged
merged 7 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"pretest": "npm run lint && npm run build && npm run build:tests",
"test": "mocha compiled_tests/",
"build": "babel src/ --out-dir dist/",
"build:tests": "babel tests/ --out-dir compiled_tests/ && ncp tests/helpers compiled_tests/helpers"
"build:tests": "babel tests/ --out-dir compiled_tests/ && rimraf compiled_tests/helpers && ncp tests/helpers compiled_tests/helpers && node scripts/createSpecialDirectory.js"
},
"dependencies": {
"globby": "^7.1.1",
Expand All @@ -34,12 +34,14 @@
"babel-cli": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"chai": "^3.4.0",
"eslint": "^2.9.0",
"enhanced-resolve": "^3.4.1",
"eslint": "^2.9.0",
"is-gzip": "^2.0.0",
"mkdirp": "^0.5.1",
"mocha": "^2.4.5",
"ncp": "^2.0.0",
"standard-version": "^4.2.0",
"is-gzip": "^2.0.0"
"rimraf": "^2.6.2",
"standard-version": "^4.2.0"
},
"homepage": "https://github.com/webpack-contrib/copy-webpack-plugin",
"bugs": "https://github.com/webpack-contrib/copy-webpack-plugin/issues",
Expand Down
20 changes: 20 additions & 0 deletions scripts/createSpecialDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const removeIllegalCharacterForWindows = require('./removeIllegalCharacterForWindows');

const baseDir = 'compiled_tests/helpers';

const specialFiles = {
'[special?directory]/nested/nestedfile.txt': '',
'[special?directory]/(special-*file).txt': 'special',
'[special?directory]/directoryfile.txt': 'new'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a test with ! in the file/path should be added to ensure that [!] works as intended.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loveky yep, add test

};

Object.keys(specialFiles).forEach(function (originFile) {
const file = removeIllegalCharacterForWindows(originFile);
const dir = path.dirname(file);
mkdirp.sync(path.join(baseDir, dir));
fs.writeFileSync(path.join(baseDir, file), specialFiles[originFile]);
});

6 changes: 6 additions & 0 deletions scripts/removeIllegalCharacterForWindows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path');

module.exports = function (string) {
return path.sep === '/' ? string : string.replace(/[*?"<>|]/g, '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If those are illegal characters on windows only then a check for windows should be used instead of path.sep which does not guarantee windows. (process.platform === 'win32'?)

};

2 changes: 1 addition & 1 deletion src/utils/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function escape(context, from) {
// Handles special characters in paths
const absoluteContext = path.resolve(context)
.replace(/\\/, '/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this replacement be removed if the character class escaping method is being used?

Also, on MacOS, this r\!4 is a valid directory name.

.replace(/[\*|\?|\!|\(|\)|\[|\]|\{|\}]/g, (substring) => `\\${substring}`);
.replace(/[\*|\?|\!|\(|\)|\[|\{|\}]/g, (substring) => `[${substring}]`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closing brace should probably stay for consistency (i.e., makes for a simple rule of escape all special characters).


if (!from) {
return absoluteContext;
Expand Down
1 change: 0 additions & 1 deletion tests/helpers/[special?directory]/(special-*file).txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/helpers/[special?directory]/directoryfile.txt

This file was deleted.

Empty file.
17 changes: 13 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import cacache from 'cacache';
import isGzip from 'is-gzip';
import zlib from 'zlib';

import removeIllegalCharacterForWindows from '../scripts/removeIllegalCharacterForWindows';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be in src/utils instead of scripts/ otherwise it won't get published.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loveky yep, move this to tests/utils


const BUILD_DIR = path.join(__dirname, 'build');
const HELPER_DIR = path.join(__dirname, 'helpers');
const TEMP_DIR = path.join(__dirname, 'tempdir');
Expand Down Expand Up @@ -58,6 +60,13 @@ describe('apply function', () => {
// Ideally we pass in patterns and confirm the resulting assets
const run = (opts) => {
return new Promise((resolve, reject) => {
if (Array.isArray(opts.patterns)) {
opts.patterns.forEach(function (pattern) {
if (pattern.context) {
pattern.context = removeIllegalCharacterForWindows(pattern.context);
}
});
}
const plugin = CopyWebpackPlugin(opts.patterns, opts.options);

// Get a mock compiler to pass to plugin.apply
Expand Down Expand Up @@ -109,7 +118,7 @@ describe('apply function', () => {
return run(opts)
.then((compilation) => {
if (opts.expectedAssetKeys && opts.expectedAssetKeys.length > 0) {
expect(compilation.assets).to.have.all.keys(opts.expectedAssetKeys);
expect(compilation.assets).to.have.all.keys(opts.expectedAssetKeys.map(removeIllegalCharacterForWindows));
} else {
expect(compilation.assets).to.deep.equal({});
}
Expand Down Expand Up @@ -451,7 +460,7 @@ describe('apply function', () => {
],
patterns: [{
from: '*/*.*',
test: /([^\/]+)\/([^\/]+)\.\w+$/,
test: `([^\\${path.sep}]+)\\${path.sep}([^\\${path.sep}]+)\\.\\w+$`,
to: '[1]-[2].[ext]'
}]
})
Expand Down Expand Up @@ -984,7 +993,7 @@ describe('apply function', () => {
'nested/nestedfile.txt'
],
patterns: [{
from: '[special?directory]'
from: (path.sep === '/' ? '[special?directory]' : '[specialdirectory]')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path.sep conditionals are fairly non-obvious as to their purpose. Separate windows only tests would probably make more sense and simplify maintenance moving forward.

@evilebottnawi A Windows CI instance would probably be very useful for this plugin since windows pathing is vastly different than linux/MacOS/etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clydin we already use appveyor for many plugins, need merge webpack-default here, will be done in near future

}]
})
.then(done)
Expand Down Expand Up @@ -1390,7 +1399,7 @@ describe('apply function', () => {
'noextension'
],
options: {
ignore: ['directory/**/*', '\\[special\\?directory\\]/**/*']
ignore: ['directory/**/*', (path.sep === '/' ? '\\[special\\?directory\\]/**/*' : '[[]specialdirectory]/**/*')]
Copy link
Contributor

@clydin clydin May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The escape sequences should be consistent and there should really be a test for both escaping techniques (where appropriate) as they are both accepted.

},
patterns: [{
from: '.'
Expand Down