diff --git a/README.md b/README.md index 8b56a6eb..f3af2210 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ OPTIONS this command invocation ``` -_See code: [src/commands/sgd/source/delta.ts](https://github.com/scolladon/sfdx-git-delta/blob/v5.36.0/src/commands/sgd/source/delta.ts)_ +_See code: [src/commands/sgd/source/delta.ts](https://github.com/scolladon/sfdx-git-delta/blob/main/src/commands/sgd/source/delta.ts)_ ### Windows users diff --git a/__tests__/unit/lib/utils/fsUtils.test.ts b/__tests__/unit/lib/utils/fsUtils.test.ts index b8bbe471..2316ce76 100644 --- a/__tests__/unit/lib/utils/fsUtils.test.ts +++ b/__tests__/unit/lib/utils/fsUtils.test.ts @@ -1,9 +1,8 @@ 'use strict' -import { sep } from 'path' - import { expect, jest, describe, it } from '@jest/globals' import { Stats, stat, readFile as fsReadFile } from 'fs-extra' +import { PATH_SEP } from '../../../../src/constant/fsConstants' import { dirExists, fileExists, @@ -192,32 +191,43 @@ describe('readFile', () => { }) }) -describe('treatPathSep', () => { - it(`replace / by ${sep}`, () => { +describe.each(['/', '\\'])('treatPathSep', sep => { + it(`replace ${sep} by ${PATH_SEP}`, () => { + // Arrange + const input = `test${sep + sep + sep}test${sep + sep}test${sep}test` + + // Act + const result = treatPathSep(input) + + // Assert + expect(result).toBe(`test${PATH_SEP}test${PATH_SEP}test${PATH_SEP}test`) + }) + + it(`keeps the leading ${sep}`, () => { // Arrange - const input = 'test///test//test/test' + const input = `${sep}test${sep}test` // Act const result = treatPathSep(input) // Assert - expect(result).toBe(`test${sep}test${sep}test${sep}test`) + expect(result).toBe(`${PATH_SEP}test${PATH_SEP}test`) }) - it(`replace \\ by ${sep}`, () => { + it(`keeps the trailing ${sep}`, () => { // Arrange - const input = 'test\\\\\\test\\\\test\\test' + const input = `test${sep}test${sep}` // Act const result = treatPathSep(input) // Assert - expect(result).toBe(`test${sep}test${sep}test${sep}test`) + expect(result).toBe(`test${PATH_SEP}test${PATH_SEP}`) }) }) describe('sanitizePath', () => { - it(`returns path with '${sep}' separator`, () => { + it(`returns path with '${PATH_SEP}' separator`, () => { // Arrange const input = 'test\\test/test' @@ -225,7 +235,7 @@ describe('sanitizePath', () => { const result = sanitizePath(input) // Assert - expect(result).toBe(`test${sep}test${sep}test`) + expect(result).toBe(`test${PATH_SEP}test${PATH_SEP}test`) }) it(`normalize path`, () => { @@ -236,7 +246,7 @@ describe('sanitizePath', () => { const result = sanitizePath(input) // Assert - expect(result).toBe(`test${sep}test`) + expect(result).toBe(`test${PATH_SEP}test`) }) it('return empty string when data is empty string', () => { diff --git a/src/adapter/GitAdapter.ts b/src/adapter/GitAdapter.ts index 39ac646f..42a61957 100644 --- a/src/adapter/GitAdapter.ts +++ b/src/adapter/GitAdapter.ts @@ -5,7 +5,7 @@ import { readFile } from 'fs-extra' import git, { TREE, WalkerEntry, WalkerIterateCallback } from 'isomorphic-git' import { simpleGit, SimpleGit } from 'simple-git' -import { DOT, PATH_SEP } from '../constant/fsConstants' +import { DOT } from '../constant/fsConstants' import { UTF8_ENCODING, GIT_FOLDER, @@ -289,7 +289,7 @@ const isContentsEqualIgnoringWhiteChars = async ( } const pathDoesNotStartsWith = (root: string) => { - const gitFormattedRoot = treatPathSep(root) + PATH_SEP + const gitFormattedRoot = treatPathSep(root) return (path: string) => gitFormattedRoot !== SOURCE_DEFAULT_VALUE && diff --git a/src/constant/fsConstants.ts b/src/constant/fsConstants.ts index 48e46013..4e02e96f 100644 --- a/src/constant/fsConstants.ts +++ b/src/constant/fsConstants.ts @@ -2,5 +2,5 @@ export const DOT = '.' export const UTF8_ENCODING = 'utf8' -export const PATH_SEPARATOR_REGEX = /[/\\]+/ +export const PATH_SEPARATOR_REGEX = /[/\\]+/g export const PATH_SEP = '/' diff --git a/src/utils/fsUtils.ts b/src/utils/fsUtils.ts index 4ae226db..0f68ffc4 100644 --- a/src/utils/fsUtils.ts +++ b/src/utils/fsUtils.ts @@ -10,7 +10,8 @@ import { } from '../constant/fsConstants' export const treatPathSep = (data: string) => - data.split(PATH_SEPARATOR_REGEX).filter(Boolean).join(PATH_SEP) + data.replace(PATH_SEPARATOR_REGEX, PATH_SEP) + export const sanitizePath = (data: string) => data ? normalize(treatPathSep(data)) : data