Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean process.cwd() out of snapshots by default
Also, make the test-built package.json a bit closer to it's eventual state, just to avoid cases where the build fails, and it leaves it in a weird state. Fix: #885
- Loading branch information
Showing
15 changed files
with
269 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { cwd } from '@tapjs/core' | ||
|
||
// escape a string to json, then unwrap the " | ||
// used for cwd detection | ||
const esc = (s: string) => { | ||
const j = JSON.stringify(s) | ||
return j.substring(1, j.length - 1) | ||
} | ||
const cwdPosix = cwd.replace(/\\/g, '/') | ||
const cwdLcase = cwd.toLowerCase() | ||
const cwdPosixLcase = cwdPosix.toLowerCase() | ||
const cwds = new Set([cwd, cwdPosix, cwdLcase, cwdPosixLcase]) | ||
for (const c of [...cwds]) { | ||
cwds.add(esc(c)) | ||
cwds.add(esc(esc(c))) | ||
cwds.add(esc(esc(esc(c)))) | ||
cwds.add(esc(esc(esc(esc(c))))) | ||
} | ||
|
||
export const cleanCWD = (snap: string) => { | ||
const tag = '{CWD}' | ||
for (const c of cwds) { | ||
let i = -1 | ||
// pad it out so that the length matches through the walk | ||
const replace = tag + '\u0001'.repeat(c.length - tag.length) | ||
while ( | ||
-1 !== (i = snap.toLowerCase().indexOf(c.toLowerCase(), i)) | ||
) { | ||
snap = | ||
snap.substring(0, i) + | ||
replace + | ||
snap.substring(i + replace.length) | ||
} | ||
snap = snap.split(replace).join(tag) | ||
} | ||
return snap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import t from 'tap' | ||
|
||
t.capture(process, 'cwd', () => 'D:\\some\\TEST-Path') | ||
const { cleanCWD } = (await t.mockImport('../dist/esm/clean-cwd.js', { | ||
'@tapjs/core': { | ||
cwd: 'D:\\some\\TEST-Path', | ||
}, | ||
})) as typeof import('../dist/esm/clean-cwd.js') | ||
|
||
const s = process.cwd() | ||
const p = s.replace(/\\/g, '/') | ||
|
||
const obj = { s: [s, p, s, p] } | ||
const j1 = JSON.stringify(obj) | ||
const j2 = JSON.stringify({ json: j1 }) | ||
const j3 = JSON.stringify({ json: j2 }) | ||
const j4 = JSON.stringify({ json: j3 }) | ||
const tests = new Set([s, p, j1, j2, j3, j4]) | ||
for (const s of [...tests]) { | ||
tests.add(s.toUpperCase()) | ||
tests.add(s.toLowerCase()) | ||
} | ||
|
||
for (const c of tests) { | ||
t.equal( | ||
cleanCWD(c).toLowerCase().indexOf('test-path'), | ||
-1, | ||
'escaped', | ||
{ | ||
string: c, | ||
} | ||
) | ||
} | ||
|
||
// just capture the limit. only 4 levels of escaping supported | ||
t.not( | ||
cleanCWD(JSON.stringify(j4)).toLowerCase().indexOf('test-path'), | ||
-1, | ||
'does not escape beyond 4 levels' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.