Skip to content

Commit

Permalink
🐛 Fix all cli workflows (#101)
Browse files Browse the repository at this point in the history
- Extract package.json reading logic from core (#43)
- Add tests e2e (#87)
  • Loading branch information
frinyvonnick authored Jul 10, 2019
1 parent 55e24c2 commit e74919a
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 164 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cache: yarn
script:
- yarn lint
- yarn test
- yarn test:e2e
3 changes: 3 additions & 0 deletions jest.config-e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(e2e).[jt]s?(x)"],
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"homepage": "https://github.com/frinyvonnick/gitmoji-changelog#readme",
"scripts": {
"test": "jest",
"test:e2e": "jest --config ./jest.config-e2e.js",
"lint": "eslint packages/**/src"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/gitmoji-changelog-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"inquirer": "^6.3.1",
"libnpm": "^1.0.0",
"lodash": "^4.17.11",
"semver": "^5.6.0",
"semver-compare": "^1.0.0",
"simple-git": "^1.113.0",
"yargs": "^12.0.1"
Expand Down
343 changes: 343 additions & 0 deletions packages/gitmoji-changelog-cli/src/cli.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
const os = require('os')
const fs = require('fs')
const { removeSync } = require('fs-extra')
const path = require('path')
const simpleGit = require('simple-git/promise')
const childProcess = require('child_process')

expect.extend({
includes(str, items) {
const pass = items.every(item => str.includes(item))
return {
pass,
message: () => pass
? `Expected ${str} to not includes ${items.join(', ')}`
: `Expected ${str} to includes ${items.join(', ')}`,
}
},
})

expect.extend({
toDisplayError(str) {
const pass = str.includes('Error')
return {
pass,
message: () => pass ? 'It passes' : `Expected ${str} to includes Error`,
}
},
})

describe('generate changelog', () => {
let testDir
let repo

beforeEach(async () => {
testDir = path.join(os.tmpdir(), 'gitmoji-changelog')
if (fs.existsSync(testDir)) removeSync(testDir)
fs.mkdirSync(testDir)
repo = simpleGit(testDir)
await repo.init()
fs.copyFileSync(
path.join(__dirname, '..', '..', '..', 'misc', 'package.sample.json'),
path.join(testDir, 'package.json'),
)
})

afterEach(() => {
removeSync(testDir)
})

describe('init', () => {
it("should get a 1.0.0 version while initializing changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')

expect(getChangelog()).includes(['1.0.0'])
})

it("should get a 1.0.0 version while initializing changelog by calling cli with 1.0.0 and having package.json's version set to 0.0.1", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
gitmojiChangelog('1.0.0')
await commit(':bookmark: Version 1.0.0')

expect(getChangelog()).includes(['1.0.0'])
})

it("should get a next version while initializing changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', 'next'])
})

it("should get two versions 1.0.0 and 2.0.0 while initializing changelog by calling cli without and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 2.0.0')

expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
})

describe('update', () => {
it("should get two versions 1.0.0 and next while updating changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', 'next'])
})
it("should get two versions 1.0.0 and 2.0.0 while updating changelog by calling cli without arguments and having package.json's version set to 2.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
it("should get two versions 1.0.0 and 2.0.0 while updating changelog by calling cli with 2.0.0 and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('2.0.0')

expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
it('should get three versions 1.0.0, 2.0.0, 3.0.0 while updating changelog by calling cli without arguments and skipping two tags creation 2.0.0 and 3.0.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')

await makeChanges('file3')
await commit(':sparkles: Add a third file')
await bumpVersion('3.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 3.0.0')
await tag('3.0.0')

expect(getChangelog()).includes(['1.0.0', '2.0.0', '3.0.0'])
})

it('should get three versions 1.0.0, 2.0.0, 3.0.0 and next while updating changelog by calling cli without arguments and skipping two tags creation 2.0.0 and 3.0.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')

await makeChanges('file3')
await commit(':sparkles: Add a third file')
await bumpVersion('3.0.0')
await commit(':bookmark: Version 3.0.0')
await tag('3.0.0')

await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', '2.0.0', '3.0.0', 'next'])
})

it('should get two versions 1.0.0, 2.0.0 and next while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')

await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', '2.0.0', 'next'])
})

it('shouldn\'t generate changelog when gimoji-changelog if there isn\'t any changes', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
const output = gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0'])
expect(output.toString('utf8')).toDisplayError()
})

it('should get two versions 1.0.0 and next after two generation while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()

await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()

expect(getChangelog()).includes(['1.0.0', 'next'])
})

it('should get two versions 1.0.0 and 1.1.0 after three generations while updating changelog by calling cli with version 1.1.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()

await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog('1.1.0')

expect(getChangelog()).includes(['1.0.0', '1.1.0'])
})

it('should get two versions 1.0.0 and next after three generations while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('1.1.0')
gitmojiChangelog()

expect(getChangelog()).not.includes(['1.1.0'])
expect(getChangelog()).includes(['1.0.0', 'next'])
})

it('should get two versions 1.0.0 and 1.2.0 after three generations while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')

await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('1.1.0')
gitmojiChangelog('1.2.0')

expect(getChangelog()).not.includes(['1.1.0'])
expect(getChangelog()).includes(['1.0.0', '1.2.0'])
})

it('should display an error if requested version isn\'t semver', async () => {
const output = gitmojiChangelog('awesomeversion')

expect(output.toString('utf8')).toDisplayError()
})
})

async function makeChanges(fileName) {
fs.writeFileSync(path.join(testDir, fileName))
}

async function commit(message) {
await repo.add('.')
await repo.commit(message)
}

async function tag(version) {
await repo.addTag(`v${version}`)
}

function gitmojiChangelog(args = []) {
if (!Array.isArray(args)) {
// eslint-disable-next-line no-param-reassign
args = [args]
}
return childProcess.execFileSync('node', [path.join(__dirname, 'index.js'), ...args], { cwd: testDir })
}

function getChangelog() {
return fs.readFileSync(path.join(testDir, 'CHANGELOG.md')).toString('utf8')
}

function bumpVersion(to) {
const pkg = path.join(testDir, 'package.json')
// eslint-disable-next-line global-require
const content = fs.readFileSync(pkg).toString('utf8')
const { version } = JSON.parse(content)
const updatedContent = content.replace(version, to)
fs.writeFileSync(pkg, updatedContent)
}
})
Loading

0 comments on commit e74919a

Please sign in to comment.