-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e24c2
commit e74919a
Showing
13 changed files
with
520 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ cache: yarn | |
script: | ||
- yarn lint | ||
- yarn test | ||
- yarn test:e2e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |
Oops, something went wrong.