|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const { execFileSync } = require('node:child_process') |
| 5 | +const fs = require('node:fs') |
| 6 | +const os = require('node:os') |
| 7 | +const path = require('node:path') |
| 8 | + |
| 9 | +const repoRoot = path.resolve(__dirname, '..', '..') |
| 10 | +const relativeTarget = path.join('lib', 'file-coverage.js') |
| 11 | +const rootPatchScript = path.join(repoRoot, 'scripts', 'patch-istanbul-lib-coverage.js') |
| 12 | +const rootInstalledTarget = path.join(repoRoot, 'node_modules', 'istanbul-lib-coverage', relativeTarget) |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {string} fixtureRoot |
| 16 | + */ |
| 17 | +function createSourceFixture (fixtureRoot) { |
| 18 | + fs.mkdirSync(path.join(fixtureRoot, 'scripts'), { recursive: true }) |
| 19 | + fs.mkdirSync(path.join(fixtureRoot, 'packages', 'datadog-instrumentations'), { recursive: true }) |
| 20 | + fs.mkdirSync(path.join(fixtureRoot, 'integration-tests', 'coverage'), { recursive: true }) |
| 21 | + fs.writeFileSync(path.join(fixtureRoot, 'eslint.config.mjs'), '') |
| 22 | + fs.writeFileSync(path.join(fixtureRoot, 'integration-tests', 'coverage', 'merge-lcov.js'), '') |
| 23 | + fs.copyFileSync( |
| 24 | + rootPatchScript, |
| 25 | + path.join(fixtureRoot, 'scripts', 'patch-istanbul-lib-coverage.js') |
| 26 | + ) |
| 27 | + fs.copyFileSync( |
| 28 | + path.join(repoRoot, 'scripts', 'replace-file.js'), |
| 29 | + path.join(fixtureRoot, 'scripts', 'replace-file.js') |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +describe('patch-istanbul-lib-coverage', function () { |
| 34 | + this.timeout(60_000) |
| 35 | + |
| 36 | + let fixtureDirectory |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + fixtureDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-istanbul-patch-')) |
| 40 | + createSourceFixture(fixtureDirectory) |
| 41 | + fs.writeFileSync(path.join(fixtureDirectory, 'package.json'), JSON.stringify({ |
| 42 | + dependencies: { |
| 43 | + 'istanbul-lib-coverage': '3.2.2', |
| 44 | + }, |
| 45 | + })) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + fs.rmSync(fixtureDirectory, { recursive: true, force: true }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('does not mutate Bun hardlink cache entries', async () => { |
| 53 | + const cachedTarget = path.join(fixtureDirectory, 'istanbul-lib-coverage-cache.js') |
| 54 | + const backupTarget = `${rootInstalledTarget}.backup-${process.pid}` |
| 55 | + fs.mkdirSync(path.dirname(cachedTarget), { recursive: true }) |
| 56 | + const sourceTarget = path.join( |
| 57 | + repoRoot, 'vendor', 'node_modules', 'istanbul-lib-coverage', relativeTarget |
| 58 | + ) |
| 59 | + fs.copyFileSync(sourceTarget, cachedTarget) |
| 60 | + fs.renameSync(rootInstalledTarget, backupTarget) |
| 61 | + fs.linkSync(cachedTarget, rootInstalledTarget) |
| 62 | + const originalCacheSource = fs.readFileSync(cachedTarget, 'utf8') |
| 63 | + |
| 64 | + try { |
| 65 | + delete require.cache[rootPatchScript] |
| 66 | + require(rootPatchScript) |
| 67 | + |
| 68 | + assert.match(fs.readFileSync(rootInstalledTarget, 'utf8'), /dd-trace-js patch v2/) |
| 69 | + assert.strictEqual(await Promise.resolve(fs.readFileSync(cachedTarget, 'utf8')), originalCacheSource) |
| 70 | + } finally { |
| 71 | + fs.rmSync(rootInstalledTarget, { force: true }) |
| 72 | + fs.renameSync(backupTarget, rootInstalledTarget) |
| 73 | + delete require.cache[rootPatchScript] |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + it('skips when the package is not installed locally', () => { |
| 78 | + const backupTarget = `${rootInstalledTarget}.backup-${process.pid}` |
| 79 | + fs.renameSync(rootInstalledTarget, backupTarget) |
| 80 | + |
| 81 | + try { |
| 82 | + delete require.cache[rootPatchScript] |
| 83 | + require(rootPatchScript) |
| 84 | + assert.strictEqual(fs.existsSync(rootInstalledTarget), false) |
| 85 | + } finally { |
| 86 | + fs.renameSync(backupTarget, rootInstalledTarget) |
| 87 | + delete require.cache[rootPatchScript] |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + it('does not patch dependencies from the parent application', async () => { |
| 92 | + const packageRoot = path.join(fixtureDirectory, 'node_modules', 'dd-trace') |
| 93 | + createSourceFixture(packageRoot) |
| 94 | + |
| 95 | + const parentTarget = path.join(fixtureDirectory, 'node_modules', 'istanbul-lib-coverage', relativeTarget) |
| 96 | + fs.mkdirSync(path.dirname(parentTarget), { recursive: true }) |
| 97 | + const sourceTarget = path.join( |
| 98 | + repoRoot, 'vendor', 'node_modules', 'istanbul-lib-coverage', relativeTarget |
| 99 | + ) |
| 100 | + fs.copyFileSync(sourceTarget, parentTarget) |
| 101 | + const originalParentSource = fs.readFileSync(parentTarget, 'utf8') |
| 102 | + |
| 103 | + execFileSync(process.execPath, ['scripts/patch-istanbul-lib-coverage.js'], { |
| 104 | + cwd: packageRoot, |
| 105 | + }) |
| 106 | + |
| 107 | + assert.strictEqual( |
| 108 | + await Promise.resolve(fs.readFileSync(parentTarget, 'utf8')), |
| 109 | + originalParentSource |
| 110 | + ) |
| 111 | + }) |
| 112 | +}) |
0 commit comments