Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore flow translation #360

Merged
merged 7 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ const {
FLOW_DIRECTORY_NAME,
TRANSLATION_TYPE,
} = require('../../../../src/utils/metadataConstants')
const { copyFiles, scanExtension } = require('../../../../src/utils/fsHelper')
const {
copyFiles,
scanExtension,
isSubDir,
} = require('../../../../src/utils/fsHelper')
jest.mock('fs-extra')
jest.mock('fast-xml-parser')
jest.mock('../../../../src/utils/fsHelper', () => ({
scanExtension: jest.fn(),
copyFiles: jest.fn(),
readFile: jest.fn(),
}))
jest.mock('../../../../src/utils/fsHelper')
const mockForPath = jest.fn()
jest.mock('../../../../src/utils/ignoreHelper', () => {
return jest.fn().mockImplementation(() => {
return { forPath: mockForPath }
})
})

const FR = 'fr'
const EN = 'en'
Expand All @@ -34,11 +40,11 @@ describe('FlowTranslationProcessor', () => {
package: new Map(),
destructiveChanges: new Map(),
},
config: { repo: 'repo', output: 'output', generateDelta: true },
config: { source: '.', output: 'output', generateDelta: true },
}
sut = new FlowTranslationProcessor(work)
flap = trueAfter(1)
scanExtension.mockImplementationOnce(() => ({
scanExtension.mockImplementation(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
value: `${FR}.translation-meta.xml`,
Expand All @@ -47,6 +53,7 @@ describe('FlowTranslationProcessor', () => {
}),
}))
})

describe('when there is no translation file', () => {
beforeEach(() => {
// Arrange
Expand Down Expand Up @@ -126,7 +133,7 @@ describe('FlowTranslationProcessor', () => {
let count = 0
const getTranslationName = () =>
[`${FR}.translation-meta.xml`, `${EN}.translation-meta.xml`][count++]
scanExtension.mockImplementationOnce(() => ({
scanExtension.mockImplementation(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
value: getTranslationName(),
Expand Down Expand Up @@ -178,12 +185,81 @@ describe('FlowTranslationProcessor', () => {
expect(work.diffs.package.has(TRANSLATION_TYPE)).toBeTruthy()
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(2)
if (generateDelta) expect(copyFiles).toHaveBeenCalled()
if (generateDelta) expect(copyFiles).toHaveBeenCalledTimes(2)
else expect(copyFiles).not.toHaveBeenCalled()
})
}
)
})
})

describe('when translation files are ignored', () => {
beforeEach(() => {
// Arrange
work.config.ignore = '.forceignore'
mockForPath.mockResolvedValue({ ignores: () => true })
})
it('should not add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.has(TRANSLATION_TYPE)).toBeFalsy()
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockForPath).toHaveBeenCalledTimes(1)
expect(mockParse).not.toHaveBeenCalled()
expect(copyFiles).not.toHaveBeenCalled()
})
})

describe('when translation files are not ignored', () => {
beforeEach(() => {
// Arrange
work.diffs.package = new Map([
[FLOW_DIRECTORY_NAME, new Set([flowFullName])],
])
work.config.ignore = '.forceignore'
mockForPath.mockResolvedValue({ ignores: () => false })
})
it('should add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.has(TRANSLATION_TYPE)).toBeTruthy()
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockForPath).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(1)
expect(copyFiles).toHaveBeenCalledTimes(1)
})
})

describe('when the translation file is subDir of output', () => {
beforeEach(() => {
// Arrange
const out = 'out'
work.config.output = out
scanExtension.mockImplementation(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
value: `${out}/${FR}.translation-meta.xml`,
done: flap(),
}),
}),
}))
mockParse.mockImplementationOnce(() => ({}))
isSubDir.mockImplementation(() => true)
})
it('should not add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.has(TRANSLATION_TYPE)).toBeFalsy()
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).not.toHaveBeenCalled()
expect(copyFiles).not.toHaveBeenCalled()
})
})
})
})
15 changes: 7 additions & 8 deletions __tests__/unit/lib/post-processor/postProcessorManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,19 @@ describe('postProcessorManager', () => {
})

describe('when postProcessor `process` throws', () => {
it('should throw', async () => {
it('should append the error in warnings', async () => {
// Arrange
expect.assertions(1)
const sut = new PostProcessorManager()
const work = {
warnings: [],
}
const sut = new PostProcessorManager(work)
sut.use(new TestProcesor())
processSpy.mockImplementationOnce(() => Promise.reject('Error'))

// Act
try {
await sut.execute()
} catch (error) {
// Assert
expect(error).toEqual('Error')
}
await sut.execute()
expect(work.warnings.length).toBe(1)
})
})
})
51 changes: 51 additions & 0 deletions __tests__/unit/lib/utils/fsHelper.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const {
copyFiles,
isSubDir,
readFile,
scan,
scanExtension,
Expand Down Expand Up @@ -275,3 +276,53 @@ describe('scanExtension', () => {
})
})
})

