Skip to content

Commit 958bee3

Browse files
authored
feat(@angular/cli): don't add empty assets to karma (angular#4952)
Removes the warning that would appear on new projects when running `ng test` due to there being no files inside `src/assets/`: ``` 23 02 2017 10:45:33.751:WARN [watcher]: Pattern "D:\sandbox\master-project\src\assets/**" does not match any file. ```
1 parent f9a97a7 commit 958bee3

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

packages/@angular/cli/plugins/karma.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
3+
import * as glob from 'glob';
34

45
import { CliConfig } from '../models/config';
56
import { Pattern } from './glob-copy-webpack-plugin';
@@ -16,6 +17,29 @@ function isDirectory(path: string) {
1617
}
1718
}
1819

20+
// Add files to the Karma files array.
21+
function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
22+
const defaults = {
23+
included: true,
24+
served: true,
25+
watched: true
26+
};
27+
28+
const processedFiles = newFiles
29+
// Remove globs that do not match any files, otherwise Karma will show a warning for these.
30+
.filter(file => glob.sync(file.pattern, { nodir: true }).length != 0)
31+
// Fill in pattern properties with defaults.
32+
.map(file => ({ ...defaults, ...file }));
33+
34+
// It's important to not replace the array, because
35+
// karma already has a reference to the existing array.
36+
if (prepend) {
37+
files.unshift(...processedFiles);
38+
} else {
39+
files.push(...processedFiles);
40+
}
41+
}
42+
1943
const init: any = (config: any) => {
2044
const apps = CliConfig.fromProject().config.apps;
2145
const appConfig = getAppFromConfig(apps, config.angularCli.app);
@@ -42,12 +66,7 @@ const init: any = (config: any) => {
4266
// Build karma file pattern.
4367
const assetPath = path.join(pattern.input, pattern.glob);
4468
const filePattern = isDirectory(assetPath) ? assetPath + '/**' : assetPath;
45-
config.files.push({
46-
pattern: filePattern,
47-
included: false,
48-
served: true,
49-
watched: true
50-
});
69+
addKarmaFiles(config.files, [{ pattern: filePattern, included: false }]);
5170

5271
// The `files` entry serves the file from `/base/{asset.input}/{asset.glob}`.
5372
// We need to add a URL rewrite that exposes the asset as `/{asset.output}/{asset.glob}`.
@@ -99,31 +118,15 @@ const init: any = (config: any) => {
99118
const globalScriptPatterns = extraEntryParser(appConfig.scripts, appRoot, 'scripts')
100119
// Neither renamed nor lazy scripts are currently supported
101120
.filter(script => !(script.output || script.lazy))
102-
.map(script => ({
103-
pattern: path.resolve(appRoot, script.input),
104-
included: true,
105-
served: true,
106-
watched: true
107-
}));
108-
109-
// Unshift elements onto the beginning of the files array.
110-
// It's important to not replace the array, because
111-
// karma already has a reference to the existing array.
112-
config.files.unshift(...globalScriptPatterns);
121+
.map(script => ({ pattern: path.resolve(appRoot, script.input) }));
122+
addKarmaFiles(config.files, globalScriptPatterns, true);
113123
}
114124

115125
// Add polyfills file before everything else
116126
if (appConfig.polyfills) {
117127
const polyfillsFile = path.resolve(appRoot, appConfig.polyfills);
118-
const polyfillsPattern = {
119-
pattern: polyfillsFile,
120-
included: true,
121-
served: true,
122-
watched: true
123-
};
124128
config.preprocessors[polyfillsFile] = ['webpack', 'sourcemap'];
125-
// Same as above.
126-
config.files.unshift(polyfillsPattern);
129+
addKarmaFiles(config.files, [{ pattern: polyfillsFile }], true);
127130
}
128131
};
129132

0 commit comments

Comments
 (0)