| 1 |
'use strict' |
| 2 |
|
| 3 |
const fs = require('../lib/fs-promises') |
| 4 |
const path = require('path') |
| 5 |
const { promisify } = require('util') |
| 6 |
|
| 7 |
const t = require('tap') |
| 8 |
const makeDir = require('make-dir') |
| 9 |
const isWindows = require('is-windows')() |
| •10 |
const { rimraf } = require('rimraf') |
| 11 |
|
| 12 |
const { runNYC, testSuccess, fixturesCLI } = require('./helpers') |
| 13 |
|
| 14 |
const subdir = path.resolve(fixturesCLI, 'subdir') |
| 15 |
const outputDir = path.resolve(subdir, './output-dir') |
| 16 |
const removedByClean = path.resolve(outputDir, 'removed-by-clean') |
| 17 |
|
| 18 |
function cleanup () { |
| 19 |
return Promise.all([ |
| 20 |
rimraf(path.resolve(subdir, 'output-dir')), |
| 21 |
rimraf(path.resolve(fixturesCLI, 'output-dir')) |
| 22 |
]) |
| 23 |
} |
| 24 |
|
| 25 |
t.test('clean before', cleanup) |
| 26 |
t.afterEach(cleanup) |
| 27 |
|
| 28 |
t.test('works in directories without a package.json', async t => { |
| 29 |
const { status } = await runNYC({ |
| 30 |
args: ['instrument', './input-dir', './output-dir'], |
| 31 |
cwd: subdir |
| 32 |
}) |
| 33 |
|
| 34 |
t.strictEqual(status, 0) |
| 35 |
const target = path.resolve(subdir, 'output-dir', 'index.js') |
| 36 |
t.match(await fs.readFile(target, 'utf8'), /console.log\('Hello, World!'\)/) |
| 37 |
}) |
| 38 |
|
| 39 |
t.test('can be configured to exit on error', async t => { |
| 40 |
const { status } = await runNYC({ |
| 41 |
args: ['instrument', '--exit-on-error', './input-dir', './output-dir'], |
| 42 |
cwd: subdir |
| 43 |
}) |
| 44 |
|
| 45 |
t.strictEqual(status, 1) |
| 46 |
}) |
| 47 |
|
| 48 |
t.test('allows a single file to be instrumented', async t => { |
| 49 |
const inputPath = path.resolve(fixturesCLI, './half-covered.js') |
| 50 |
const inputMode = (await fs.stat(inputPath)).mode & 0o7777 |
| 51 |
const newMode = 0o775 |
| 52 |
if (!isWindows) { |
| 53 |
await fs.chmod(inputPath, newMode) |
| 54 |
} |
| 55 |
|
| 56 |
const { status } = await runNYC({ |
| 57 |
args: ['instrument', './half-covered.js', outputDir] |
| 58 |
}) |
| 59 |
|
| 60 |
t.strictEqual(status, 0) |
| 61 |
|
| 62 |
const files = await fs.readdir(outputDir) |
| 63 |
t.strictSame(files, ['half-covered.js']) |
| 64 |
|
| 65 |
if (!isWindows) { |
| 66 |
const outputPath = path.resolve(outputDir, 'half-covered.js') |
| 67 |
const outputMode = (await fs.stat(outputPath)).mode & 0o7777 |
| 68 |
t.strictEqual(outputMode, newMode) |
| 69 |
|
| 70 |
await fs.chmod(inputPath, inputMode) |
| 71 |
} |
| 72 |
}) |
| 73 |
|
| 74 |
t.test('allows a directory of files to be instrumented', async t => { |
| 75 |
const { status } = await runNYC({ |
| 76 |
args: ['instrument', './nyc-config-js', outputDir] |
| 77 |
}) |
| 78 |
|
| 79 |
t.strictEqual(status, 0) |
| 80 |
|
| 81 |
const files = fs.readdirSync(outputDir) |
| 82 |
t.strictEqual(files.includes('index.js'), true) |
| 83 |
t.strictEqual(files.includes('ignore.js'), true) |
| 84 |
t.strictEqual(files.includes('package.json'), false) |
| 85 |
t.strictEqual(files.includes('node_modules'), false) |
| 86 |
|
| 87 |
const includeTarget = path.resolve(outputDir, 'ignore.js') |
| 88 |
t.match(await fs.readFile(includeTarget, 'utf8'), /function cov_/) |
| 89 |
}) |
| 90 |
|
| 91 |
t.test('copies all files from <input> to <output> as well as those that have been instrumented', async t => { |
| 92 |
// force node_modules to exist so we can verify that it is copied. |
| 93 |
const nmDir = path.resolve(fixturesCLI, 'nyc-config-js', 'node_modules') |
| 94 |
await makeDir(nmDir) |
| 95 |
await fs.writeFile(path.join(nmDir, 'test-file'), '') |
| 96 |
|
| 97 |
const { status } = await runNYC({ |
| 98 |
args: ['instrument', '--complete-copy', './nyc-config-js', outputDir] |
| 99 |
}) |
| 100 |
|
| 101 |
t.strictEqual(status, 0) |
| 102 |
|
| 103 |
const files = await fs.readdir(outputDir) |
| 104 |
t.strictEqual(files.includes('index.js'), true) |
| 105 |
t.strictEqual(files.includes('ignore.js'), true) |
| 106 |
t.strictEqual(files.includes('package.json'), true) |
| 107 |
t.strictEqual(files.includes('node_modules'), true) |
| 108 |
|
| 109 |
const includeTarget = path.resolve(outputDir, 'ignore.js') |
| 110 |
t.match(await fs.readFile(includeTarget, 'utf8'), /function cov_/) |
| 111 |
}) |
| 112 |
|
| 113 |
t.test('can instrument the project directory', async t => { |
| 114 |
const { status } = await runNYC({ |
| 115 |
args: ['instrument', '.', outputDir] |
| 116 |
}) |
| 117 |
|
| 118 |
t.strictEqual(status, 0) |
| 119 |
|
| 120 |
const files = await fs.readdir(outputDir) |
| 121 |
t.strictEqual(files.includes('args.js'), true) |
| 122 |
t.strictEqual(files.includes('subdir'), true) |
| 123 |
}) |
| 124 |
|
| 125 |
t.test('allows a sub-directory of files to be instrumented', async t => { |
| 126 |
const { status } = await runNYC({ |
| 127 |
args: ['instrument', './subdir/input-dir', outputDir] |
| 128 |
}) |
| 129 |
|
| 130 |
t.strictEqual(status, 0) |
| 131 |
|
| 132 |
const files = await fs.readdir(outputDir) |
| 133 |
t.strictEqual(files.includes('index.js'), true) |
| 134 |
}) |
| 135 |
|
| 136 |
t.test('allows a subdirectory to be excluded via .nycrc file', async t => { |
| 137 |
const { status } = await runNYC({ |
| 138 |
args: [ |
| 139 |
'instrument', |
| 140 |
'--nycrc-path', |
| 141 |
'./.instrument-nycrc', |
| 142 |
'./subdir/input-dir', |
| 143 |
outputDir |
| 144 |
] |
| 145 |
}) |
| 146 |
|
| 147 |
t.strictEqual(status, 0) |
| 148 |
|
| 149 |
const files = fs.readdirSync(outputDir) |
| 150 |
t.strictEqual(files.includes('exclude-me'), true) |
| 151 |
t.strictEqual(files.includes('node_modules'), true) |
| 152 |
t.strictEqual(files.includes('index.js'), true) |
| 153 |
t.strictEqual(files.includes('bad.js'), true) |
| 154 |
|
| 155 |
const includeTarget = path.resolve(outputDir, 'index.js') |
| 156 |
t.match(await fs.readFile(includeTarget, 'utf8'), /function cov_/) |
| 157 |
|
| 158 |
const excludeTarget = path.resolve(outputDir, 'exclude-me', 'index.js') |
| 159 |
t.notMatch(await fs.readFile(excludeTarget, 'utf8'), /function cov_/) |
| 160 |
}) |
| 161 |
|
| 162 |
t.test('allows a file to be excluded', async t => { |
| 163 |
const { status } = await runNYC({ |
| 164 |
args: [ |
| 165 |
'instrument', |
| 166 |
'--complete-copy', |
| 167 |
'--exclude', |
| 168 |
'exclude-me/index.js', |
| 169 |
'./subdir/input-dir', |
| 170 |
outputDir |
| 171 |
] |
| 172 |
}) |
| 173 |
|
| 174 |
t.strictEqual(status, 0) |
| 175 |
|
| 176 |
const files = await fs.readdir(outputDir) |
| 177 |
t.strictEqual(files.includes('exclude-me'), true) |
| 178 |
|
| 179 |
const excludeTarget = path.resolve(outputDir, 'exclude-me', 'index.js') |
| 180 |
t.notMatch(await fs.readFile(excludeTarget, 'utf8'), /function cov_/) |
| 181 |
}) |
| 182 |
|
| 183 |
t.test('allows specifying a single sub-directory to be included', async t => { |
| 184 |
const { status } = await runNYC({ |
| 185 |
args: [ |
| 186 |
'instrument', |
| 187 |
'--include', |
| 188 |
'**/include-me/**', |
| 189 |
'./subdir/input-dir', |
| 190 |
outputDir |
| 191 |
] |
| 192 |
}) |
| 193 |
|
| 194 |
t.strictEqual(status, 0) |
| 195 |
|
| 196 |
const files = await fs.readdir(outputDir) |
| 197 |
t.strictEqual(files.includes('include-me'), true) |
| 198 |
const instrumented = path.resolve(outputDir, 'include-me', 'include-me.js') |
| 199 |
t.match(await fs.readFile(instrumented, 'utf8'), /function cov_/) |
| 200 |
}) |
| 201 |
|
| 202 |
t.test('allows a file to be excluded from an included directory', async t => { |
| 203 |
const { status } = await runNYC({ |
| 204 |
args: [ |
| 205 |
'instrument', |
| 206 |
'--complete-copy', |
| 207 |
'--exclude', |
| 208 |
'**/exclude-me.js', |
| 209 |
'--include', |
| 210 |
'**/include-me/**', |
| 211 |
'./subdir/input-dir', |
| 212 |
outputDir |
| 213 |
] |
| 214 |
}) |
| 215 |
|
| 216 |
t.strictEqual(status, 0) |
| 217 |
|
| 218 |
const files = await fs.readdir(outputDir) |
| 219 |
t.strictEqual(files.includes('include-me'), true) |
| 220 |
|
| 221 |
const includeMeFiles = await fs.readdir(path.resolve(outputDir, 'include-me')) |
| 222 |
t.strictEqual(includeMeFiles.includes('include-me.js'), true) |
| 223 |
t.strictEqual(includeMeFiles.includes('exclude-me.js'), true) |
| 224 |
|
| 225 |
const includeTarget = path.resolve(outputDir, 'include-me', 'include-me.js') |
| 226 |
t.match(await fs.readFile(includeTarget, 'utf8'), /function cov_/) |
| 227 |
|
| 228 |
const excludeTarget = path.resolve(outputDir, 'exclude-me', 'index.js') |
| 229 |
t.notMatch(await fs.readFile(excludeTarget, 'utf8'), /function cov_/) |
| 230 |
}) |
| 231 |
|
| 232 |
t.test('aborts if trying to write files in place', async t => { |
| 233 |
const { status, stderr } = await runNYC({ |
| 234 |
args: ['instrument', './', './'] |
| 235 |
}) |
| 236 |
|
| 237 |
t.strictEqual(status, 1) |
| 238 |
t.match(stderr, /cannot instrument files in place/) |
| 239 |
}) |
| 240 |
|
| 241 |
t.test('can write files in place with --in-place switch', async t => { |
| 242 |
const sourceDir = path.resolve(fixturesCLI, 'instrument-inplace') |
| 243 |
await makeDir(outputDir) |
| 244 |
await Promise.all(['package.json', 'file1.js', 'file2.js'].map( |
| 245 |
file => fs.copyFile(path.join(sourceDir, file), path.join(outputDir, file)) |
| 246 |
)) |
| 247 |
|
| 248 |
const { status } = await runNYC({ |
| 249 |
args: [ |
| 250 |
'instrument', |
| 251 |
'--in-place', |
| 252 |
'--include', |
| 253 |
'file1.js', |
| 254 |
'.' |
| 255 |
], |
| 256 |
cwd: outputDir |
| 257 |
}) |
| 258 |
|
| 259 |
t.strictEqual(status, 0) |
| 260 |
|
| 261 |
const file1 = path.resolve(outputDir, 'file1.js') |
| 262 |
t.match(await fs.readFile(file1, 'utf8'), /function cov_/) |
| 263 |
|
| 264 |
const file2 = path.resolve(outputDir, 'file2.js') |
| 265 |
t.notMatch(await fs.readFile(file2, 'utf8'), /function cov_/) |
| 266 |
|
| 267 |
await testSuccess(t, { |
| 268 |
args: ['--all', process.execPath, '-e', ''], |
| 269 |
cwd: outputDir |
| 270 |
}) |
| 271 |
}) |
| 272 |
|
| 273 |
t.test('aborts if trying to delete while writing files in place', async t => { |
| 274 |
const { status, stderr } = await runNYC({ |
| 275 |
args: [ |
| 276 |
'instrument', |
| 277 |
'--in-place', |
| 278 |
'--delete', |
| 279 |
'--include', |
| 280 |
'file1.js', |
| 281 |
'./instrument-inplace' |
| 282 |
] |
| 283 |
}) |
| 284 |
|
| 285 |
t.strictEqual(status, 1) |
| 286 |
t.match(stderr, /cannot use '--delete' when instrumenting files in place/) |
| 287 |
}) |
| 288 |
|
| 289 |
t.test('aborts if trying to instrument files from outside the project root directory', async t => { |
| 290 |
const { status, stderr } = await runNYC({ |
| 291 |
args: [ |
| 292 |
'instrument', |
| 293 |
'--delete', |
| 294 |
'../', |
| 295 |
'./' |
| 296 |
] |
| 297 |
}) |
| 298 |
|
| 299 |
t.strictEqual(status, 1) |
| 300 |
t.match(stderr, /cannot instrument files outside project root directory/) |
| 301 |
}) |
| 302 |
|
| 303 |
t.test('cleans the output directory if `--delete` is specified', async t => { |
| 304 |
await makeDir(removedByClean) |
| 305 |
const { status } = await runNYC({ |
| 306 |
args: [ |
| 307 |
'instrument', |
| 308 |
'--delete', |
| 309 |
'true', |
| 310 |
'./subdir/input-dir', |
| 311 |
outputDir |
| 312 |
] |
| 313 |
}) |
| 314 |
|
| 315 |
t.strictEqual(status, 0) |
| 316 |
|
| 317 |
const files = await fs.readdir(outputDir) |
| 318 |
t.strictEqual(files.includes('removed-by-clean'), false) |
| 319 |
t.strictEqual(files.includes('exclude-me'), true) |
| 320 |
}) |
| 321 |
|
| 322 |
t.test('does not clean the output directory by default', async t => { |
| 323 |
await makeDir(removedByClean) |
| 324 |
|
| 325 |
const { status } = await runNYC({ |
| 326 |
args: [ |
| 327 |
'instrument', |
| 328 |
'./subdir/input-dir', |
| 329 |
outputDir |
| 330 |
] |
| 331 |
}) |
| 332 |
|
| 333 |
t.strictEqual(status, 0) |
| 334 |
|
| 335 |
const files = await fs.readdir(outputDir) |
| 336 |
t.strictEqual(files.includes('removed-by-clean'), true) |
| 337 |
}) |
| 338 |
|
| 339 |
t.test('aborts if trying to clean process.cwd()', async t => { |
| 340 |
const { status, stderr } = await runNYC({ |
| 341 |
args: ['instrument', '--delete', './src', './'] |
| 342 |
}) |
| 343 |
|
| 344 |
t.strictEqual(status, 1) |
| 345 |
t.match(stderr, /attempt to delete/) |
| 346 |
}) |
| 347 |
|
| 348 |
t.test('aborts if trying to clean outside working directory', async t => { |
| 349 |
const { status, stderr } = await runNYC({ |
| 350 |
args: ['instrument', '--delete', './', '../'] |
| 351 |
}) |
| 352 |
|
| 353 |
t.strictEqual(status, 1) |
| 354 |
t.match(stderr, /attempt to delete/) |
| 355 |
}) |