describe('isSubDir', () => {
describe('when parent contains dir', () => {
it('returns true', async () => {
// Arrange

// Act
const result = isSubDir('parent', 'parent/dir')

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

describe('when parent does not contains dir', () => {
it('returns false', async () => {
// Arrange

// Act
const result = isSubDir('parent', 'dir/child')

// Assert
expect(result).toBe(false)
})
})
it.each([
['/foo', '/foo', false],
['/foo', '/bar', false],
['/foo', '/foobar', false],
['/foo', '/foo/bar', true],
['/foo', '/foo/../bar', false],
['/foo', '/foo/./bar', true],
['/bar/../foo', '/foo/bar', true],
['/foo', './bar', false],
['C:\\Foo', 'C:\\Foo\\Bar', false],
['C:\\Foo', 'C:\\Bar', false],
['C:\\Foo', 'D:\\Foo\\Bar', false],
])(
`should verify %s expect %s to be a subDir: %s`,
(parent, child, expected) => {
// Arrange

// Act
const actual = isSubDir(parent, child)

// Assert
expect(actual).toBe(expected)
}
)
})
68 changes: 68 additions & 0 deletions __tests__/unit/lib/utils/ignoreHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const IgnoreHelper = require('../../../../src/utils/ignoreHelper')
const { readFile } = require('../../../../src/utils/fsHelper')
jest.mock('../../../../src/utils/fsHelper', () => ({
readFile: jest.fn(),
}))

describe('ignoreHelper', () => {
let sut
beforeEach(() => {
sut = new IgnoreHelper()
})
describe('forPath', () => {
describe('when path exist', () => {
it('returns an ignore instance', async () => {
// Arrange
expect.assertions(2)
readFile.mockImplementationOnce(() => {
return ''
})

// Act
const actual = await sut.forPath('.forceignore')

// Assert
expect(actual).toBeTruthy()
expect(readFile).toHaveBeenCalledTimes(1)
})
})

describe('when path is asked multiple times', () => {
it('returns the same ignore instance', async () => {
// Arrange
expect.assertions(3)
const ignorePath = '.ignore'
readFile.mockImplementationOnce(() => {
return ''
})
const expected = await sut.forPath(ignorePath)

// Act
const actual = await sut.forPath(ignorePath)

// Assert
expect(actual).toBeTruthy()
expect(expected).toBe(actual)
expect(readFile).toHaveBeenCalledTimes(1)
})
})

describe('when path does not exist', () => {
it('throws exception', async () => {
// Arrange
expect.assertions(2)
readFile.mockRejectedValue(new Error())

// Act
let actual
try {
actual = await sut.forPath('.notexist')
} catch (e) {
// Assert
expect(actual).toBeFalsy()
expect(readFile).toHaveBeenCalledTimes(1)
}
})
})
})
})
6 changes: 3 additions & 3 deletions __tests__/unit/lib/utils/repoGitDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ describe(`test if repoGitDiff`, () => {
expect(work).toStrictEqual(expected)
})

test('can filter deletion if only ignored is specified files', async () => {
test('cannot filter deletion if only ignored is specified files', async () => {
scolladon marked this conversation as resolved.
Show resolved Hide resolved
const output = 'force-app/main/default/lwc/jsconfig.json'
child_process.__setOutput([[], [`1${TAB}1${TAB}${output}`], []])
const repoGitDiff = new RepoGitDiff(
{ output: '', repo: '', ignore: FORCEIGNORE_MOCK_PATH },
globalMetadata
)
const work = await repoGitDiff.getLines()
//should be empty
const expected = []

const expected = [`${DELETION}${TAB}${output}`]
expect(work).toStrictEqual(expected)
})

Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
},
"author": "Sebastien Colladon <[email protected]>",
"dependencies": {
"@oclif/command": "^1.8.16",
"@oclif/config": "^1.18.3",
"@oclif/errors": "^1.3.5",
"@salesforce/command": "^5.2.6",
"@salesforce/core": "^2.37.1",
"fast-xml-parser": "^4.0.9",
"@oclif/command": "^1.8.18",
"@oclif/config": "^1.18.5",
"@oclif/errors": "^1.3.6",
"@salesforce/command": "^5.2.11",
"@salesforce/core": "^3.30.9",
"fast-xml-parser": "^4.0.10",
"fs-extra": "^10.1.0",
"ignore": "^5.2.0",
"micromatch": "^4.0.5",
Expand Down Expand Up @@ -71,30 +71,30 @@
"@commitlint/config-angular": "^17.1.0",
"@commitlint/prompt-cli": "^17.1.2",
"@oclif/dev-cli": "^1.26.10",
"@oclif/plugin-help": "^5.1.12",
"@oclif/test": "^2.1.1",
"@oclif/plugin-help": "^5.1.14",
"@oclif/test": "^2.2.2",
"@salesforce/dev-config": "^3.1.0",
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.14",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"@types/mocha": "^10.0.0",
"@types/node": "^18.7.23",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"conventional-github-releaser": "^3.1.5",
"eslint": "^8.23.0",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-salesforce-typescript": "^1.1.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^39.3.6",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"jest": "^29.0.1",
"jest": "^29.1.2",
"lint-staged": "^13.0.3",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"prettier-eslint": "^15.0.1",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.2",
"typescript": "^4.8.4",
"yarn-upgrade-all": "^0.7.1"
},
"oclif": {
Expand Down
Loading