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

feat: verify --from and --to parameters to be valid git pointer #248

Merged
merged 4 commits into from
Mar 30, 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
36 changes: 30 additions & 6 deletions __tests__/functional.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'
const fs = require('fs')
const child_process = require('child_process')
const app = require('../src/main')
const { GIT_FOLDER } = require('../src/utils/gitConstants')
const { COMMIT_REF_TYPE, GIT_FOLDER } = require('../src/utils/gitConstants')
jest.mock('fs')
jest.mock('fs-extra')
jest.mock('child_process')
const fs = require('fs')
const child_process = require('child_process')

const lines = [
'D force-app/main/default/objects/Account/fields/deleted.field-meta.xml',
Expand Down Expand Up @@ -38,39 +38,63 @@ describe(`test if the appli`, () => {
})
})
test('can execute with rich parameters and big diff', async () => {
child_process.__setOutput([lines, [], ['firstSHA']])
child_process.__setOutput([
lines,
[],
[],
[COMMIT_REF_TYPE],
[COMMIT_REF_TYPE],
[],
])
expect(
await app({
output: 'output',
repo: '.',
source: '',
to: 'test',
from: 'main',
apiVersion: '46',
})
).toHaveProperty('warnings', [])
})

test('catch internal warnings', async () => {
fs.errorMode = true
child_process.__setOutput([lines, [], ['firstSHA']])
child_process.__setOutput([
lines,
[],
[],
[],
[COMMIT_REF_TYPE],
[COMMIT_REF_TYPE],
])
const work = await app({
output: 'output',
repo: '',
source: '',
to: 'HEAD',
from: 'main',
apiVersion: '46',
generateDelta: true,
})
expect(work.warnings).not.toHaveLength(0)
})

test('do not generate destructiveChanges.xml and package.xml with same element', async () => {
child_process.__setOutput([lines, [], ['firstSHA']])
child_process.__setOutput([
lines,
[],
[],
[],
[COMMIT_REF_TYPE],
[COMMIT_REF_TYPE],
])
const work = await app({
output: 'output',
repo: '',
source: '',
to: 'HEAD',
from: 'main',
apiVersion: '46',
generateDelta: true,
})
Expand Down
17 changes: 3 additions & 14 deletions __tests__/integration/main.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const app = require('../../src/main')
const { GIT_FOLDER } = require('../../src/utils/gitConstants')
const { COMMIT_REF_TYPE, GIT_FOLDER } = require('../../src/utils/gitConstants')

jest.mock('child_process')
jest.mock('fs-extra')
Expand All @@ -14,6 +14,7 @@ const testConfig = {
repo: '',
source: '',
to: 'test',
from: 'main',
apiVersion: '46',
}

Expand All @@ -32,7 +33,7 @@ describe(`test if the appli`, () => {
})

beforeEach(() => {
child_process.__setOutput([])
child_process.__setOutput([[], [COMMIT_REF_TYPE], [COMMIT_REF_TYPE], []])
child_process.__setError(false)
})

Expand Down Expand Up @@ -93,16 +94,4 @@ describe(`test if the appli`, () => {
})
).toHaveProperty('warnings', [])
})

test('catch and reject big issues', async () => {
fsMocked.errorMode = true
fseMocked.errorMode = true
fseMocked.outputFileError = true
child_process.__setError(true)
try {
await app(testConfig)
} catch (error) {
expect(error).toBeDefined()
}
})
})
179 changes: 86 additions & 93 deletions __tests__/unit/lib/utils/cliHelper.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict'

jest.mock('../../../../src/utils/repoSetup')
const RepoSetup = require('../../../../src/utils/repoSetup')
RepoSetup.mockImplementation(() => {
return {
isToEqualHead: jest.fn(),
repoConfiguration: jest.fn(),
computeFromRef: jest.fn(),
}
})
jest.mock('fs')

const child_process = require('child_process')
const fs = require('fs')
const { COMMIT_REF_TYPE } = require('../../../../src/utils/gitConstants')
const RepoSetup = require('../../../../src/utils/repoSetup')
const CLIHelper = require('../../../../src/utils/cliHelper')
jest.mock('fs')
jest.mock('child_process')
jest.mock('../../../../src/utils/repoSetup')
RepoSetup.mockImplementation(() => ({
isToEqualHead: jest.fn(),
repoConfiguration: jest.fn(),
}))

const testConfig = {
output: 'output',
Expand All @@ -32,30 +30,16 @@ describe(`test if the application`, () => {
})
})

beforeEach(() => {
jest.resetAllMocks()
})

test('throws errors when to parameter is not filled', async () => {
let cliHelper = new CLIHelper({ ...testConfig, to: undefined })
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when apiVersion parameter is NaN', async () => {
let cliHelper = new CLIHelper({ ...testConfig, apiVersion: 'NotANumber' })
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when fs.stat throw error', async () => {
Expand All @@ -65,13 +49,8 @@ describe(`test if the application`, () => {
to: undefined,
output: 'stat/error',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when output folder does not exist', async () => {
Expand All @@ -80,94 +59,59 @@ describe(`test if the application`, () => {
to: undefined,
output: 'not/exist/folder',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when output is not a folder', async () => {
let cliHelper = new CLIHelper({ ...testConfig, output: 'file' })
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when repo is not git repository', async () => {
let cliHelper = new CLIHelper({
...testConfig,
repo: 'not/git/folder',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when file is not found for --ignore', async () => {
let cliHelper = new CLIHelper({
...testConfig,
ignore: 'not-a-file',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when file is not found for --ignore-destructive', async () => {
let cliHelper = new CLIHelper({
...testConfig,
ignoreDestructive: 'not-a-file',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when file is not found for --include', async () => {
let cliHelper = new CLIHelper({
...testConfig,
include: 'not-a-file',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when file is not found for --include-destructive', async () => {
let cliHelper = new CLIHelper({
...testConfig,
includeDestructive: 'not-a-file',
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when "-t" and "-d" are set', async () => {
Expand All @@ -177,12 +121,61 @@ describe(`test if the application`, () => {
to: notHeadSHA,
generateDelta: true,
})
try {
await cliHelper.validateConfig()
} catch (e) {
expect(e).toBeDefined()
return
}
expect(true).toBe(false)
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow()
})

test('throws errors when "-t" is not a git expression', async () => {
const emptyString = ''
let cliHelper = new CLIHelper({
...testConfig,
to: emptyString,
generateDelta: false,
})
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow(
`--to is blank: '${emptyString}'`
)
})

test('throws errors when "-f" is not a git expression', async () => {
const emptyString = ''
let cliHelper = new CLIHelper({
...testConfig,
from: emptyString,
generateDelta: false,
})
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow(
`--from is blank: '${emptyString}'`
)
})

test('throws errors when "-t" is not a valid sha pointer', async () => {
child_process.__setOutput([[COMMIT_REF_TYPE], ['not a valid sha pointer']])
const notHeadSHA = 'test'
let cliHelper = new CLIHelper({
...testConfig,
to: notHeadSHA,
generateDelta: false,
})
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow(
`--to is not a valid sha pointer: '${notHeadSHA}'`
)
})

test('throws errors when "-f" is not a valid sha pointer', async () => {
child_process.__setOutput([['not a valid sha pointer'], [COMMIT_REF_TYPE]])
const notHeadSHA = 'test'
let cliHelper = new CLIHelper({
...testConfig,
from: notHeadSHA,
generateDelta: false,
})
expect.assertions(1)
await expect(cliHelper.validateConfig()).rejects.toThrow(
`--from is not a valid sha pointer: '${notHeadSHA}'`
)
})
})
Loading