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 white space parameter --ignore-whitepsace, -W #267

Merged
merged 12 commits into from
Mar 18, 2022
1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

yarn pack
yarn test:coverage
yarn outdated
13 changes: 7 additions & 6 deletions __mocks__/fast-xml-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ fxp.__setMockContent = contents => {
}
}

fxp.XMLParser = class XMLParser {
parse(content) {
return JSON.parse(xml2json[content])
fxp.XMLParser = function () {
return {
parse: jest.fn(content => JSON.parse(xml2json[content])),
}
}
fxp.XMLBuilder = class XMLBuilder {
build(content) {
return json2xml[content]

fxp.XMLBuilder = function () {
return {
build: jest.fn(content => json2xml[content]),
}
}

Expand Down
6 changes: 3 additions & 3 deletions __mocks__/fs-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ fse.errorMode = false
fse.outputFileError = false
fse.pathShouldExist = true

fse.pathExists = jest.fn(() => Promise.resolve(fse.pathShouldExist))
fse.pathExists.mockImplementation(() => Promise.resolve(fse.pathShouldExist))
fse.copy = jest.fn(() => {
if (fse.errorMode) return Promise.reject()
return Promise.resolve()
})
fse.copySync = jest.fn(() => {
fse.copySync.mockImplementation(() => {
if (fse.errorMode) throw new Error()
})
fse.outputFile = jest.fn(() =>
fse.outputFile.mockImplementation(() =>
fse.outputFileError ? Promise.reject() : Promise.resolve()
)

Expand Down
62 changes: 40 additions & 22 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = jest.genMockFromModule('fs')
const { MASTER_DETAIL_TAG } = require('../src/utils/metadataConstants')

fs.errorMode = false
fs.statErrorMode = false
let mockFiles = {}
let mockContent = {}
let filePathList = new Set()
Expand All @@ -22,31 +23,48 @@ fs.__setMockFiles = newMockFiles => {

fs.promises = {}

fs.promises.stat = elem =>
Promise.resolve({
isDirectory() {
return elem !== 'file'
},
isFile() {
return filePathList.has(elem)
},
})
fs.promises.stat = jest.fn(
elem =>
new Promise((res, rej) => {
if (fs.statErrorMode) rej(new Error())
else
res({
isDirectory() {
return filePathList.has(elem)
},
isFile() {
return filePathList.has(elem)
},
})
})
)

fs.promises.readFile = jest.fn(
path =>
new Promise((res, rej) => {
if (fs.errorMode) rej(new Error())
else {
const result = Object.prototype.hasOwnProperty.call(mockContent, path)
? mockContent[path]
: MASTER_DETAIL_TAG
res(result)
}
})
)

fs.promises.readFile = path =>
new Promise((res, rej) => {
if (fs.errorMode) rej(new Error())
else {
const result = Object.prototype.hasOwnProperty.call(mockContent, path)
? mockContent[path]
: MASTER_DETAIL_TAG
fs.promises.readdir = jest.fn(
directoryPath =>
new Promise(res => {
const result = mockFiles[path.basename(directoryPath)] ?? []
res(result)
}
})
})
)

fs.promises.readdir = directoryPath =>
new Promise(res => {
const result = mockFiles[path.basename(directoryPath)] ?? []
res(result)
fs.promises.copyFile = jest.fn(() =>
jest.fn(() => {
if (fs.errorMode) return Promise.reject()
return Promise.resolve()
})
)

module.exports = fs
5 changes: 4 additions & 1 deletion __tests__/functional.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
const app = require('../src/main')
const { GIT_FOLDER } = require('../src/utils/gitConstants')
jest.mock('fs')
jest.mock('fs-extra')
jest.mock('child_process')
Expand Down Expand Up @@ -29,8 +30,10 @@ const lines = [

describe(`test if the appli`, () => {
beforeEach(() => {
fs.errorMode = false
fs.__setMockFiles({
output: '',
[GIT_FOLDER]: '',
'.': '',
})
})
Expand All @@ -39,7 +42,7 @@ describe(`test if the appli`, () => {
expect(
await app({
output: 'output',
repo: 'repo/path',
repo: '.',
source: '',
to: 'test',
apiVersion: '46',
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration/main.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
const app = require('../../src/main')
const { GIT_FOLDER } = require('../../src/utils/gitConstants')

jest.mock('child_process')
jest.mock('fs-extra')
Expand All @@ -23,6 +24,7 @@ describe(`test if the appli`, () => {
fseMocked.outputFileError = false
fsMocked.__setMockFiles({
output: '',
[GIT_FOLDER]: '',
'.': '',
'.forceignore': '',
'.forceinclude': '',
Expand Down
101 changes: 66 additions & 35 deletions __tests__/unit/lib/service/customObjectHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const CustomObjectHandler = require('../../../../src/service/customObjectHandler')
const metadataManager = require('../../../../src/metadata/metadataManager')
const { MASTER_DETAIL_TAG } = require('../../../../src/utils/metadataConstants')
const { copy } = require('fs-extra')
const fse = require('fs-extra')
jest.mock('fs')
jest.mock('fs-extra')

Expand Down Expand Up @@ -32,48 +32,79 @@ const testContext = {
}

// eslint-disable-next-line no-undef
describe('test CustomObjectHandler with fields', () => {
describe('customObjectHandler', () => {
let globalMetadata
beforeEach(async () => {
jest.clearAllMocks()
require('fs').__setMockFiles({
'force-app/main/default/objects/Account/Account.object-meta.xml': 'test',
'force-app/main/default/objects/Account/fields': '',
'force-app/main/default/objects/Account/fields/test__c.field-meta.xml':
MASTER_DETAIL_TAG,
'force-app/main/default/objects/Test/Account/Account.object-meta.xml':
'test',
'force-app/main/default/objects/Test/Account/fields/no':
MASTER_DETAIL_TAG,
})
beforeAll(async () => {
globalMetadata = await metadataManager.getDefinition('directoryName', 50)
})
// eslint-disable-next-line no-undef
describe('test CustomObjectHandler with fields', () => {
beforeEach(async () => {
jest.clearAllMocks()
require('fs').__setMockFiles({
'force-app/main/default/objects/Account/Account.object-meta.xml':
'test',
'force-app/main/default/objects/Account/fields': '',
'force-app/main/default/objects/Account/fields/test__c.field-meta.xml':
MASTER_DETAIL_TAG,
'force-app/main/default/objects/Test/Account/Account.object-meta.xml':
'test',
'force-app/main/default/objects/Test/Account/fields/no':
MASTER_DETAIL_TAG,
})
})

test('addition and masterdetail fields', async () => {
const handler = new testContext.handler(
'A force-app/main/default/objects/Account/Account.object-meta.xml',
'objects',
testContext.work,
globalMetadata
)
await handler.handle()
expect(copy).toBeCalled()
test('addition and masterdetail fields', async () => {
const handler = new testContext.handler(
'A force-app/main/default/objects/Account/Account.object-meta.xml',
'objects',
testContext.work,
globalMetadata
)
await handler.handle()
expect(fse.copy).toBeCalled()
})

// eslint-disable-next-line no-undef
testHandlerHelper(testContext)
})

// eslint-disable-next-line no-undef
testHandlerHelper(testContext)
})
describe('test CustomObjectHandler without fields', () => {
beforeEach(async () => {
fse.pathShouldExist = false
require('fs').__setMockFiles({
'force-app/main/default/objects/Account/Account.object-meta.xml':
'test',
'force-app/main/default/objects/Test/Account/Account.object-meta.xml':
'test',
})
})

// eslint-disable-next-line no-undef
describe('test CustomObjectHandler without fields', () => {
beforeAll(async () => {
require('fs').__setMockFiles({
'force-app/main/default/objects/Account/Account.object-meta.xml': 'test',
'force-app/main/default/objects/Test/Account/Account.object-meta.xml':
'test',
// eslint-disable-next-line no-undef
testHandlerHelper(testContext)

test('addition', async () => {
const handler = new testContext.handler(
'A force-app/main/default/objects/Account/Account.object-meta.xml',
'objects',
testContext.work,
globalMetadata
)
await handler.handle()
expect(testContext.work.diffs.package).toHaveProperty('objects')
})
})

// eslint-disable-next-line no-undef
testHandlerHelper(testContext)
test('addition and do not generate delta', async () => {
testContext.work.config.generateDelta = false
const handler = new testContext.handler(
'A force-app/main/default/objects/Account/Account.object-meta.xml',
'objects',
testContext.work,
globalMetadata
)
await handler.handle()
expect(testContext.work.diffs.package).toHaveProperty('objects')
})
})
})
7 changes: 2 additions & 5 deletions __tests__/unit/lib/service/inFileHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe(`test if inFileHandler`, () => {
)

fileGitDiff.mockImplementation(() =>
xmlContent.split(EOL).map(x => `${PLUS} ${x}`)
xmlContent.split(EOL).map((x, i) => (i % 2 ? `${PLUS} ${x}` : x))
)

await handler.handle()
Expand Down Expand Up @@ -169,10 +169,7 @@ describe(`test if inFileHandler`, () => {
})

test('modification without delta generation', async () => {
const work = {
config: { output: '', repo: '', generateDelta: false },
diffs: { package: {}, destructiveChanges: {} },
}
work.config.generateDelta = false
const handler = new testContext.handler(
`M ${changePath}`,
expectedType,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/lib/service/inFolderHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ testHandlerHelper({
],
],
work: {
config: { output: '', repo: './repo', generateDelta: true },
config: { output: '', repo: './repo', generateDelta: false },
diffs: { package: {}, destructiveChanges: {} },
warnings: [],
},
Expand Down
Loading