-
Notifications
You must be signed in to change notification settings - Fork 91
/
parse-args.js
213 lines (190 loc) · 7.77 KB
/
parse-args.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* global describe, it, beforeEach, afterEach */
const {
buildYargs,
hideInstrumenteeArgs,
hideInstrumenterArgs,
getConfigFileNames,
loadConfigFile
} = require('../lib/parse-args')
const { join, resolve } = require('path')
const { existsSync } = require('fs')
const { assert } = require('chai')
const chaiJestSnapshot = require('chai-jest-snapshot')
const {
testReadingConfigFile,
beforeTestReadingConfigFile,
afterTestReadingConfigFile
} = require('./parse-args-helper.js')
require('chai')
.use(chaiJestSnapshot)
.should()
describe('parse-args', () => {
describe('hideInstrumenteeArgs', () => {
it('hides arguments passed to instrumented app', () => {
process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help']
const instrumenterArgs = hideInstrumenteeArgs()
instrumenterArgs.should.eql(['--foo=99', 'my-app'])
})
it('test early exit from function if no arguments are passed', () => {
process.argv = []
const instrumenterArgs = hideInstrumenteeArgs()
instrumenterArgs.length.should.eql(0)
})
})
describe('hideInstrumenterArgs', () => {
it('hides arguments passed to c8 bin', () => {
process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help']
const argv = buildYargs().parse(hideInstrumenteeArgs())
const instrumenteeArgs = hideInstrumenterArgs(argv)
instrumenteeArgs.should.eql(['my-app', '--help'])
argv.tempDirectory.endsWith(join('coverage', 'tmp')).should.be.equal(true)
})
it('interprets first args after -- as Node.js execArgv', async () => {
const expected = [process.execPath, '--expose-gc', 'index.js']
process.argv = ['node', 'c8', '--', '--expose-gc', 'index.js']
const argv = buildYargs().parse(hideInstrumenteeArgs())
const munged = hideInstrumenterArgs(argv)
munged.should.deep.equal(expected)
})
})
describe('with NODE_V8_COVERAGE already set', () => {
it('should not override it', () => {
const NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE
process.env.NODE_V8_COVERAGE = './coverage/tmp_'
process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help']
const argv = buildYargs().parse(hideInstrumenteeArgs())
argv.tempDirectory.endsWith('/coverage/tmp_').should.be.equal(true)
process.env.NODE_V8_COVERAGE = NODE_V8_COVERAGE
})
})
describe('--config', () => {
it('c8 process should throw an error message if an invalid configuration file name is passed', function () {
const invalidConfig = './fixtures/config/.c8.config.py'
const loadInvalidConfigFile = function (file, callBack) {
try {
callBack(file)
assert.fail('Invalid configuration file loaded')
} catch (error) {
const expectErrorValue = `Error: Unsupported file type .py while reading file ${invalidConfig}`
String(error).should.eql(expectErrorValue)
}
}
loadInvalidConfigFile(invalidConfig, function (file) {
loadConfigFile(file)
})
})
it('config directory should contain all variations of the config file naming convention', () => {
let count = 0
const fileMessages = []
const configFileList = getConfigFileNames()
configFileList.forEach((file) => {
const fullPath = './test/fixtures/config/' + file
if (existsSync(fullPath)) {
count++
} else {
fileMessages.push(`Missing ${file} from ./test/fixtures/config directory`)
}
})
if (count === configFileList.length) {
assert.equal(count, configFileList.length)
} else {
const msg = fileMessages.join(' \n ')
assert.equal(fileMessages.length, 0, msg)
}
})
const filePath = './fixtures/config/'
describe('c8 variations of config file', () => {
describe('should be able to read config files with .json, .yml, .yaml, .js, .cjs extensions', () => {
beforeEach(beforeTestReadingConfigFile)
const fileNameLineNumberMap = {
'.c8rc.json': 101,
'.c8rc.yml': 69,
'.c8rc.yaml': 10,
'c8.config.js': 47,
'c8.config.cjs': 51,
'.c8rc.js': 22,
'.c8rc.cjs': 32,
'.c8.config.js': 47,
'.c8.config.cjs': 45
}
testReadingConfigFile(fileNameLineNumberMap, filePath)
afterEach(afterTestReadingConfigFile)
})
})
describe('nyc variations of config file', () => {
describe('should be able to read config files with .json, .yml, .yaml, .js, .cjs extensions', () => {
beforeEach(beforeTestReadingConfigFile)
const fileNameLineNumberMap = {
'.nycrc': 51,
'.nycrc.json': 96,
'.nycrc.yml': 99,
'.nycrc.yaml': 98,
'nyc.config.js': 95,
'nyc.config.cjs': 94,
'.nyc.config.js': 85,
'.nyc.config.cjs': 71
}
testReadingConfigFile(fileNameLineNumberMap, filePath)
afterEach(afterTestReadingConfigFile)
})
})
it('should resolve to .nycrc at cwd', () => {
const argv = buildYargs().parse(['node', 'c8', 'my-app'])
argv.lines.should.be.equal(95)
})
it('should use config file specified in --config', () => {
const argv = buildYargs().parse(['node', 'c8', '--config', require.resolve('./fixtures/config/.c8rc.json')])
argv.lines.should.be.equal(101)
argv.tempDirectory.should.be.equal('./foo')
argv.functions.should.be.equal(24)
})
it('should have -c as an alias', () => {
const argv = buildYargs().parse(['node', 'c8', '-c', require.resolve('./fixtures/config/.c8rc.json')])
argv.lines.should.be.equal(101)
argv.tempDirectory.should.be.equal('./foo')
argv.functions.should.be.equal(24)
})
it('should respect options on the command line over config file', () => {
const argv = buildYargs().parse(['node', 'c8', '--lines', '100', '--config', require.resolve('./fixtures/config/.c8rc.json')])
argv.lines.should.be.equal(100)
})
it('should allow config files to extend each other', () => {
const argv = buildYargs().parse(['node', 'c8', '--lines', '100', '--config', require.resolve('./fixtures/config/.c8rc-base.json')])
argv.branches.should.be.equal(55)
argv.lines.should.be.equal(100)
argv.functions.should.be.equal(24)
})
it('should allow relative path reports directories', () => {
const argsArray = ['node', 'c8', '--lines', '100', '--reports-dir', './coverage_']
const argv = buildYargs().parse(argsArray)
argv.reportsDir.should.be.equal('./coverage_')
})
it('should allow relative path temporary directories', () => {
const argsArray = ['node', 'c8', '--lines', '100', '--temp-directory', './coverage/tmp_']
const argv = buildYargs().parse(argsArray)
argv.tempDirectory.should.be.equal('./coverage/tmp_')
})
it('should allow absolute path reports directories', () => {
const tmpDir = resolve(process.cwd(), 'coverage_')
const argsArray = ['node', 'c8', '--lines', '100', '--reports-dir', tmpDir]
const argv = buildYargs().parse(argsArray)
argv.reportsDir.should.be.equal(tmpDir)
})
it('should allow absolute path temporary directories', () => {
const tmpDir = resolve(process.cwd(), './coverage/tmp_')
const argsArray = ['node', 'c8', '--lines', '100', '--temp-directory', tmpDir]
const argv = buildYargs().parse(argsArray)
argv.tempDirectory.should.be.equal(tmpDir)
})
})
describe('--merge-async', () => {
it('should default to false', () => {
const argv = buildYargs().parse(['node', 'c8'])
argv.mergeAsync.should.be.equal(false)
})
it('should set to true when flag exists', () => {
const argv = buildYargs().parse(['node', 'c8', '--merge-async'])
argv.mergeAsync.should.be.equal(true)
})
})
})