From 85f80f2ef64a9ba8737d0bc295bf914affffc279 Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Mon, 24 Jul 2023 22:45:21 +0300 Subject: [PATCH] fix: do not enforce paths to POSIX format (#370, #379) --- src/providers/transformers/entry.spec.ts | 7 ++----- src/providers/transformers/entry.ts | 12 ++++++++++-- src/tests/e2e/options/absolute.e2e.ts | 8 ++++++-- src/utils/path.spec.ts | 10 ---------- src/utils/path.ts | 7 ------- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/providers/transformers/entry.spec.ts b/src/providers/transformers/entry.spec.ts index 0db6c6dd..2e55163d 100644 --- a/src/providers/transformers/entry.spec.ts +++ b/src/providers/transformers/entry.spec.ts @@ -3,7 +3,6 @@ import * as path from 'path'; import Settings from '../../settings'; import * as tests from '../../tests'; -import * as utils from '../../utils'; import EntryTransformer from './entry'; import type { EntryTransformerFunction } from '../../types'; @@ -64,8 +63,7 @@ describe('Providers → Transformers → Entry', () => { const transformer = getTransformer({ absolute: true }); const entry = tests.entry.builder().path('root/file.txt').file().build(); - const fullpath = path.join(process.cwd(), 'root', 'file.txt'); - const expected = utils.path.unixify(fullpath); + const expected = path.join(process.cwd(), 'root', 'file.txt'); const actual = transformer(entry); @@ -87,8 +85,7 @@ describe('Providers → Transformers → Entry', () => { const transformer = getTransformer({ absolute: true, markDirectories: true }); const entry = tests.entry.builder().path('root/directory').directory().build(); - const fullpath = path.join(process.cwd(), 'root', 'directory', '/'); - const expected = utils.path.unixify(fullpath); + const expected = path.join(process.cwd(), 'root', 'directory', '/'); const actual = transformer(entry); diff --git a/src/providers/transformers/entry.ts b/src/providers/transformers/entry.ts index bb511d52..9da071a0 100644 --- a/src/providers/transformers/entry.ts +++ b/src/providers/transformers/entry.ts @@ -1,3 +1,5 @@ +import * as path from 'path'; + import * as utils from '../../utils'; import type Settings from '../../settings'; @@ -5,9 +7,12 @@ import type { Entry, EntryItem, EntryTransformerFunction } from '../../types'; export default class EntryTransformer { readonly #settings: Settings; + readonly #pathSeparatorSymbol: string; constructor(settings: Settings) { this.#settings = settings; + + this.#pathSeparatorSymbol = this.#getPathSeparatorSymbol(); } public getTransformer(): EntryTransformerFunction { @@ -19,11 +24,10 @@ export default class EntryTransformer { if (this.#settings.absolute) { filepath = utils.path.makeAbsolute(this.#settings.cwd, filepath); - filepath = utils.path.unixify(filepath); } if (this.#settings.markDirectories && entry.dirent.isDirectory()) { - filepath += '/'; + filepath += this.#pathSeparatorSymbol; } if (!this.#settings.objectMode) { @@ -35,4 +39,8 @@ export default class EntryTransformer { path: filepath, }; } + + #getPathSeparatorSymbol(): string { + return this.#settings.absolute ? path.sep : '/'; + } } diff --git a/src/tests/e2e/options/absolute.e2e.ts b/src/tests/e2e/options/absolute.e2e.ts index b5907871..84a46dbc 100644 --- a/src/tests/e2e/options/absolute.e2e.ts +++ b/src/tests/e2e/options/absolute.e2e.ts @@ -2,10 +2,14 @@ import * as path from 'path'; import * as runner from '../runner'; -const CWD = process.cwd().replace(/\\/g, '/'); +const CWD = process.cwd(); function resultTransform(item: string): string { - return item.replace(CWD, ''); + return item + .replace(CWD, '') + // Backslashes are used on Windows. + // The `fixtures` directory is under our control, so we are confident that the conversions are correct. + .replace(/[/\\]/g, '/'); } runner.suite('Options Absolute', { diff --git a/src/utils/path.spec.ts b/src/utils/path.spec.ts index 770ad317..680cd838 100644 --- a/src/utils/path.spec.ts +++ b/src/utils/path.spec.ts @@ -4,16 +4,6 @@ import * as path from 'path'; import * as util from './path'; describe('Utils → Path', () => { - describe('.unixify', () => { - it('should return path with converted slashes', () => { - const expected = 'directory/nested/file.md'; - - const actual = util.unixify('directory\\nested/file.md'); - - assert.strictEqual(actual, expected); - }); - }); - describe('.makeAbsolute', () => { it('should return absolute filepath', () => { const expected = path.join(process.cwd(), 'file.md'); diff --git a/src/utils/path.ts b/src/utils/path.ts index 6808e8ab..da74c5d0 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -24,13 +24,6 @@ const DOS_DEVICE_PATH_RE = /^\\\\(?[.?])/; */ const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@{}])/g; -/** - * Designed to work only with simple paths: `dir\\file`. - */ -export function unixify(filepath: string): string { - return filepath.replace(/\\/g, '/'); -} - export function makeAbsolute(cwd: string, filepath: string): string { return path.resolve(cwd, filepath); }