From 7af36bb9f8e5c9facaa8deb114b76368841fbc66 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 26 Aug 2021 10:58:44 -0700 Subject: [PATCH] tests(config): fix snapshot cleaner Order matters here so we clean the more specific things before the generic ones. PR-URL: https://github.com/npm/cli/pull/3687 Credit: @wraithgar Close: #3687 Reviewed-by: @nlf --- test/fixtures/sandbox.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/test/fixtures/sandbox.js b/test/fixtures/sandbox.js index 45af373d2753a..4cdd9b70dbc6f 100644 --- a/test/fixtures/sandbox.js +++ b/test/fixtures/sandbox.js @@ -136,21 +136,6 @@ class Sandbox extends EventEmitter { cleanSnapshot (snapshot) { let clean = normalize(snapshot) - if (this[_npm]) { - // replace default config values with placeholders - for (const name of redactedDefaults) { - let value = this[_npm].config.defaults[name] - clean = clean.split(value).join(`{${name.toUpperCase()}}`) - } - - // replace vague default config values that are present within quotes - // with placeholders - for (const name of vagueRedactedDefaults) { - const value = this[_npm].config.defaults[name] - clean = clean.split(`"${value}"`).join(`"{${name.toUpperCase()}}"`) - } - } - const viewer = _process.platform === 'win32' ? /"browser"([^:]+|$)/g : /"man"([^:]+|$)/g @@ -180,6 +165,26 @@ class Sandbox extends EventEmitter { .split(this[_proxy].platform).join('{PLATFORM}') .split(this[_proxy].arch).join('{ARCH}') + // We do the defaults after everything else so that they don't cause the + // other cleaners to miss values we would have clobbered here. For + // instance if execPath is /home/user/.nvm/versions/node/1.0.0/bin/node, + // and we replaced the node version first, the real execPath we're trying + // to replace would no longer be represented, and be missed. + if (this[_npm]) { + // replace default config values with placeholders + for (const name of redactedDefaults) { + let value = this[_npm].config.defaults[name] + clean = clean.split(value).join(`{${name.toUpperCase()}}`) + } + + // replace vague default config values that are present within quotes + // with placeholders + for (const name of vagueRedactedDefaults) { + const value = this[_npm].config.defaults[name] + clean = clean.split(`"${value}"`).join(`"{${name.toUpperCase()}}"`) + } + } + return clean }