Skip to content

Commit

Permalink
fix(file-list): always use file from first matcher
Browse files Browse the repository at this point in the history
If a file is matched by multiple patterns, only the file from
the first pattern should be used.

This fixes issues that occurr when the same file appears in
multiple buckets. One such issue is that the watcher reruns
the preprocessors on the first occurrence of the file but
the second version of the file is the one being included in
the context. This results in the browser not requesting the
updated version of the file after the watch fires.
  • Loading branch information
chriscasola committed Apr 1, 2017
1 parent bcfac8a commit 74bfdf3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/file-list.js
Expand Up @@ -150,6 +150,7 @@ List.prototype._isRefreshing = function () {
List.prototype._refresh = function () {
var self = this
var buckets = this.buckets
var matchedFiles = new Set()

var promise = Promise.map(this._patterns, function (patternObject) {
var pattern = patternObject.pattern
Expand All @@ -174,6 +175,12 @@ List.prototype._refresh = function () {
return Promise.resolve()
}

if (matchedFiles.has(path)) {
return Promise.resolve()
}

matchedFiles.add(path)

var mtime = mg.statCache[path].mtime
var doNotCache = patternObject.nocache
var file = new File(path, mtime, doNotCache)
Expand All @@ -191,7 +198,7 @@ List.prototype._refresh = function () {
files = _.compact(files)

if (_.isEmpty(files)) {
log.warn('All files matched by "%s" were excluded.', pattern)
log.warn('All files matched by "%s" were excluded or matched by prior matchers.', pattern)
} else {
buckets.set(pattern, new Set(files))
}
Expand Down
38 changes: 30 additions & 8 deletions test/e2e/files.feature
Expand Up @@ -38,11 +38,15 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""
And it passes with like:
"""
files/log_foo.js" were excluded or matched by prior matchers.
"""

Scenario: Execute a test excluding an explicitly included file in another order
Given a configuration with:
Expand All @@ -59,11 +63,15 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""
And it passes with like:
"""
files/log_foo.js" were excluded or matched by prior matchers.
"""

Scenario: Execute a test excluding an file included with brackets patterns
Given a configuration with:
Expand All @@ -80,11 +88,15 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""
And it passes with like:
"""
files/{log,bug}_foo.js" were excluded or matched by prior matchers.
"""

Scenario: Execute a test excluding an file included with wildcard
Given a configuration with:
Expand All @@ -101,11 +113,15 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""
And it passes with like:
"""
files/*.js" were excluded or matched by prior matchers.
"""

Scenario: Execute a test excluding an file included with glob-star
Given a configuration with:
Expand All @@ -122,12 +138,15 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""

And it passes with like:
"""
files/**" were excluded or matched by prior matchers.
"""

Scenario: Execute a test excluding an file included with ext. glob patterns
Given a configuration with:
Expand All @@ -145,9 +164,12 @@ Feature: Including files
];
"""
When I start Karma
Then it passes with:
Then it passes with like:
"""
.
PhantomJS
"""

And it passes with like:
"""
files/{log,bug}_foo.js" were excluded or matched by prior matchers.
"""
11 changes: 11 additions & 0 deletions test/unit/file-list.spec.js
Expand Up @@ -266,6 +266,17 @@ describe('FileList', () => {
})
})

it('uses the file from the first matcher if two matchers match the same file', () => {
list = new List(patterns('/a.*', '*.txt'), [], emitter, preprocess, 100)
return list.refresh().then(() => {
var first = pathsFrom(list.buckets.get('/a.*'))
var second = pathsFrom(list.buckets.get('*.txt'))

expect(first).to.contain('/a.txt')
expect(second).not.to.contain('/a.txt')
})
})

it('cancels refreshs', () => {
var checkResult = (files) => {
expect(_.pluck(files.served, 'path')).to.contain('/some/a.js', '/some/b.js', '/some/c.js')
Expand Down

0 comments on commit 74bfdf3

Please sign in to comment.