mirror of https://github.com/actions/toolkit
core: add helpers for working with paths across OSes (#1102)
parent
b5f31bb5a2
commit
00282d6145
|
@ -310,3 +310,26 @@ runs:
|
|||
using: 'node12'
|
||||
main: 'dist/index.js'
|
||||
```
|
||||
|
||||
#### Filesystem path helpers
|
||||
|
||||
You can use these methods to manipulate file paths across operating systems.
|
||||
|
||||
The `toPosixPath` function converts input paths to Posix-style (Linux) paths.
|
||||
The `toWin32Path` function converts input paths to Windows-style paths. These
|
||||
functions work independently of the underlying runner operating system.
|
||||
|
||||
```js
|
||||
toPosixPath('\\foo\\bar') // => /foo/bar
|
||||
toWin32Path('/foo/bar') // => \foo\bar
|
||||
```
|
||||
|
||||
The `toPlatformPath` function converts input paths to the expected value on the runner's operating system.
|
||||
|
||||
```js
|
||||
// On a Windows runner.
|
||||
toPlatformPath('/foo/bar') // => \foo\bar
|
||||
|
||||
// On a Linux runner.
|
||||
toPlatformPath('\\foo\\bar') // => /foo/bar
|
||||
```
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
})
|
|
@ -369,3 +369,8 @@ export {summary} from './summary'
|
|||
* @deprecated use core.summary
|
||||
*/
|
||||
export {markdownSummary} from './summary'
|
||||
|
||||
/**
|
||||
* Path exports
|
||||
*/
|
||||
export {toPosixPath, toWin32Path, toPlatformPath} from './path-utils'
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue