diff --git a/src/utils/utils.spec.ts b/src/utils/utils.spec.ts index 08ba05c..3e937f0 100644 --- a/src/utils/utils.spec.ts +++ b/src/utils/utils.spec.ts @@ -1,8 +1,15 @@ -import { asyncForEach, uuidv4, isWindows, isLinux } from './utils' +import { + asyncForEach, + uuidv4, + isWindows, + isLinux, + escapeWinSlashes +} from './utils' +import * as utilsModule from './utils' describe('uuidv4', () => { it('should generate 10000 uniq UUID', () => { - const uuids = [] + const uuids: string[] = [] for (let i = 0; i < 10 * 1000; i++) { const uuid = uuidv4() @@ -39,3 +46,21 @@ describe('isLinux', () => { expect(isLinux()).toEqual(process.platform === 'linux') }) }) + +describe('escapeWinSlashes', () => { + describe('when platform is win32', () => { + it('should return the path with double backslashes', () => { + jest.spyOn(utilsModule, 'isWindows').mockImplementationOnce(() => true) + const path = 'some\\file\\path' + expect(escapeWinSlashes(path)).toEqual('some\\\\file\\\\path') + }) + }) + + describe('when platform is not win32', () => { + it('should return the path as it is ', () => { + jest.spyOn(utilsModule, 'isWindows').mockImplementationOnce(() => false) + const path = 'some/file/path' + expect(escapeWinSlashes(path)).toEqual(path) + }) + }) +}) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index f95615f..17a732e 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -20,3 +20,9 @@ export const uniqArray = (data: any[]) => Array.from(new Set(data)) export const isWindows = () => process.platform === 'win32' export const isLinux = () => process.platform === 'linux' + +/** + * replace single backslash with double backslash + */ +export const escapeWinSlashes = (path: string) => + isWindows() ? path.replace(/\\/g, '\\\\') : path