Skip to content

Commit

Permalink
fix: source parameter consideration
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Jan 16, 2024
1 parent 7b5ef62 commit 59f43de
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 25 deletions.
72 changes: 56 additions & 16 deletions __tests__/unit/lib/adapter/GitAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ describe('GitAdapter', () => {
const gitAdapter = GitAdapter.getInstance(config)

// Act
await gitAdapter.getFilesPath()
await gitAdapter.getFilesPath(config.source)

// Assert
expect(mockedWalk).toBeCalled()
Expand Down Expand Up @@ -587,13 +587,31 @@ describe('GitAdapter', () => {
// Arrange

// Act
const result = await diffLineWalker(false)('.', [null])
const result = await diffLineWalker(config)('.', [null])

// Assert
expect(result).toBe(undefined)
})
})

describe(`when filepath does not start with "config.source"`, () => {
it.each(['not-force-app', 'force-app-extended'])(
'returns undefined',
async root => {
// Arrange

// Act
const result = await diffLineWalker({
...config,
source: 'force-app',
})(`${root}/test.file`, [null])

// Assert
expect(result).toBe(undefined)
}
)
})

describe('when first version of the file is not a blob', () => {
it('returns undefined', async () => {
// Arrange
Expand All @@ -602,7 +620,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file/path', [entry])
const result = await diffLineWalker(config)('file/path', [entry])

// Assert
expect(result).toBe(undefined)
Expand All @@ -620,7 +638,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file/path', [
const result = await diffLineWalker(config)('file/path', [
firstEntry,
secondEntry,
])
Expand All @@ -643,7 +661,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file/path', [
const result = await diffLineWalker(config)('file/path', [
firstEntry,
secondEntry,
])
Expand All @@ -655,6 +673,28 @@ describe('GitAdapter', () => {
})

describe('when filepath should be treated', () => {
describe(`when filepath starts with "config.source"`, () => {
it('returns the normalized path', async () => {
// Arrange
const firstEntry = {
type: jest.fn(() => Promise.resolve('blob')),
oid: jest.fn(() => undefined),
} as unknown as WalkerEntry
const secondEntry = {
type: jest.fn(() => Promise.resolve('blob')),
oid: jest.fn(() => 10),
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker({
...config,
source: 'force-app',
})('force-app/test.file', [firstEntry, secondEntry])

// Assert
expect(result).toBe('A\tforce-app/test.file')
})
})
describe('when file is added', () => {
it('returns the addition type and normalized path', async () => {
// Arrange
Expand All @@ -668,7 +708,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file\\path', [
const result = await diffLineWalker(config)('file/path', [
firstEntry,
secondEntry,
])
Expand All @@ -690,7 +730,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file\\path', [
const result = await diffLineWalker(config)('file/path', [
firstEntry,
secondEntry,
])
Expand All @@ -712,7 +752,7 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(false)('file\\path', [
const result = await diffLineWalker(config)('file/path', [
firstEntry,
secondEntry,
])
Expand Down Expand Up @@ -741,10 +781,10 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(true)('file\\path', [
firstEntry,
secondEntry,
])
const result = await diffLineWalker({
...config,
ignoreWhitespace: true,
})('file/path', [firstEntry, secondEntry])

// Assert
expect(result).toBe(undefined)
Expand All @@ -771,10 +811,10 @@ describe('GitAdapter', () => {
} as unknown as WalkerEntry

// Act
const result = await diffLineWalker(true)('file\\path', [
firstEntry,
secondEntry,
])
const result = await diffLineWalker({
...config,
ignoreWhitespace: true,
})('file/path', [firstEntry, secondEntry])

// Assert
expect(result).toBe('M\tfile/path')
Expand Down
25 changes: 17 additions & 8 deletions src/adapter/GitAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { SOURCE_DEFAULT_VALUE } from '../utils/cliConstants'
import { dirExists, fileExists } from '../utils/fsUtils'
import { DOT } from '../constant/fsConstants'
import { join } from 'path'
import { join, sep } from 'path'
import { getLFSObjectContentPath, isLFS } from '../utils/gitLfsHelper'
import { FileGitRef } from '../types/git'

Expand Down Expand Up @@ -155,7 +155,7 @@ export default class GitAdapter {
}
}

public async getFilesPath(path: string = SOURCE_DEFAULT_VALUE) {
public async getFilesPath(path: string) {
const walker = filePathWalker(path)
return await this.isoGit.walk({
...this.gitConfig,
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class GitAdapter {
}

public async getDiffLines() {
const walker = diffLineWalker(this.config.ignoreWhitespace)
const walker = diffLineWalker(this.config)
return this.isoGit.walk({
...this.gitConfig,
cache: GitAdapter.sharedCache,
Expand All @@ -236,10 +236,10 @@ export const filePathWalker =
return normalizedPath
}

export const diffLineWalker =
(ignoreWhitespace: boolean) =>
async (path: string, trees: (WalkerEntry | null)[]) => {
if (path === DOT) {
export const diffLineWalker = (config: Config) => {
const doesNotStartsWithSource = pathDoesNotStartsWith(config.source)
return async (path: string, trees: (WalkerEntry | null)[]) => {
if (path === DOT || doesNotStartsWithSource(path)) {
return
}

Expand All @@ -261,7 +261,7 @@ export const diffLineWalker =
type = DELETION
} else {
if (
ignoreWhitespace &&
config.ignoreWhitespace &&
(await isContentsEqualIgnoringWhiteChars(trees))
) {
return
Expand All @@ -271,6 +271,15 @@ export const diffLineWalker =

return `${type}\t${gitPathSeparatorNormalizer(path)}`
}
}

const pathDoesNotStartsWith = (root: string) => {
const gitFormattedRoot = (
root.split(sep).join(GIT_PATH_SEP) + GIT_PATH_SEP
).replace(/\/{2,}/g, '')
return (path: string) =>
root !== SOURCE_DEFAULT_VALUE && !path.startsWith(gitFormattedRoot)
}

const isContentsEqualIgnoringWhiteChars = async (
trees: (WalkerEntry | null)[]
Expand Down
4 changes: 3 additions & 1 deletion src/post-processor/includeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default class IncludeProcessor extends BaseProcessor {
[ADDITION]: [],
[DELETION]: [],
}
const lines: string[] = await this.gitAdapter.getFilesPath()
const lines: string[] = await this.gitAdapter.getFilesPath(
this.config.source
)
for (const line of lines) {
Object.keys(includeHolder).forEach(changeType => {
const changedLine = `${changeType}${TAB}${treatPathSep(line)}`
Expand Down

0 comments on commit 59f43de

Please sign in to comment.