forked from actions/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add helpers for working with paths across OSes (actions#1102)
- Loading branch information
Showing
4 changed files
with
226 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import * as path from 'path' | ||
|
||
import {toPlatformPath, toPosixPath, toWin32Path} from '../src/path-utils' | ||
|
||
describe('#toPosixPath', () => { | ||
const cases: { | ||
only?: boolean | ||
name: string | ||
input: string | ||
expected: string | ||
}[] = [ | ||
{ | ||
name: 'empty string', | ||
input: '', | ||
expected: '' | ||
}, | ||
{ | ||
name: 'single value', | ||
input: 'foo', | ||
expected: 'foo' | ||
}, | ||
{ | ||
name: 'with posix relative', | ||
input: 'foo/bar/baz', | ||
expected: 'foo/bar/baz' | ||
}, | ||
{ | ||
name: 'with posix absolute', | ||
input: '/foo/bar/baz', | ||
expected: '/foo/bar/baz' | ||
}, | ||
{ | ||
name: 'with win32 relative', | ||
input: 'foo\\bar\\baz', | ||
expected: 'foo/bar/baz' | ||
}, | ||
{ | ||
name: 'with win32 absolute', | ||
input: '\\foo\\bar\\baz', | ||
expected: '/foo/bar/baz' | ||
}, | ||
{ | ||
name: 'with a mix', | ||
input: '\\foo/bar/baz', | ||
expected: '/foo/bar/baz' | ||
} | ||
] | ||
|
||
for (const tc of cases) { | ||
const fn = tc.only ? it.only : it | ||
fn(tc.name, () => { | ||
const result = toPosixPath(tc.input) | ||
expect(result).toEqual(tc.expected) | ||
}) | ||
} | ||
}) | ||
|
||
describe('#toWin32Path', () => { | ||
const cases: { | ||
only?: boolean | ||
name: string | ||
input: string | ||
expected: string | ||
}[] = [ | ||
{ | ||
name: 'empty string', | ||
input: '', | ||
expected: '' | ||
}, | ||
{ | ||
name: 'single value', | ||
input: 'foo', | ||
expected: 'foo' | ||
}, | ||
{ | ||
name: 'with posix relative', | ||
input: 'foo/bar/baz', | ||
expected: 'foo\\bar\\baz' | ||
}, | ||
{ | ||
name: 'with posix absolute', | ||
input: '/foo/bar/baz', | ||
expected: '\\foo\\bar\\baz' | ||
}, | ||
{ | ||
name: 'with win32 relative', | ||
input: 'foo\\bar\\baz', | ||
expected: 'foo\\bar\\baz' | ||
}, | ||
{ | ||
name: 'with win32 absolute', | ||
input: '\\foo\\bar\\baz', | ||
expected: '\\foo\\bar\\baz' | ||
}, | ||
{ | ||
name: 'with a mix', | ||
input: '\\foo/bar\\baz', | ||
expected: '\\foo\\bar\\baz' | ||
} | ||
] | ||
|
||
for (const tc of cases) { | ||
const fn = tc.only ? it.only : it | ||
fn(tc.name, () => { | ||
const result = toWin32Path(tc.input) | ||
expect(result).toEqual(tc.expected) | ||
}) | ||
} | ||
}) | ||
|
||
describe('#toPlatformPath', () => { | ||
const cases: { | ||
only?: boolean | ||
name: string | ||
input: string | ||
expected: string | ||
}[] = [ | ||
{ | ||
name: 'empty string', | ||
input: '', | ||
expected: '' | ||
}, | ||
{ | ||
name: 'single value', | ||
input: 'foo', | ||
expected: 'foo' | ||
}, | ||
{ | ||
name: 'with posix relative', | ||
input: 'foo/bar/baz', | ||
expected: path.join('foo', 'bar', 'baz') | ||
}, | ||
{ | ||
name: 'with posix absolute', | ||
input: '/foo/bar/baz', | ||
expected: path.join(path.sep, 'foo', 'bar', 'baz') | ||
}, | ||
{ | ||
name: 'with win32 relative', | ||
input: 'foo\\bar\\baz', | ||
expected: path.join('foo', 'bar', 'baz') | ||
}, | ||
{ | ||
name: 'with win32 absolute', | ||
input: '\\foo\\bar\\baz', | ||
expected: path.join(path.sep, 'foo', 'bar', 'baz') | ||
}, | ||
{ | ||
name: 'with a mix', | ||
input: '\\foo/bar\\baz', | ||
expected: path.join(path.sep, 'foo', 'bar', 'baz') | ||
} | ||
] | ||
|
||
for (const tc of cases) { | ||
const fn = tc.only ? it.only : it | ||
fn(tc.name, () => { | ||
const result = toPlatformPath(tc.input) | ||
expect(result).toEqual(tc.expected) | ||
}) | ||
} | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as path from 'path' | ||
|
||
/** | ||
* toPosixPath converts the given path to the posix form. On Windows, \\ will be | ||
* replaced with /. | ||
* | ||
* @param pth. Path to transform. | ||
* @return string Posix path. | ||
*/ | ||
export function toPosixPath(pth: string): string { | ||
return pth.replace(/[\\]/g, '/') | ||
} | ||
|
||
/** | ||
* toWin32Path converts the given path to the win32 form. On Linux, / will be | ||
* replaced with \\. | ||
* | ||
* @param pth. Path to transform. | ||
* @return string Win32 path. | ||
*/ | ||
export function toWin32Path(pth: string): string { | ||
return pth.replace(/[/]/g, '\\') | ||
} | ||
|
||
/** | ||
* toPlatformPath converts the given path to a platform-specific path. It does | ||
* this by replacing instances of / and \ with the platform-specific path | ||
* separator. | ||
* | ||
* @param pth The path to platformize. | ||
* @return string The platform-specific path. | ||
*/ | ||
export function toPlatformPath(pth: string): string { | ||
return pth.replace(/[/\\]/g, path.sep) | ||
} |