|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ArrayPrototypeJoin, |
| 5 | + ArrayPrototypeMap, |
| 6 | + ArrayPrototypeSlice, |
| 7 | + RegExp, |
| 8 | + SafeMap, |
| 9 | + SafeSet, |
| 10 | + StringPrototypeSplit, |
| 11 | + StringPrototypeReplace, |
| 12 | + Symbol, |
| 13 | +} = primordials; |
| 14 | + |
| 15 | +const { codes: { ERR_ASSERT_SNAPSHOT_NOT_SUPPORTED } } = require('internal/errors'); |
| 16 | +const AssertionError = require('internal/assert/assertion_error'); |
| 17 | +const { inspect } = require('internal/util/inspect'); |
| 18 | +const { getOptionValue } = require('internal/options'); |
| 19 | +const { validateString } = require('internal/validators'); |
| 20 | +const { once } = require('events'); |
| 21 | +const { createReadStream, createWriteStream } = require('fs'); |
| 22 | +const path = require('path'); |
| 23 | +const assert = require('assert'); |
| 24 | + |
| 25 | +const kUpdateSnapshot = getOptionValue('--update-assert-snapshot'); |
| 26 | +const kInitialSnapshot = Symbol('kInitialSnapshot'); |
| 27 | +const kDefaultDelimiter = '\n#*#*#*#*#*#*#*#*#*#*#*#\n'; |
| 28 | +const kDefaultDelimiterRegex = new RegExp(kDefaultDelimiter.replaceAll('*', '\\*').replaceAll('\n', '\r?\n'), 'g'); |
| 29 | +const kKeyDelimiter = /:\r?\n/g; |
| 30 | + |
| 31 | +function getSnapshotPath() { |
| 32 | + if (process.mainModule) { |
| 33 | + const { dir, name } = path.parse(process.mainModule.filename); |
| 34 | + return path.join(dir, `${name}.snapshot`); |
| 35 | + } |
| 36 | + if (!process.argv[1]) { |
| 37 | + throw new ERR_ASSERT_SNAPSHOT_NOT_SUPPORTED(); |
| 38 | + } |
| 39 | + const { dir, name } = path.parse(process.argv[1]); |
| 40 | + return path.join(dir, `${name}.snapshot`); |
| 41 | +} |
| 42 | + |
| 43 | +function getSource() { |
| 44 | + return createReadStream(getSnapshotPath(), { encoding: 'utf8' }); |
| 45 | +} |
| 46 | + |
| 47 | +let _target; |
| 48 | +function getTarget() { |
| 49 | + _target ??= createWriteStream(getSnapshotPath(), { encoding: 'utf8' }); |
| 50 | + return _target; |
| 51 | +} |
| 52 | + |
| 53 | +function serializeName(name) { |
| 54 | + validateString(name, 'name'); |
| 55 | + return StringPrototypeReplace(`${name}`, kKeyDelimiter, '_'); |
| 56 | +} |
| 57 | + |
| 58 | +let writtenNames; |
| 59 | +let snapshotValue; |
| 60 | +let counter = 0; |
| 61 | + |
| 62 | +async function writeSnapshot({ name, value }) { |
| 63 | + const target = getTarget(); |
| 64 | + if (counter > 1) { |
| 65 | + target.write(kDefaultDelimiter); |
| 66 | + } |
| 67 | + writtenNames = writtenNames || new SafeSet(); |
| 68 | + if (writtenNames.has(name)) { |
| 69 | + throw new AssertionError({ message: `Snapshot "${name}" already used` }); |
| 70 | + } |
| 71 | + writtenNames.add(name); |
| 72 | + const drained = target.write(`${name}:\n${value}`); |
| 73 | + await drained || once(target, 'drain'); |
| 74 | +} |
| 75 | + |
| 76 | +async function getSnapshot() { |
| 77 | + if (snapshotValue !== undefined) { |
| 78 | + return snapshotValue; |
| 79 | + } |
| 80 | + if (kUpdateSnapshot) { |
| 81 | + snapshotValue = kInitialSnapshot; |
| 82 | + return kInitialSnapshot; |
| 83 | + } |
| 84 | + try { |
| 85 | + const source = getSource(); |
| 86 | + let data = ''; |
| 87 | + for await (const line of source) { |
| 88 | + data += line; |
| 89 | + } |
| 90 | + snapshotValue = new SafeMap( |
| 91 | + ArrayPrototypeMap( |
| 92 | + StringPrototypeSplit(data, kDefaultDelimiterRegex), |
| 93 | + (item) => { |
| 94 | + const arr = StringPrototypeSplit(item, kKeyDelimiter); |
| 95 | + return [ |
| 96 | + arr[0], |
| 97 | + ArrayPrototypeJoin(ArrayPrototypeSlice(arr, 1), ':\n'), |
| 98 | + ]; |
| 99 | + } |
| 100 | + )); |
| 101 | + } catch (e) { |
| 102 | + if (e.code === 'ENOENT') { |
| 103 | + snapshotValue = kInitialSnapshot; |
| 104 | + } else { |
| 105 | + throw e; |
| 106 | + } |
| 107 | + } |
| 108 | + return snapshotValue; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +async function snapshot(input, name) { |
| 113 | + const snapshot = await getSnapshot(); |
| 114 | + counter = counter + 1; |
| 115 | + name = serializeName(name); |
| 116 | + |
| 117 | + const value = inspect(input); |
| 118 | + if (snapshot === kInitialSnapshot) { |
| 119 | + await writeSnapshot({ name, value }); |
| 120 | + } else if (snapshot.has(name)) { |
| 121 | + const expected = snapshot.get(name); |
| 122 | + // eslint-disable-next-line no-restricted-syntax |
| 123 | + assert.strictEqual(value, expected); |
| 124 | + } else { |
| 125 | + throw new AssertionError({ message: `Snapshot "${name}" does not exist`, actual: inspect(snapshot) }); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +module.exports = snapshot; |
0 commit comments