mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-04-22 15:58:06 +08:00
core: add helpers for working with paths across OSes (#1102)
This commit is contained in:
162
packages/core/__tests__/path-utils.test.ts
Normal file
162
packages/core/__tests__/path-utils.test.ts
Normal file
@@ -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)
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user