• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const { relative, dirname } = require('path')
2
3// normalize line endings (for ini)
4const cleanNewlines = (s) => s.replace(/\r\n/g, '\n')
5
6// XXX: this also cleans quoted " in json snapshots
7// ideally this could be avoided but its easier to just
8// run this command inside cleanSnapshot
9const normalizePath = (str) => cleanNewlines(str)
10  .replace(/[A-z]:\\/g, '\\') // turn windows roots to posix ones
11  .replace(/\\+/g, '/') // replace \ with /
12
13const pathRegex = (p) => new RegExp(normalizePath(p), 'gi')
14
15// create a cwd replacer in the module scope, since some tests
16// overwrite process.cwd()
17const CWD = pathRegex(process.cwd())
18const TESTDIR = pathRegex(relative(process.cwd(), dirname(require.main.filename)))
19
20const cleanCwd = (path) => normalizePath(path)
21  // repalce CWD, TESTDIR, and TAPDIR separately
22  .replace(CWD, '{CWD}')
23  .replace(TESTDIR, '{TESTDIR}')
24  .replace(/tap-testdir-[\w-.]+/gi, '{TAPDIR}')
25  // if everything ended up in line, reduce it all to CWD
26  .replace(/\{CWD\}\/\{TESTDIR\}\/\{TAPDIR\}/g, '{CWD}')
27  // replace for platform differences in global nodemodules
28  .replace(/lib\/node_modules/g, 'node_modules')
29  .replace(/global\/lib/g, 'global')
30
31const cleanDate = (str) =>
32  str.replace(/\d{4}-\d{2}-\d{2}T\d{2}[_:]\d{2}[_:]\d{2}[_:.]\d{3}Z/g, '{DATE}')
33
34const cleanTime = str => str.replace(/in [0-9]+m?s\s*$/gm, 'in {TIME}')
35
36const cleanZlib = str => str
37  .replace(/shasum:( *)[0-9a-f]{40}/g, 'shasum:$1{sha}')
38  .replace(/integrity:( *).*/g, 'integrity:$1{integrity}')
39  .replace(/package size:( *)[0-9 A-Z]*/g, 'package size:$1{size}')
40
41  .replace(/"shasum": "[0-9a-f]{40}",/g, '"shasum": "{sha}",')
42  .replace(/"integrity": ".*",/g, '"integrity": "{integrity}",')
43  .replace(/"size": [0-9]*,/g, '"size": "{size}",')
44
45module.exports = {
46  cleanCwd,
47  cleanDate,
48  cleanNewlines,
49  cleanTime,
50  cleanZlib,
51  normalizePath,
52  pathRegex,
53}
54