Skip to content

Commit

Permalink
test: add test for the new output behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Aug 1, 2021
1 parent a50949a commit 1f419a9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 53 deletions.
53 changes: 0 additions & 53 deletions __tests__/integration/main.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const app = require('../../src/main')
const gc = require('../../src/utils/gitConstants')

const child_process = require('child_process')
jest.mock('child_process', () => ({ spawnSync: jest.fn() }))
Expand Down Expand Up @@ -97,56 +96,4 @@ describe(`test if the appli`, () => {
app(testConfig)
}).toThrow()
})

test('throw errors when to parameter is not filled', () => {
expect(() => {
app({ ...testConfig, to: undefined })
}).toThrow()
})

test('throw errors when apiVersion parameter is NaN', () => {
expect(() => {
app({ ...testConfig, apiVersion: 'NotANumber' })
}).toThrow()
})

test('throw errors when output folder does not exist', () => {
expect(() => {
app({
...testConfig,
output: 'not/exist/folder',
})
}).toThrow()
})

test('throw errors when output is not a folder', () => {
expect(() => {
app({ ...testConfig, output: 'file' })
}).toThrow()
})

test('throw errors when repo is not git repository', () => {
expect(() => {
app({
...testConfig,
repo: 'not/git/folder',
})
}).toThrow()
})

test('throw errors when "-t" and "-d" are set', () => {
const notHeadSHA = 'test'
child_process.spawnSync
.mockReturnValueOnce({ stdout: Buffer.from('HEAD', gc.UTF8_ENCODING) })
.mockReturnValueOnce({
stdout: Buffer.from(notHeadSHA, gc.UTF8_ENCODING),
})
expect(() => {
app({
...testConfig,
to: notHeadSHA,
generateDelta: true,
})
}).toThrow()
})
})
104 changes: 104 additions & 0 deletions __tests__/unit/lib/utils/cliHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict'
const CLIHelper = require('../../../../src/utils/cliHelper')
const gc = require('../../../../src/utils/gitConstants')

const child_process = require('child_process')
jest.mock('child_process', () => ({ spawnSync: jest.fn() }))
jest.mock('fs-extra')
jest.mock('fs')
jest.mock('git-state')
jest.mock('fast-xml-parser')

const fsMocked = require('fs')
const fseMocked = require('fs-extra')

const testConfig = {
output: 'output',
repo: '',
source: '',
to: 'test',
apiVersion: '46',
}

describe(`test if the appli`, () => {
let cliHelper
beforeAll(() => {
fsMocked.errorMode = false
fseMocked.errorMode = false
fseMocked.outputFileSyncError = false
fsMocked.__setMockFiles({
output: '',
'.': '',
})
child_process.spawnSync.mockImplementation(() => ({ stdout: '' }))
})

test('throw errors when to parameter is not filled', () => {
cliHelper = new CLIHelper({ ...testConfig, to: undefined })
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})

test('throw errors when apiVersion parameter is NaN', () => {
cliHelper = new CLIHelper({ ...testConfig, apiVersion: 'NotANumber' })
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})

test('throw errors when output folder does not exist', () => {
cliHelper = new CLIHelper({ ...testConfig, to: undefined })
expect(() => {
cliHelper.validateConfig({
...testConfig,
output: 'not/exist/folder',
})
}).toThrow()
})

test('throw errors when output is not a folder', () => {
cliHelper = new CLIHelper({ ...testConfig, output: 'file' })
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})

test('throw errors when repo is not git repository', () => {
cliHelper = new CLIHelper({
...testConfig,
repo: 'not/git/folder',
})
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})

test('throw errors when "-t" and "-d" are set', () => {
const notHeadSHA = 'test'
child_process.spawnSync
.mockReturnValueOnce({ stdout: Buffer.from('HEAD', gc.UTF8_ENCODING) })
.mockReturnValueOnce({
stdout: Buffer.from(notHeadSHA, gc.UTF8_ENCODING),
})
cliHelper = new CLIHelper({
...testConfig,
to: notHeadSHA,
generateDelta: true,
})
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})

test('throw errors when "-d" is set and "-r" equals "-o', () => {
cliHelper = new CLIHelper({
...testConfig,
output: '',
generateDelta: true,
})
expect(() => {
cliHelper.validateConfig()
}).toThrow()
})
})

0 comments on commit 1f419a9

Please sign in to comment.