jest-serializer-path
Advanced tools
Comparing version 0.1.14 to 0.1.15
@@ -6,6 +6,23 @@ 'use strict' | ||
const fs = require('fs') | ||
const fse = require('fs-extra') | ||
const Serializer = require('./') | ||
const normalizePaths = Serializer.normalizePaths | ||
const getRealPath = Serializer.getRealPath | ||
const cwd = process.cwd() | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
if (process.cwd() !== cwd) { | ||
process.chdir(cwd) | ||
} | ||
}) | ||
describe('serializer', () => { | ||
@@ -74,4 +91,2 @@ | ||
const cwd = process.cwd() | ||
const cwdUpdated = path.join(cwd, 'lib') | ||
@@ -117,3 +132,2 @@ process.chdir(cwdUpdated) | ||
const tempDir = os.tmpdir() | ||
const cwd = process.cwd() | ||
@@ -474,3 +488,2 @@ const sut = { | ||
const cwd = process.cwd() | ||
const homeDir = os.homedir() | ||
@@ -485,4 +498,2 @@ | ||
process.chdir(cwd) | ||
}) | ||
@@ -495,3 +506,3 @@ | ||
const tmpDirSpy = jest.spyOn(os, 'tmpdir') | ||
jest.spyOn(os, 'tmpdir') | ||
.mockImplementation(() => tempDir) | ||
@@ -504,4 +515,2 @@ | ||
tmpDirSpy.mockRestore() | ||
}) | ||
@@ -514,3 +523,3 @@ | ||
const homeDirSpy = jest.spyOn(os, 'homedir') | ||
jest.spyOn(os, 'homedir') | ||
.mockImplementation(() => homeDir) | ||
@@ -524,6 +533,138 @@ | ||
homeDirSpy.mockRestore() | ||
}) | ||
describe('symlinks', () => { | ||
const tempDir = path.resolve(os.tmpdir(), 'jest-serializer-path') | ||
beforeEach(() => { | ||
/** | ||
* Ensures that a directory is empty. | ||
* Deletes directory contents if the directory is not empty. | ||
* If the directory does not exist, it is created. | ||
* The directory itself is not deleted. | ||
*/ | ||
fse.emptyDirSync(tempDir) | ||
}) | ||
afterAll(() => { | ||
fse.removeSync(tempDir) | ||
}) | ||
it('handles process.cwd as symlink', () => { | ||
const realCwd = getRealPath(cwd) | ||
const linkedCwd = path.resolve(tempDir, 'fake-cwd') | ||
/** | ||
* normally would use process.chdir to update process.cwd but | ||
* process.chdir seems to already use the real filesystem path | ||
* | ||
* mock process.cwd to handle edge cases when a real filesystem path | ||
* is not returned | ||
*/ | ||
jest.spyOn(process, 'cwd') | ||
.mockImplementation(() => linkedCwd) | ||
fse.ensureSymlinkSync(realCwd, linkedCwd) | ||
const value = path.resolve(realCwd, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<PROJECT_ROOT>/some/nested/dir') | ||
}) | ||
it('handles process.cwd as symlink nested inside itself', () => { | ||
const root = path.parse(cwd).root | ||
const realCwd = path.normalize(`${root}/private/nested/cwd`) | ||
const linkedCwd = path.normalize(`${root}/nested/cwd`) | ||
jest.spyOn(process, 'cwd') | ||
.mockImplementation(() => linkedCwd) | ||
jest.spyOn(fs, 'realpathSync') | ||
.mockImplementation(() => realCwd) | ||
const value = path.resolve(realCwd, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<PROJECT_ROOT>/some/nested/dir') | ||
}) | ||
it('handles temp dir as symlink', () => { | ||
const realTempDir = getRealPath(tempDir) | ||
const linkedTempDir = path.resolve(realTempDir, 'fake-tmp') | ||
jest.spyOn(os, 'tmpdir') | ||
.mockImplementation(() => linkedTempDir) | ||
fse.ensureSymlinkSync(tempDir, linkedTempDir) | ||
const value = path.resolve(realTempDir, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<TEMP_DIR>/some/nested/dir') | ||
}) | ||
it('handles home dir as symlink', () => { | ||
const realHomeDir = os.homedir() | ||
const linkedHomeDir = path.resolve(tempDir, 'fake-home') | ||
jest.spyOn(os, 'homedir') | ||
.mockImplementation(() => linkedHomeDir) | ||
fse.ensureSymlinkSync(realHomeDir, linkedHomeDir) | ||
const value = path.resolve(realHomeDir, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<HOME_DIR>/some/nested/dir') | ||
}) | ||
it('use <HOME_DIR> when nested inside <TEMP_DIR> as symlink', () => { | ||
const realHomeDir = path.resolve(tempDir, 'real-home-dir') | ||
fse.emptyDirSync(realHomeDir) | ||
const linkedHomeDir = path.resolve(tempDir, 'home-dir') | ||
jest.spyOn(os, 'homedir') | ||
.mockImplementation(() => linkedHomeDir) | ||
fse.ensureSymlinkSync(realHomeDir, linkedHomeDir) | ||
const value = path.resolve(realHomeDir, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<HOME_DIR>/some/nested/dir') | ||
}) | ||
it('handles temp dir as symlink in mac os', () => { | ||
/** | ||
* on a mac this will equal /private/var/folders/... | ||
*/ | ||
const realTempDir = fs.realpathSync(os.tmpdir()) | ||
const value = path.resolve(realTempDir, 'some/nested/dir') | ||
const normalized = normalizePaths(value) | ||
expect(normalized).toEqual('<TEMP_DIR>/some/nested/dir') | ||
}) | ||
}) | ||
}) |
@@ -10,2 +10,3 @@ 'use strict' | ||
const path = require('path') | ||
const fs = require('fs') | ||
@@ -106,2 +107,3 @@ module.exports = { | ||
normalizePaths, | ||
getRealPath, | ||
} | ||
@@ -122,18 +124,35 @@ | ||
const cwd = process.cwd() | ||
const cwdReal = getRealPath(cwd) | ||
const tempDir = os.tmpdir() | ||
const tempDirReal = getRealPath(tempDir) | ||
const homeDir = os.homedir() | ||
const tempDir = os.tmpdir() | ||
const homeDirReal = getRealPath(homeDir) | ||
const homeRelativeToTemp = path.relative(tempDir, homeDir) | ||
const homeRelativeToTempReal = path.relative(tempDirReal, homeDir) | ||
const homeRealRelativeToTempReal = path.relative(tempDirReal, homeDirReal) | ||
const homeRealRelativeToTemp = path.relative(tempDir, homeDirReal) | ||
const runner = [ | ||
// Replace process.cwd with <PROJECT_ROOT> | ||
val => val.split(cwdReal).join('<PROJECT_ROOT>'), | ||
val => val.split(cwd).join('<PROJECT_ROOT>'), | ||
// Replace home directory with <TEMP_DIR> | ||
val => val.split(tempDirReal).join('<TEMP_DIR>'), | ||
val => val.split(tempDir).join('<TEMP_DIR>'), | ||
// Replace home directory with <HOME_DIR> | ||
val => val.split(homeDirReal).join('<HOME_DIR>'), | ||
val => val.split(homeDir).join('<HOME_DIR>'), | ||
// handle HOME_DIR nested inside TEMP_DIR | ||
val => val.split(`<TEMP_DIR>${path.sep + homeRelativeToTemp}`).join('<HOME_DIR>'), | ||
val => val.split(`<TEMP_DIR>${path.sep + homeRelativeToTempReal}`).join('<HOME_DIR>'), // untested | ||
val => val.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTempReal}`).join('<HOME_DIR>'), | ||
val => val.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTemp}`).join('<HOME_DIR>'), // untested | ||
// Remove win32 drive letters, C:\ -> \ | ||
val => val.replace(/[a-zA-Z]:\\/g, '\\'), | ||
// Convert win32 backslash's to forward slashes, \ -> / | ||
@@ -166,1 +185,18 @@ val => slash(val), | ||
} | ||
function getRealPath (pathname) { | ||
try { | ||
const realPath = fs.realpathSync(pathname) | ||
return realPath | ||
} | ||
catch (error) { | ||
return pathname | ||
} | ||
} |
{ | ||
"name": "jest-serializer-path", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "Remove absolute paths from your Jest snapshots", | ||
@@ -34,2 +34,3 @@ "author": "tribou", | ||
"eslint-plugin-tribou": "^1.0.5", | ||
"fs-extra": "^5.0.0", | ||
"jest": "^22.4.3", | ||
@@ -36,0 +37,0 @@ "jest-junit": "^3.3.0" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176427
532
8
3