Skip to content

Commit

Permalink
feat: add dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 15, 2018
1 parent 5ca9d42 commit b2b0b84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LOG_PACKS_SH = resolve('./log_modified_packs.sh')
const COUNT_PACKS_SH = resolve('./count_modified_packs.sh')
const DROP_TAG_SH = resolve('./drop_last_tag.sh')

module.exports = function () {
module.exports = function (dryRun) {
const exec = cp.execSync
const isFirstRun = !fs.existsSync(COUNT)

Expand All @@ -25,24 +25,26 @@ module.exports = function () {
log('is last run:', isLastRun)
log('packages left:', left)

if (count > 0) {
try {
if (isFirstRun) {
log(exec(`sh ${LOG_PACKS_SH}`))
} else {
tag = exec(`sh ${DROP_TAG_SH}`)
log('drop tag', tag)
if (!dryRun) {
if (count > 0) {
try {
if (isFirstRun) {
log(exec(`sh ${LOG_PACKS_SH}`))
} else {
tag = exec(`sh ${DROP_TAG_SH}`)
log('drop tag', tag)
}

count -= 1
fs.writeFileSync(COUNT, count + '')
} catch (err) {
log('[error]:', err)
}

count -= 1
fs.writeFileSync(COUNT, count + '')
} catch (err) {
log('[error]:', err)
}
}

if (isLastRun && !isFirstRun) {
fs.unlinkSync(COUNT)
if (isLastRun && !isFirstRun) {
fs.unlinkSync(COUNT)
}
}

return {
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ describe('lib', () => {
mockFs.restore()
})

it('does nothing if no changes found', () => {
exec
.mockReturnValueOnce(0)

const res = rh()

expect(res.isFirstRun).toBeTruthy()
expect(res.isLastRun).toBeTruthy()
expect(res.packagesLeft).toBe(0)
expect(exec).toHaveBeenCalledWith(`sh ${path.resolve(__dirname, '../src/count_modified_packs.sh')}`)
})

it('detects the first run, gets modified packages count from `sh`, stores data to temp file', () => {
exec
.mockReturnValueOnce(10)
Expand Down Expand Up @@ -52,6 +64,19 @@ describe('lib', () => {
expect(fs.readFileSync(PATH, {encoding: 'utf8'})).toBe('9')
})

it('when `dryRun` passed handler works as analyser', () => {
mockFs({[PATH]: '10'})

const res = rh(true)

expect(res.isFirstRun).toBeFalsy()
expect(res.packagesLeft).toBe(9)
expect(res.droppedTag).toBeNull()
expect(exec).not.toHaveBeenCalled()

expect(fs.readFileSync(PATH, {encoding: 'utf8'})).toBe('10')
})

it('unlinks tempfile on the last run', () => {
mockFs({[PATH]: '1'})

Expand Down

0 comments on commit b2b0b84

Please sign in to comment.