diff --git a/__tests__/unit/lib/utils/cliHelper.test.js b/__tests__/unit/lib/utils/cliHelper.test.js index 3ab16cb5..8c1a0d95 100644 --- a/__tests__/unit/lib/utils/cliHelper.test.js +++ b/__tests__/unit/lib/utils/cliHelper.test.js @@ -7,12 +7,15 @@ const { } = require('../../../../src/utils/gitConstants') const RepoSetup = require('../../../../src/utils/repoSetup') const CLIHelper = require('../../../../src/utils/cliHelper') +const messages = require('../../../../src/locales/en') +const { format } = require('util') jest.mock('fs') jest.mock('child_process') jest.mock('../../../../src/utils/repoSetup') RepoSetup.mockImplementation(() => ({ isToEqualHead: jest.fn(), repoConfiguration: jest.fn(), + getCommitRefType: jest.fn(), })) const testConfig = { @@ -137,7 +140,7 @@ describe(`test if the application`, () => { }) expect.assertions(1) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--to is blank: '${emptyString}'` + format(messages.errorGitSHAisBlank, 'to', emptyString) ) }) @@ -150,7 +153,7 @@ describe(`test if the application`, () => { }) expect.assertions(1) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--from is blank: '${emptyString}'` + format(messages.errorGitSHAisBlank, 'from', emptyString) ) }) @@ -164,7 +167,7 @@ describe(`test if the application`, () => { }) expect.assertions(1) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--to is not a valid sha pointer: '${notHeadSHA}'` + format(messages.errorParameterIsNotGitSHA, 'to', notHeadSHA) ) }) @@ -178,7 +181,7 @@ describe(`test if the application`, () => { }) expect.assertions(1) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--from is not a valid sha pointer: '${notHeadSHA}'` + format(messages.errorParameterIsNotGitSHA, 'from', notHeadSHA) ) }) @@ -196,10 +199,10 @@ describe(`test if the application`, () => { }) expect.assertions(2) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--to is not a valid sha pointer: '${notHeadSHA}'` + format(messages.errorParameterIsNotGitSHA, 'to', notHeadSHA) ) await expect(cliHelper.validateConfig()).rejects.toThrow( - `--from is not a valid sha pointer: '${notHeadSHA}'` + format(messages.errorParameterIsNotGitSHA, 'from', notHeadSHA) ) }) @@ -213,10 +216,10 @@ describe(`test if the application`, () => { }) expect.assertions(2) await expect(cliHelper.validateConfig()).rejects.not.toThrow( - `--to is not a valid sha pointer: '${COMMIT_REF_TYPE}'` + format(messages.errorParameterIsNotGitSHA, 'to', COMMIT_REF_TYPE) ) await expect(cliHelper.validateConfig()).rejects.not.toThrow( - `--from is not a valid sha pointer: '${TAG_REF_TYPE}'` + format(messages.errorParameterIsNotGitSHA, 'from', TAG_REF_TYPE) ) }) }) diff --git a/__tests__/unit/lib/utils/repoSetup.test.js b/__tests__/unit/lib/utils/repoSetup.test.js index f40aa5e5..ef330dbc 100644 --- a/__tests__/unit/lib/utils/repoSetup.test.js +++ b/__tests__/unit/lib/utils/repoSetup.test.js @@ -4,27 +4,63 @@ jest.mock('child_process') const child_process = require('child_process') describe(`test if repoSetup`, () => { - test('say "to" equal "HEAD"', async () => { - const config = { repo: '.', to: 'HEAD' } - const repoSetup = new RepoSetup(config) - const toEqualHead = await repoSetup.isToEqualHead() + describe('isToEquelHead', () => { + test('say "to" equal "HEAD"', async () => { + const config = { repo: '.', to: 'HEAD' } + const repoSetup = new RepoSetup(config) + const toEqualHead = await repoSetup.isToEqualHead() - expect(toEqualHead).toBe(true) - }) + expect(toEqualHead).toBe(true) + }) + + test('say when "to" do not equals "HEAD"', async () => { + const config = { repo: '.', to: 'not HEAD' } + child_process.__setOutput([['not HEAD'], ['HEAD']]) + const repoSetup = new RepoSetup(config) + const toEqualHead = await repoSetup.isToEqualHead() - test('say when "to" do not equals "HEAD"', async () => { - const config = { repo: '.', to: 'not HEAD' } - child_process.__setOutput([['not HEAD'], ['HEAD']]) - const repoSetup = new RepoSetup(config) - const toEqualHead = await repoSetup.isToEqualHead() + expect(toEqualHead).toBe(false) + }) + }) - expect(toEqualHead).toBe(false) + describe('repoConfiguration', () => { + test('can set core.quotepath to off', async () => { + const config = { repo: '.', from: 'HEAD~1' } + child_process.__setOutput([['']]) + const repoSetup = new RepoSetup(config) + await repoSetup.repoConfiguration() + }) }) - test('can set core.quotepath to off', async () => { - const config = { repo: '.', from: 'HEAD~1' } - child_process.__setOutput([['']]) - const repoSetup = new RepoSetup(config) - await repoSetup.repoConfiguration() + describe('getCommitRefType', () => { + test('returns "commit" when commitRef is a commit', async () => { + const shaRef = 'HEAD' + const config = { repo: '.', to: shaRef } + child_process.__setOutput([['commit']]) + const repoSetup = new RepoSetup(config) + const commitRef = await repoSetup.getCommitRefType(shaRef) + + expect(commitRef).toBe('commit') + }) + + test('returns "tag" when commitRef is a tag', async () => { + const shaRef = 'tag' + const config = { repo: '.', to: shaRef } + child_process.__setOutput([['tag']]) + const repoSetup = new RepoSetup(config) + const commitRef = await repoSetup.getCommitRefType(shaRef) + + expect(commitRef).toBe('tag') + }) + + test('return empty string when commitRef is a not a git sha', async () => { + const shaRef = 'wrong sha' + const config = { repo: '.', to: shaRef } + child_process.__setOutput([['']]) + const repoSetup = new RepoSetup(config) + const commitRef = await repoSetup.getCommitRefType(shaRef) + + expect(commitRef).toBe('') + }) }) }) diff --git a/src/locales/en.js b/src/locales/en.js new file mode 100644 index 00000000..a65a01db --- /dev/null +++ b/src/locales/en.js @@ -0,0 +1,10 @@ +module.exports = { + errorGitSHAisBlank: '--%s is blank: "%s"', + errorParameterIsNotGitSHA: '--%s is not a valid sha pointer: "%s"', + errorAPIVersionIsNan: 'api-version %s is not a number', + errorPathIsNotDir: '%s folder does not exist', + errorPathIsNotFile: '%s file does not exist', + errorPathIsNotGit: '%s is not a git repository', + errorToNotHeadWithDeltaGenerate: + '--generate-delta (-d) parameter cannot be used when --to (-t) parameter is not equivalent to HEAD', +} diff --git a/src/utils/cliHelper.js b/src/utils/cliHelper.js index aabc4710..76991617 100644 --- a/src/utils/cliHelper.js +++ b/src/utils/cliHelper.js @@ -1,9 +1,10 @@ 'use strict' const asyncFilter = require('./asyncFilter') +const messages = require('../locales/en') const RepoSetup = require('./repoSetup') -const { sanitizePath, getStreamContent } = require('./childProcessUtils') +const { sanitizePath } = require('./childProcessUtils') const { GIT_FOLDER, POINTER_REF_TYPES } = require('./gitConstants') -const { spawn } = require('child_process') +const { format } = require('util') const { stat } = require('fs').promises const { join } = require('path') @@ -24,10 +25,10 @@ const isGit = async dir => { return await dirExists(join(dir, GIT_FOLDER)) } -const commitCheckParams = ['cat-file', '-t'] - const isBlank = str => !str || /^\s*$/.test(str) +const GIT_SHA_PARAMETERS = ['to', 'from'] + class CLIHelper { constructor(config) { this.config = config @@ -37,24 +38,26 @@ class CLIHelper { async _validateGitSha() { const errors = [] await Promise.all( - ['to', 'from'] - .filter( - field => - !isBlank(this.config[field]) || - errors.push(`--${field} is blank: '${this.config[field]}'`) - ) - .map(async field => { - const refType = await getStreamContent( - spawn('git', [...commitCheckParams, this.config[field]], { - cwd: this.config.repo, - }) + GIT_SHA_PARAMETERS.filter( + field => + !isBlank(this.config[field]) || + !errors.push( + format(messages.errorGitSHAisBlank, field, this.config[field]) ) - if (!POINTER_REF_TYPES.includes(refType?.replace(/\s/g, ''))) { - errors.push( - `--${field} is not a valid sha pointer: '${this.config[field]}'` + ).map(async field => { + const refType = await this.repoSetup.getCommitRefType( + this.config[field] + ) + if (!POINTER_REF_TYPES.includes(refType?.replace(/\s/g, ''))) { + errors.push( + format( + messages.errorParameterIsNotGitSHA, + field, + this.config[field] ) - } - }) + ) + } + }) ) return errors @@ -65,7 +68,7 @@ class CLIHelper { const errors = [] if (isNaN(this.config.apiVersion)) { - errors.push(`api-version ${this.config.apiVersion} is not a number`) + errors.push(format(messages.errorAPIVersionIsNan, this.config.apiVersion)) } const isGitPromise = isGit(this.config.repo) @@ -74,14 +77,18 @@ class CLIHelper { const filesPromise = this._filterFiles() const directories = await directoriesPromise - directories.forEach(dir => errors.push(`${dir} folder does not exist`)) + directories.forEach(dir => + errors.push(format(messages.errorPathIsNotDir, dir)) + ) const files = await filesPromise - files.forEach(file => errors.push(`${file} file does not exist`)) + files.forEach(file => + errors.push(format(messages.errorPathIsNotFile, file)) + ) const isGitRepo = await isGitPromise if (!isGitRepo) { - errors.push(`${this.config.repo} is not a git repository`) + errors.push(format(messages.errorPathIsNotGit, this.config.repo)) } const gitErrors = await this._validateGitSha() @@ -89,9 +96,7 @@ class CLIHelper { const isToEqualHead = await isToEqualHeadPromise if (!isToEqualHead && this.config.generateDelta) { - errors.push( - `--generate-delta (-d) parameter cannot be used when --to (-t) parameter is not equivalent to HEAD` - ) + errors.push(messages.errorToNotHeadWithDeltaGenerate) } if (errors.length > 0) { diff --git a/src/utils/repoSetup.js b/src/utils/repoSetup.js index 78e5ccd9..b8582bdf 100644 --- a/src/utils/repoSetup.js +++ b/src/utils/repoSetup.js @@ -5,6 +5,7 @@ const { spawn } = require('child_process') const HEAD = 'HEAD' const revparseParams = ['rev-parse'] +const commitCheckParams = ['cat-file', '-t'] const gitConfig = ['config', 'core.quotepath', 'off'] class RepoSetup { @@ -34,6 +35,14 @@ class RepoSetup { async repoConfiguration() { await getStreamContent(spawn('git', gitConfig, this.spawnConfig)) } + + async getCommitRefType(commitRef) { + return await getStreamContent( + spawn('git', [...commitCheckParams, commitRef], { + cwd: this.config.repo, + }) + ) + } } module.exports = RepoSetup