Skip to content

Commit

Permalink
fix: normalize process.cwd on windows (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 1, 2022
1 parent d429aa2 commit a7b0f7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/path.ts
Expand Up @@ -82,7 +82,7 @@ export const resolve: typeof path.resolve = function (...args) {
let resolvedAbsolute = false

for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
const path = i >= 0 ? args[i] : process.cwd()
const path = i >= 0 ? args[i] : process.cwd().replace(/\\/g, '/')

// Skip empty entries
if (path.length === 0) {
Expand Down
26 changes: 20 additions & 6 deletions test/index.spec.ts
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'

import { basename, dirname, extname, format, parse, relative, delimiter, isAbsolute, join, normalize, resolve, sep, toNamespacedPath } from '../src'

Expand Down Expand Up @@ -171,23 +171,26 @@ it('parse', () => {
runTest('relative', relative, [
// POSIX
['/data/orandea/test/aaa', '/data/orandea/impl/bbb', '../../impl/bbb'],
[() => process.cwd(), './dist/client/b-scroll.d.ts', 'dist/client/b-scroll.d.ts'],

// Windows
['C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb', '../../impl/bbb']
['C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb', '../../impl/bbb'],
[() => process.cwd().replace(/\\/g, '/'), './dist/client/b-scroll.d.ts', 'dist/client/b-scroll.d.ts'],
[() => process.cwd(), './dist/client/b-scroll.d.ts', 'dist/client/b-scroll.d.ts']
])

runTest('resolve', resolve, [
// POSIX
['/', '/path', '/path'],
['/foo/bar', './baz', '/foo/bar/baz'],
['/foo/bar', '/tmp/file/', '/tmp/file'],
['wwwroot', 'static_files/png/', '../gif/image.gif', `${process.cwd()}/wwwroot/static_files/gif/image.gif`],
['wwwroot', 'static_files/png/', '../gif/image.gif', () => `${process.cwd().replace(/\\/g, '/')}/wwwroot/static_files/gif/image.gif`],

// Windows
['C:\\foo\\bar', '.\\baz', 'C:/foo/bar/baz'],
['\\foo\\bar', '.\\baz', '/foo/bar/baz'],
['\\foo\\bar', '\\tmp\\file\\', '/tmp/file'],
['wwwroot', 'static_files\\png\\', '..\\gif\\image.gif', `${process.cwd()}/wwwroot/static_files/gif/image.gif`],
['wwwroot', 'static_files\\png\\', '..\\gif\\image.gif', () => `${process.cwd().replace(/\\/g, '/')}/wwwroot/static_files/gif/image.gif`],
['C:\\Windows\\path\\only', '../../reports', 'C:/Windows/reports'],
['C:\\Windows\\long\\path\\mixed/with/unix', '../..', '..\\../reports', 'C:/Windows/long/reports']
])
Expand All @@ -212,19 +215,30 @@ describe('constants', () => {
})

function _s (item) {
return JSON.stringify(item).replace(/"/g, '\'')
return JSON.stringify(_r(item)).replace(/"/g, '\'')
}

function _r (item) {
return typeof item === 'function' ? item() : item
}

export function runTest (name, fn, items) {
if (!Array.isArray(items)) {
items = Object.entries(items).map(e => e.flat())
}
describe(`${name}`, () => {
let cwd
for (const item of items) {
const expected = item.pop()
const args = item
it(`${name}(${args.map(_s).join(',')}) should be ${_s(expected)}`, () => {
expect(fn(...args)).to.equal(expected)
expect(fn(...args.map(_r))).to.equal(_r(expected))
})
it(`${name}(${args.map(_s).join(',')}) should be ${_s(expected)} on Windows`, () => {
cwd = process.cwd
process.cwd = vi.fn(() => 'C:\\Windows\\path\\only')
expect(fn(...args.map(_r))).to.equal(_r(expected))
process.cwd = cwd
})
}
})
Expand Down

0 comments on commit a7b0f7b

Please sign in to comment.