-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathstate.spec.js
159 lines (119 loc) · 5.9 KB
/
state.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
require('../../setup/mocha')
describe('findScriptFromPartialPath', function () {
let state
const cases = [
['file:///path/to/foo.js', 'script-id-posix'],
['file:///C:/path/to/bar.js', 'script-id-win-slash'],
// We have no evidence that Chrome DevTools Protocol uses backslashes in paths, but test in case it changes
['file:///C:\\path\\to\\baz.js', 'script-id-win-backslash']
]
before(function () {
state = proxyquire('../src/debugger/devtools_client/state', {
'./session': {
'@noCallThru': true,
on (event, listener) {
if (event === 'Debugger.scriptParsed') {
cases.forEach(([url, scriptId]) => {
listener({ params: { scriptId, url } })
})
// Test case for when there's multiple partial matches
listener({ params: { scriptId: 'should-match', url: 'file:///server/index.js' } })
listener({ params: { scriptId: 'should-not-match', url: 'file:///index.js' } })
// Test case for when there's two equal length partial matches
listener({ params: { scriptId: 'should-not-match-longest-a', url: 'file:///node_modules/foo/index.js' } })
listener({ params: { scriptId: 'should-match-shortest-a', url: 'file:///foo/index.js' } })
// The same, but in reverse order to ensure this doesn't influence the result
listener({ params: { scriptId: 'should-match-shortest-b', url: 'file:///bar/index.js' } })
listener({ params: { scriptId: 'should-not-match-longest-b', url: 'file:///node_modules/bar/index.js' } })
}
}
}
})
})
for (const [url, scriptId] of cases) {
const filename = url.includes('\\') ? url.split('\\').pop() : url.split('/').pop()
describe(`targeting ${url}`, function () {
describe('POSIX paths', function () {
describe('full path matches', function () {
it('with a "file:" protocol', testPath(`file:///path/to/${filename}`))
it('with a root slash', testPath(`/path/to/${filename}`))
it('without a root slash', testPath(`path/to/${filename}`))
})
describe('partial path matches', function () {
it('fewer directories', testPath(`to/${filename}`))
it('no directories', testPath(filename))
})
describe('path contains directory prefix', function () {
it('prefixed with unknown directory', testPath(`prefix/to/${filename}`))
it('prefixed with two unknown directories', testPath(`prefix1/prefix2/to/${filename}`))
})
describe('non-matching paths', function () {
it('should not match if only part of a directory matches (at boundary)',
testPathNoMatch(`path/o/${filename}`))
it('should not match if only part of a directory matches (not at boundary)',
testPathNoMatch(`path/no/${filename}`))
it('should not match if only part of a directory matches (root)', testPathNoMatch(`o/${filename}`))
it('should not match if only part of a file matches', testPathNoMatch(filename.slice(1)))
it('should not match if only difference is the letter casing', testPathNoMatch(filename.toUpperCase()))
})
})
describe('Windows paths', function () {
describe('with backslashes', function () {
describe('full path matches', function () {
it('with a "file:" protocol', testPath(`file:///C|\\path\\to\\${filename}`))
it('with a drive letter', testPath(`C:\\path\\to\\${filename}`))
it('without a drive slash', testPath(`C:path\\to\\${filename}`))
})
describe('partial path matches', function () {
it('fewer directories', testPath(`to\\${filename}`))
})
describe('path contains directory prefix', function () {
it('prefixed with unknown directory', testPath(`prefix\\to\\${filename}`))
it('prefixed with two unknown directories', testPath(`prefix1\\prefix2\\to\\${filename}`))
})
})
describe('with forward slashes', function () {
describe('full path matches', function () {
it('with a "file:" protocol', testPath(`file:///C|/path/to/${filename}`))
it('with a drive letter', testPath(`C:/path/to/${filename}`))
it('without a drive slash', testPath(`C:path/to/${filename}`))
})
})
})
})
function testPath (path) {
return function () {
const result = state.findScriptFromPartialPath(path)
expect(result).to.deep.equal([url, scriptId, undefined])
}
}
}
describe('multiple partial matches', function () {
it('should match the longest partial match', function () {
const result = state.findScriptFromPartialPath('server/index.js')
expect(result).to.deep.equal(['file:///server/index.js', 'should-match', undefined])
})
it('should match the shorter of two equal length partial matches', function () {
const result1 = state.findScriptFromPartialPath('foo/index.js')
expect(result1).to.deep.equal(['file:///foo/index.js', 'should-match-shortest-a', undefined])
const result2 = state.findScriptFromPartialPath('bar/index.js')
expect(result2).to.deep.equal(['file:///bar/index.js', 'should-match-shortest-b', undefined])
})
})
describe('should abort if the path is', function () {
it('unknown', testPathNoMatch('this/path/does/not/exist.js'))
it('undefined', testPathNoMatch(undefined))
it('an empty string', testPathNoMatch(''))
it('a slash', testPathNoMatch('/'))
it('a backslash', testPathNoMatch('\\'))
it('a Windows drive letter', testPathNoMatch('c:'))
it('a Windows drive letter with a backslash', testPathNoMatch('c:\\'))
})
function testPathNoMatch (path) {
return function () {
const result = state.findScriptFromPartialPath(path)
expect(result).to.be.null
}
}
})