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 diff exit code #68

Merged
merged 1 commit into from
Jan 16, 2020
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
32 changes: 15 additions & 17 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ export const builder = (args: Argv): Argv<DiffOptions> => {
})
.fail((msg, err) => {
if (msg) {
// TODO: should refactor console message
console.error(msg)
process.exit(1)
} else {
if (err instanceof DiffError) {
// TODO: should refactor console message
console.warn(err.message)
process.exit(1)
process.exit(64)
} else {
if (err) throw err
// TODO: should refactor console message
console.error(err.message)
process.exit(1)
}
}
})
Expand All @@ -83,37 +87,31 @@ export const handler = async (args: Arguments<DiffOptions>): Promise<unknown> =>
const ProviderFactory = loadProvider(args.provider)

if (ProviderFactory === null) {
// TODO: should refactor console message
console.log(`Not found ${args.provider} provider`)
return
throw new Error(`Not found ${args.provider} provider`)
}

if (!args.target && !args.targetPaths) {
// TODO: should refactor console message
console.log('You need to specify either --target or --target-paths')
return
throw new Error('You need to specify either --target or --target-paths')
}

const confPath = resolveProviderConf(args.provider, args.conf)
const conf = loadProviderConf(confPath) || DEFUALT_CONF

let localeMessages
try {
localeMessages = getLocaleMessages(args)
} catch (e) {
console.log(e.message)
return
}
const localeMessages = getLocaleMessages(args)

const provider = ProviderFactory(conf)
const locales = Object.keys(localeMessages) as Locale[]
const serviceMessages = await provider.pull({ locales, dryRun: false, normalize, format })

const ret = diffString(localeMessages, serviceMessages)
console.log(ret)

return !ret
? Promise.reject(new DiffError('There are differences!'))
: Promise.resolve('No difference!')
if (ret) {
throw new DiffError('There are differences!')
}

return Promise.resolve()
}

export default {
Expand Down
16 changes: 14 additions & 2 deletions test/commands/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ const PROCESS_CWD_TARGET_PATH = path.resolve(__dirname)
let orgCwd // for process.cwd mock
let orgExit // for process.exit mock
let spyLog
let spyWarn
let spyError
beforeEach(() => {
spyLog = jest.spyOn(global.console, 'log')
spyWarn = jest.spyOn(global.console, 'warn')
spyError = jest.spyOn(global.console, 'error')
orgCwd = process.cwd
process.cwd = jest.fn(() => PROCESS_CWD_TARGET_PATH) // mock: process.cwd
Expand All @@ -38,6 +40,7 @@ beforeEach(() => {

afterEach(() => {
spyError.mockRestore()
spyWarn.mockRestore()
spyLog.mockRestore()
jest.clearAllMocks()
process.exit = orgExit
Expand Down Expand Up @@ -72,7 +75,7 @@ test('--provider: not found', async () => {
})

// verify
expect(spyLog).toHaveBeenCalledWith('Not found ./404-provider.js provider')
expect(spyError).toHaveBeenCalledWith('Not found ./404-provider.js provider')
})

test('not specified --target and --targetPaths', async () => {
Expand All @@ -86,7 +89,7 @@ test('not specified --target and --targetPaths', async () => {
})

// verify
expect(spyLog).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
expect(spyError).toHaveBeenCalledWith('You need to specify either --target or --target-paths')
})

test('--target option', async () => {
Expand All @@ -111,6 +114,9 @@ test('--target option', async () => {

// verify with snapshot
expect(spyLog.mock.calls[0][0]).toMatchSnapshot()
// NOTE: cannot detect process.exit calling in `fail` ...
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
// expect(process.exit).toHaveBeenCalledWith(64)
})

test('--locale option', async () => {
Expand All @@ -135,6 +141,9 @@ test('--locale option', async () => {

// verify with snapshot
expect(spyLog.mock.calls[0][0]).toMatchSnapshot()
// NOTE: cannot detect process.exit calling in `fail` ...
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
// expect(process.exit).toHaveBeenCalledWith(64)
})

test('--target-paths option', async () => {
Expand All @@ -159,4 +168,7 @@ test('--target-paths option', async () => {

// verify with snapshot
expect(spyLog.mock.calls[1][0]).toMatchSnapshot()
// NOTE: cannot detect process.exit calling in `fail` ...
// expect(spyWarn).toHaveBeenCalledWith('There are differences!')
// expect(process.exit).toHaveBeenCalledWith(64)
})