-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: update win32 test serializer to work on any win32 machine (#313)
* test: update win32 test serializer to work on any win32 machine * fix: one more take on cwd normalization * fix: add original replace * fix: replace jest-serializer-path and normalizeWindowsLogSerializer with simpler serializer * fix: remove console logs
- Loading branch information
Showing
4 changed files
with
18 additions
and
66 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
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 |
---|---|---|
@@ -1,41 +1,25 @@ | ||
import os from 'os' | ||
import type { SnapshotSerializer } from 'vitest' | ||
|
||
const winNewLineReg = /\/r\/n/gim | ||
const winSepReg = /\\/g | ||
|
||
function doesUseDoubleSlashAsPath(val: string) { | ||
return val.includes('//vite-plugin-checker//') | ||
function normalizePaths(val: string) { | ||
return val | ||
.replace(/\\/gim, '/') // replace slashes | ||
.replace(/\/\//gim, '/') // replace slashes | ||
.replace(/[a-zA-Z]:\//gim, '/') // Remove win32 drive letters, C:\ -> \ | ||
} | ||
|
||
export const normalizeWindowsLogSerializer = { | ||
print(val: string, serialize) { | ||
let result = val | ||
if (os.platform() === 'win32') { | ||
result = result.replaceAll(winNewLineReg, '/n') | ||
result = result.replaceAll(process.cwd().replace(winSepReg, '/'), '<PROJECT_ROOT>') | ||
function createResult(val: string) { | ||
const cwd = normalizePaths(process.cwd()) | ||
|
||
if (doesUseDoubleSlashAsPath(result)) { | ||
result = result.replaceAll( | ||
`//a//vite-plugin-checker//vite-plugin-checker//playground-temp`, | ||
'<PROJECT_ROOT>/playground-temp' | ||
) | ||
result = result.split('//').join('/') | ||
} | ||
} | ||
return normalizePaths(val) | ||
.replaceAll(cwd, '<PROJECT_ROOT>') | ||
.replace(/\/r\/n/gim, '/n') | ||
} | ||
|
||
return serialize(result) | ||
export const normalizeLogSerializer: SnapshotSerializer = { | ||
print(val: string, print) { | ||
return print(createResult(val)) | ||
}, | ||
test(val) { | ||
if (typeof val !== 'string') return false | ||
|
||
if ( | ||
(os.platform() === 'win32' && | ||
(val.includes(process.cwd().replace(winSepReg, '/')) || winNewLineReg.test(val))) || | ||
doesUseDoubleSlashAsPath(val) | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
return typeof val === 'string' && val && createResult(val) !== val | ||
}, | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.