Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize process.cwd on windows #31

Merged
merged 4 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/path.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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