Repository: webpack-contrib/copy-webpack-plugin
Pull request #362 (2019-03-22 10:57:23)

fix: emit errors instead throw
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

Emit errors instead throw, so we don't break compilation if error happens

### Breaking Changes

No

### Additional Info

No

codecov[bot]: # [Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=h1) Report
> Merging [#362](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=desc) into [master](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/commit/05963eb91c62732afc090b9e992dc201b4d66cf7?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362/graphs/tree.svg?width=650&token=fEoU9iBFGL&height=150&src=pr)](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #362   +/-   ##
=======================================
  Coverage   94.75%   94.75%           
=======================================
  Files           8        8           
  Lines         286      286           
  Branches       75       75           
=======================================
  Hits          271      271           
  Misses         14       14           
  Partials        1        1
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/preProcessPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362/diff?src=pr&el=tree#diff-c3JjL3ByZVByb2Nlc3NQYXR0ZXJuLmpz) | `97.59% <100%> (ø)` | :arrow_up: |
| [src/processPattern.js](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362/diff?src=pr&el=tree#diff-c3JjL3Byb2Nlc3NQYXR0ZXJuLmpz) | `97.91% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=footer). Last update [05963eb...850fa83](https://codecov.io/gh/webpack-contrib/copy-webpack-plugin/pull/362?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/preProcessPattern.js b/src/preProcessPattern.js
index 4e7d6de..086b89f 100644
--- a/src/preProcessPattern.js
+++ b/src/preProcessPattern.js
@@ -34,7 +34,7 @@ export default function preProcessPattern(globalRef, pattern) {
 
     logger.error(message);
 
-    throw new Error(message);
+    compilation.errors.push(new Error(message));
   }
 
   pattern.to = pattern.to || '';
diff --git a/src/processPattern.js b/src/processPattern.js
index 1469eab..2731027 100644
--- a/src/processPattern.js
+++ b/src/processPattern.js
@@ -7,7 +7,7 @@ import minimatch from 'minimatch';
 import isObject from './utils/isObject';
 
 export default function processPattern(globalRef, pattern) {
-  const { logger, output, concurrency } = globalRef;
+  const { logger, output, concurrency, compilation } = globalRef;
   const globOptions = Object.assign(
     {
       cwd: pattern.context,
@@ -103,7 +103,7 @@ export default function processPattern(globalRef, pattern) {
 
               logger.error(message);
 
-              throw new Error(message);
+              compilation.errors.push(new Error(message));
             }
 
             file.webpackTo = path.relative(output, file.webpackTo);


Test patch:
diff --git a/test/CopyPlugin.test.js b/test/CopyPlugin.test.js
index 54039ef..835b6a8 100644
--- a/test/CopyPlugin.test.js
+++ b/test/CopyPlugin.test.js
@@ -157,6 +157,10 @@ describe('apply function', () => {
 
   const runEmit = (opts) =>
     run(opts).then((compilation) => {
+      if (opts.skipAssetsTesting) {
+        return;
+      }
+
       if (opts.expectedAssetKeys && opts.expectedAssetKeys.length > 0) {
         expect(Object.keys(compilation.assets).sort()).toEqual(
           opts.expectedAssetKeys
@@ -884,7 +888,24 @@ describe('apply function', () => {
 
     it('warns when pattern is empty', (done) => {
       runEmit({
-        expectedAssetKeys: [],
+        expectedAssetKeys: [
+          '.file.txt',
+          '[!]/hello.txt',
+          '[special?directory]/(special-*file).txt',
+          '[special?directory]/directoryfile.txt',
+          '[special?directory]/nested/nestedfile.txt',
+          'binextension.bin',
+          'dir (86)/file.txt',
+          'dir (86)/nesteddir/deepnesteddir/deepnesteddir.txt',
+          'dir (86)/nesteddir/nestedfile.txt',
+          'directory/.dottedfile',
+          'directory/directoryfile.txt',
+          'directory/nested/deep-nested/deepnested.txt',
+          'directory/nested/nestedfile.txt',
+          'file.txt',
+          'file.txt.gz',
+          'noextension',
+        ],
         expectedErrors: [new Error(`path "from" cannot be empty string`)],
         patterns: [
           {
@@ -964,7 +985,7 @@ describe('apply function', () => {
         compiler: new MockCompiler({
           outputPath: '/',
         }),
-        expectedAssetKeys: [],
+        skipAssetsTesting: true,
         expectedErrors: [
           new Error(
             'using older versions of webpack-dev-server, devServer.outputPath must be defined to write to absolute paths'