Skip to content

Commit f399063

Browse files
authoredMay 16, 2020
fix: detect type for URLs with query parameter or fragment identifier (#3509)
Closes #3497
1 parent 17b50bc commit f399063

File tree

7 files changed

+103
-6
lines changed

7 files changed

+103
-6
lines changed
 

‎docs/dev/05-plugins.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A preprocessor is a function that accepts three arguments (`content`, `file`, an
2727
- **path:** the current file, mutable file path. e. g. `some/file.coffee` -> `some/file.coffee.js` _This path is mutable and may not actually exist._
2828
- **originalPath:** the original, unmutated path
2929
- **encodings:** A mutable, keyed object where the keys are a valid encoding type ('gzip', 'compress', 'br', etc.) and the values are the encoded content. Encoded content should be stored here and not resolved using `next(null, encodedContent)`
30-
- **type:** the pattern used to match the file
30+
- **type:** determines how to include a file, when serving
3131
- **`next`** function to be called when preprocessing is complete, should be called as `next(null, processedContent)` or `next(error)`
3232
- example plugins: [karma-coffee-preprocessor], [karma-ng-html2js-preprocessor]
3333
- use naming convention is `karma-*-preprocessor`

‎lib/file.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const path = require('path')
4+
35
/**
46
* File object used for tracking files in `file-list.js`.
57
*/
@@ -29,6 +31,14 @@ class File {
2931
this.isBinary = isBinary === undefined ? null : isBinary
3032
}
3133

34+
/**
35+
* Detect type from the file extension.
36+
* @returns {string} detected file type or empty string
37+
*/
38+
detectType () {
39+
return path.extname(this.path).substring(1)
40+
}
41+
3242
toString () {
3343
return this.path
3444
}

‎lib/middleware/karma.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
* - setting propert caching headers
1212
*/
1313

14-
const path = require('path')
1514
const url = require('url')
16-
const helper = require('../helper')
1715

1816
const log = require('../logger').create('middleware:karma')
1917
const stripHost = require('./strip_host').stripHost
@@ -164,10 +162,18 @@ function createKarmaMiddleware (
164162
const scriptTags = []
165163
for (const file of files.included) {
166164
let filePath = file.path
167-
const fileType = file.type || path.extname(filePath).substring(1)
165+
const fileType = file.type || file.detectType()
168166

169-
if (helper.isDefined(fileType) && !FILE_TYPES.includes(fileType)) {
170-
log.warn(`Invalid file type (${fileType}), defaulting to js.`)
167+
if (!FILE_TYPES.includes(fileType)) {
168+
if (file.type == null) {
169+
log.warn(
170+
`Unable to determine file type from the file extension, defaulting to js.\n` +
171+
` To silence the warning specify a valid type for ${file.originalPath} in the configuration file.\n` +
172+
` See http://karma-runner.github.io/latest/config/files.html`
173+
)
174+
} else {
175+
log.warn(`Invalid file type (${file.type || 'empty string'}), defaulting to js.`)
176+
}
171177
}
172178

173179
if (!file.isUrl) {

‎lib/url.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
'use strict'
22

3+
const path = require('path')
4+
const { URL } = require('url')
5+
36
/**
47
* Url object used for tracking files in `file-list.js`.
58
*/
69
class Url {
710
constructor (path, type) {
811
this.path = path
12+
this.originalPath = path
913
this.type = type
1014
this.isUrl = true
1115
}
1216

17+
/**
18+
* Detect type from the file extension in the path part of the URL.
19+
* @returns {string} detected file type or empty string
20+
*/
21+
detectType () {
22+
return path.extname(new URL(this.path).pathname).substring(1)
23+
}
24+
1325
toString () {
1426
return this.path
1527
}

‎test/e2e/helpful-logs.feature

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Feature: Helpful warning and errors
2+
In order to use Karma
3+
As a person who wants to write great tests
4+
I want to get messages which help me to fix problems
5+
6+
Scenario: Karma fails to determine a file type from the file extension
7+
Given a configuration with:
8+
"""
9+
files = [ 'modules/**/*.mjs' ];
10+
browsers = ['ChromeHeadlessNoSandbox'];
11+
frameworks = ['mocha', 'chai'];
12+
plugins = [
13+
'karma-mocha',
14+
'karma-chai',
15+
'karma-chrome-launcher'
16+
];
17+
"""
18+
When I start Karma
19+
Then the stdout matches RegExp:
20+
"""
21+
WARN \[middleware:karma\]: Unable to determine file type from the file extension, defaulting to js.
22+
To silence the warning specify a valid type for .+modules/minus.mjs in the configuration file.
23+
See http://karma-runner.github.io/latest/config/files.html
24+
"""

‎test/unit/file.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const File = require('../../lib/file')
2+
3+
describe('File', () => {
4+
describe('detectType', () => {
5+
it('should detect type from the file extension', () => {
6+
const file = new File('/path/to/file.js')
7+
expect(file.detectType()).to.equal('js')
8+
})
9+
10+
it('should return empty string if file does not have an extension', () => {
11+
const file = new File('/path/to/file-without-extension')
12+
expect(file.detectType()).to.equal('')
13+
})
14+
})
15+
})

‎test/unit/url.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Url = require('../../lib/url')
2+
3+
describe('Url', () => {
4+
describe('detectType', () => {
5+
it('should detect type from the file extension in the path of the URL', () => {
6+
const file = new Url('https://example.com/path/to/file.js')
7+
expect(file.detectType()).to.equal('js')
8+
})
9+
10+
it('should detect type for URL with query params', () => {
11+
const file = new Url('https://example.com/path/to/file.js?query=simple')
12+
expect(file.detectType()).to.equal('js')
13+
})
14+
15+
it('should detect type for URL with a fragment', () => {
16+
const file = new Url('https://example.com/path/to/file.js#fragment')
17+
expect(file.detectType()).to.equal('js')
18+
})
19+
20+
it('should return empty string if URL does not have path', () => {
21+
const file = new Url('https://example.com')
22+
expect(file.detectType()).to.equal('')
23+
})
24+
25+
it('should return empty string if path in the URL does not have an extension', () => {
26+
const file = new Url('https://example.com/path/to/file-without-extension')
27+
expect(file.detectType()).to.equal('')
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)
Please sign in to comment.