Skip to content

Commit

Permalink
fix: windows path separator with git (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Jan 30, 2023
1 parent 037c394 commit b760a7e
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 400 deletions.
8 changes: 8 additions & 0 deletions __tests__/unit/lib/utils/fileGitDiff.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'
const fileGitDiff = require('../../../../src/utils/fileGitDiff')
const { gitPathSeparatorNormalizer } = require('../../../../src/utils/fsHelper')
jest.mock('child_process')
jest.mock('../../../../src/utils/fsHelper')
const child_process = require('child_process')

const TEST_PATH = 'path/to/file'
Expand All @@ -17,6 +19,7 @@ describe(`test if fileGitDiff`, () => {
for await (const line of result) {
expect(line).toStrictEqual(output.shift())
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})

test('can parse git diff addition', async () => {
Expand All @@ -27,6 +30,7 @@ describe(`test if fileGitDiff`, () => {
for await (const line of result) {
expect(line).toStrictEqual(output.shift())
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})

test('can parse git diff deletion', async () => {
Expand All @@ -37,6 +41,7 @@ describe(`test if fileGitDiff`, () => {
for await (const line of result) {
expect(line).toStrictEqual(output.shift())
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})

test('can apply permissive git diff', async () => {
Expand All @@ -51,6 +56,7 @@ describe(`test if fileGitDiff`, () => {
for await (const line of result) {
expect(line).toStrictEqual(output.shift())
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})

test('can parse git diff context line', async () => {
Expand All @@ -61,6 +67,7 @@ describe(`test if fileGitDiff`, () => {
for await (const line of result) {
expect(line).toStrictEqual(output.shift())
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})

test('can reject in case of error', () => {
Expand All @@ -71,5 +78,6 @@ describe(`test if fileGitDiff`, () => {
} catch (e) {
expect(e).toBeDefined()
}
expect(gitPathSeparatorNormalizer).toHaveBeenCalled()
})
})
67 changes: 63 additions & 4 deletions __tests__/unit/lib/utils/fsHelper.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
'use strict'
const {
copyFiles,
gitPathSeparatorNormalizer,
readDir,
isSubDir,
readFile,
readPathFromGit,
scan,
scanExtension,
} = require('../../../../src/utils/fsHelper')
const { getStreamContent } = require('../../../../src/utils/childProcessUtils')
const {
getStreamContent,
treatPathSep,
} = require('../../../../src/utils/childProcessUtils')
const { spawn } = require('child_process')
const fs = require('fs')
const { outputFile } = require('fs-extra')
const { EOL } = require('os')

jest.mock('../../../../src/utils/childProcessUtils')
jest.mock('fs')
jest.mock('fs-extra')
jest.mock('child_process')
Expand All @@ -23,22 +27,73 @@ jest.mock('../../../../src/utils/childProcessUtils', () => {
'../../../../src/utils/childProcessUtils'
)

//Mock the default export and named export 'foo'
return {
...originalModule,
getStreamContent: jest.fn(),
treatPathSep: jest.fn(),
}
})

let work
beforeEach(() => {
//jest.clearAllMocks()
work = {
config: { output: '', source: '', repo: '', generateDelta: false },
config: {
output: '',
source: '',
repo: '',
generateDelta: false,
from: 'pastsha',
to: 'recentsha',
},
warnings: [],
}
})

describe('gitPathSeparatorNormalizer', () => {
it('replaces every instance of \\', async () => {
// Arrange
const windowsPath = 'path\\to\\a\\\\file'

// Act
const result = gitPathSeparatorNormalizer(windowsPath)

// Assert
expect(result).toEqual('path/to/a/file')
})

describe.each([undefined, null])('when called with %s', falsy => {
it('return null', () => {
// Act
const result = gitPathSeparatorNormalizer(falsy)

// Assert
expect(result).toBeUndefined()
})
})
})

describe('readPathFromGit', () => {
describe.each([
['windows', 'force-app\\main\\default\\classes\\myClass.cls'],
['unix', 'force-app/main/default/classes/myClass.cls'],
])('when path is %s format', (_, path) => {
it('should use "config.to" and "normalized path" to get git history', async () => {
// Act
await readPathFromGit(path, work.config)

// Assert
const normalizedPath = path.replace(/\\+/g, '/')
expect(spawn).toHaveBeenCalledWith(
'git',
expect.arrayContaining([`${work.config.to}:${normalizedPath}`]),
expect.anything()
)
expect(getStreamContent).toBeCalled()
})
})
})

describe('copyFile', () => {
describe('when file is already copied', () => {
it('should not copy file', async () => {
Expand Down Expand Up @@ -75,6 +130,7 @@ describe('copyFile', () => {
describe('when content is a folder', () => {
it('should copy the folder', async () => {
// Arrange
treatPathSep.mockImplementationOnce(() => 'output/copyDir/copyFile')
getStreamContent.mockImplementationOnce(
() => 'tree HEAD:folder\n\ncopyFile'
)
Expand All @@ -87,6 +143,7 @@ describe('copyFile', () => {
expect(spawn).toBeCalledTimes(2)
expect(getStreamContent).toBeCalledTimes(2)
expect(outputFile).toBeCalledTimes(1)
expect(treatPathSep).toBeCalledTimes(1)
})
})
describe('when content is not a git location', () => {
Expand All @@ -113,6 +170,7 @@ describe('copyFile', () => {
beforeEach(async () => {
// Arrange
getStreamContent.mockImplementation(() => 'content')
treatPathSep.mockImplementationOnce(() => 'output/copyFile')
})
it('should copy the file', async () => {
// Act
Expand All @@ -122,6 +180,7 @@ describe('copyFile', () => {
expect(spawn).toBeCalled()
expect(getStreamContent).toBeCalled()
expect(outputFile).toBeCalledTimes(1)
expect(treatPathSep).toBeCalledTimes(1)
})
})
})
Expand Down
48 changes: 32 additions & 16 deletions __tests__/unit/lib/utils/repoGitDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe(`test if repoGitDiff`, () => {
const output = []
child_process.__setOutput([output])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
{ output: '', repo: '', source: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -42,6 +42,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
ignore: FORCEIGNORE_MOCK_PATH,
ignoreWhitespace: true,
},
Expand All @@ -57,7 +58,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([[], output.map(x => `1${TAB}1${TAB}${x}`), []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
{ output: '', repo: '', source: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -70,7 +71,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([[], output.map(x => `1${TAB}1${TAB}${x}`), []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -83,7 +84,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([[], [], output.map(x => `1${TAB}1${TAB}${x}`)])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -96,7 +97,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([[], [], output.map(x => `-${TAB}-${TAB}${x}`)])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -109,7 +110,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([output.map(x => `-${TAB}-${TAB}${x}`), [], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -122,7 +123,7 @@ describe(`test if repoGitDiff`, () => {
]
child_process.__setOutput([output.map(x => `-${TAB}-${TAB}${x}`), [], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -133,7 +134,7 @@ describe(`test if repoGitDiff`, () => {
const output = 'force-app/main/default/pages/test.page-meta.xml'
child_process.__setOutput([[`1${TAB}1${TAB}${output}`], [], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
{ output: '', repo: '', source: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -146,7 +147,12 @@ describe(`test if repoGitDiff`, () => {
const output = 'force-app/main/default/pages/test.page-meta.xml'
child_process.__setOutput([[], [`1${TAB}1${TAB}${output}`], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignoreDestructive: FORCEIGNORE_MOCK_PATH },
{
output: '',
repo: '',
source: '',
ignoreDestructive: FORCEIGNORE_MOCK_PATH,
},
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -166,6 +172,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
ignore: FORCEIGNORE_MOCK_PATH,
ignoreDestructive: FORCEIGNORE_MOCK_PATH,
},
Expand All @@ -181,7 +188,7 @@ describe(`test if repoGitDiff`, () => {
const output = 'force-app/main/default/pages/test.page-meta.xml'
child_process.__setOutput([[], [`1${TAB}1${TAB}${output}`], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
{ output: '', repo: '', source: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -194,7 +201,12 @@ describe(`test if repoGitDiff`, () => {
const output = 'force-app/main/default/pages/test.page-meta.xml'
child_process.__setOutput([[], [], [`1${TAB}1${TAB}${output}`]])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignoreDestructive: FORCEIGNORE_MOCK_PATH },
{
output: '',
repo: '',
source: '',
ignoreDestructive: FORCEIGNORE_MOCK_PATH,
},
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -206,7 +218,7 @@ describe(`test if repoGitDiff`, () => {
const output = 'force-app/main/default/pages/test.page-meta.xml'
child_process.__setOutput([[`1${TAB}1${TAB}${output}`], [], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
{ output: '', repo: '', source: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -226,7 +238,7 @@ describe(`test if repoGitDiff`, () => {
[`1${TAB}1${TAB}${output[1]}`],
])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -245,7 +257,7 @@ describe(`test if repoGitDiff`, () => {
[`1${TAB}1${TAB}${output[1]}`],
])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -264,7 +276,7 @@ describe(`test if repoGitDiff`, () => {
[`1${TAB}1${TAB}${output[1]}`],
])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -286,7 +298,7 @@ describe(`test if repoGitDiff`, () => {
[`1${TAB}1${TAB}${output[1]}`],
])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '' },
{ output: '', repo: '', source: '' },
globalMetadata
)
const work = await repoGitDiff.getLines()
Expand All @@ -304,6 +316,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
include: FORCEINCLUDE_MOCK_PATH,
},
globalMetadata
Expand All @@ -320,6 +333,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
includeDestructive: FORCEINCLUDE_MOCK_PATH,
},
globalMetadata
Expand All @@ -339,6 +353,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
include: FORCEINCLUDE_MOCK_PATH,
},
globalMetadata
Expand All @@ -362,6 +377,7 @@ describe(`test if repoGitDiff`, () => {
{
output: '',
repo: '',
source: '',
includeDestructive: FORCEINCLUDE_MOCK_PATH,
},
globalMetadata
Expand Down
Loading

0 comments on commit b760a7e

Please sign in to comment.