Skip to content

Commit

Permalink
fix: absolute path handling (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Mar 18, 2024
1 parent b89c806 commit a926a88
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)_
<!-- commandsstop -->

### Windows users
Expand Down
34 changes: 22 additions & 12 deletions __tests__/unit/lib/utils/fsUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -192,40 +191,51 @@ 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'

// Act
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`, () => {
Expand All @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/GitAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 &&
Expand Down
2 changes: 1 addition & 1 deletion src/constant/fsConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '/'
3 changes: 2 additions & 1 deletion src/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a926a88

Please sign in to comment.