Skip to content

Commit

Permalink
core: add helpers for working with paths across OSes (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jun 15, 2022
1 parent b5f31bb commit 00282d6
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/core/README.md
Expand Up @@ -309,4 +309,27 @@ outputs:
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
```
162 changes: 162 additions & 0 deletions packages/core/__tests__/path-utils.test.ts
@@ -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)
})
}
})
5 changes: 5 additions & 0 deletions packages/core/src/core.ts
Expand Up @@ -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'
35 changes: 35 additions & 0 deletions packages/core/src/path-utils.ts
@@ -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)
}

0 comments on commit 00282d6

Please sign in to comment.