Skip to content

Commit

Permalink
feat: prepare i18n (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Apr 21, 2022
1 parent 9a038db commit 001c07e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 52 deletions.
19 changes: 11 additions & 8 deletions __tests__/unit/lib/utils/cliHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
)
})

Expand All @@ -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)
)
})

Expand All @@ -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)
)
})

Expand All @@ -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)
)
})

Expand All @@ -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)
)
})

Expand All @@ -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)
)
})
})
70 changes: 53 additions & 17 deletions __tests__/unit/lib/utils/repoSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
})
})
})
10 changes: 10 additions & 0 deletions src/locales/en.js
Original file line number Diff line number Diff line change
@@ -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',
}
59 changes: 32 additions & 27 deletions src/utils/cliHelper.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -74,24 +77,26 @@ 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()
errors.push(...gitErrors)

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) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/repoSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

0 comments on commit 001c07e

Please sign in to comment.