|
| 1 | +import path from 'path'; |
1 | 2 | import test from 'ava'; |
2 | 3 | import {outputFile, readFile} from 'fs-extra'; |
3 | 4 | import {stub} from 'sinon'; |
4 | 5 | import tempy from 'tempy'; |
5 | 6 | import prepare from '../lib/prepare'; |
6 | 7 |
|
7 | | -// Save the current working diretory |
8 | | -const cwd = process.cwd(); |
9 | | - |
10 | 8 | test.beforeEach(t => { |
11 | | - // Change current working directory to a temp directory |
12 | | - process.chdir(tempy.directory()); |
13 | 9 | // Stub the logger |
14 | 10 | t.context.log = stub(); |
15 | 11 | t.context.logger = {log: t.context.log}; |
16 | 12 | }); |
17 | 13 |
|
18 | | -test.afterEach.always(() => { |
19 | | - // Restore the current working directory |
20 | | - process.chdir(cwd); |
21 | | -}); |
22 | | - |
23 | | -test.serial('Create new CHANGELOG.md', async t => { |
| 14 | +test('Create new CHANGELOG.md', async t => { |
| 15 | + const cwd = tempy.directory(); |
24 | 16 | const notes = 'Test release note'; |
| 17 | + const changelogFile = 'CHANGELOG.md'; |
| 18 | + const changelogPath = path.resolve(cwd, changelogFile); |
25 | 19 |
|
26 | | - await prepare({}, notes, t.context.logger); |
| 20 | + await prepare({}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
27 | 21 |
|
28 | 22 | // Verify the content of the CHANGELOG.md |
29 | | - t.is((await readFile('CHANGELOG.md')).toString(), `${notes}\n`); |
| 23 | + t.is((await readFile(changelogPath)).toString(), `${notes}\n`); |
30 | 24 |
|
31 | | - t.deepEqual(t.context.log.args[0], ['Create %s', 'CHANGELOG.md']); |
| 25 | + t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]); |
32 | 26 | }); |
33 | 27 |
|
34 | | -test.serial('Create new changelog with custom path', async t => { |
| 28 | +test('Create new changelog with custom path', async t => { |
| 29 | + const cwd = tempy.directory(); |
35 | 30 | const notes = 'Test release note'; |
36 | 31 | const changelogFile = 'docs/changelog.txt'; |
| 32 | + const changelogPath = path.resolve(cwd, 'docs/changelog.txt'); |
37 | 33 |
|
38 | | - await prepare({changelogFile}, notes, t.context.logger); |
| 34 | + await prepare({changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
39 | 35 |
|
40 | 36 | // Verify the content of the CHANGELOG.md |
41 | | - t.is((await readFile(changelogFile)).toString(), `${notes}\n`); |
| 37 | + t.is((await readFile(changelogPath)).toString(), `${notes}\n`); |
42 | 38 |
|
43 | | - t.deepEqual(t.context.log.args[0], ['Create %s', changelogFile]); |
| 39 | + t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]); |
44 | 40 | }); |
45 | 41 |
|
46 | | -test.serial('Prepend the CHANGELOG.md if there is an existing one', async t => { |
| 42 | +test('Prepend the CHANGELOG.md if there is an existing one', async t => { |
| 43 | + const cwd = tempy.directory(); |
47 | 44 | const notes = 'Test release note'; |
48 | | - await outputFile('CHANGELOG.md', 'Initial CHANGELOG'); |
| 45 | + const changelogFile = 'CHANGELOG.md'; |
| 46 | + const changelogPath = path.resolve(cwd, changelogFile); |
| 47 | + await outputFile(changelogPath, 'Initial CHANGELOG'); |
49 | 48 |
|
50 | | - await prepare({}, notes, t.context.logger); |
| 49 | + await prepare({}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
51 | 50 |
|
52 | 51 | // Verify the content of the CHANGELOG.md |
53 | | - t.is((await readFile('CHANGELOG.md')).toString(), `${notes}\n\nInitial CHANGELOG\n`); |
54 | | - t.deepEqual(t.context.log.args[0], ['Update %s', 'CHANGELOG.md']); |
| 52 | + t.is((await readFile(changelogPath)).toString(), `${notes}\n\nInitial CHANGELOG\n`); |
| 53 | + t.deepEqual(t.context.log.args[0], ['Update %s', changelogPath]); |
55 | 54 | }); |
56 | 55 |
|
57 | | -test.serial('Prepend title in the CHANGELOG.md if there is none', async t => { |
| 56 | +test('Prepend title in the CHANGELOG.md if there is none', async t => { |
| 57 | + const cwd = tempy.directory(); |
58 | 58 | const notes = 'Test release note'; |
59 | | - await outputFile('CHANGELOG.md', 'Initial CHANGELOG'); |
60 | | - |
61 | 59 | const changelogTitle = '# My Changelog Title'; |
62 | | - await prepare({changelogTitle}, notes, t.context.logger); |
| 60 | + const changelogFile = 'CHANGELOG.md'; |
| 61 | + const changelogPath = path.resolve(cwd, changelogFile); |
| 62 | + await outputFile(changelogPath, 'Initial CHANGELOG'); |
| 63 | + |
| 64 | + await prepare({changelogTitle}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
63 | 65 |
|
64 | | - t.is((await readFile('CHANGELOG.md')).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`); |
| 66 | + t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`); |
65 | 67 | }); |
66 | 68 |
|
67 | | -test.serial('Keep the title at the top of the CHANGELOG.md', async t => { |
| 69 | +test('Keep the title at the top of the CHANGELOG.md', async t => { |
| 70 | + const cwd = tempy.directory(); |
68 | 71 | const notes = 'Test release note'; |
69 | 72 | const changelogTitle = '# My Changelog Title'; |
70 | | - await outputFile('CHANGELOG.md', `${changelogTitle}\n\nInitial CHANGELOG`); |
| 73 | + const changelogFile = 'CHANGELOG.md'; |
| 74 | + const changelogPath = path.resolve(cwd, changelogFile); |
| 75 | + await outputFile(changelogPath, `${changelogTitle}\n\nInitial CHANGELOG`); |
71 | 76 |
|
72 | | - await prepare({changelogTitle}, notes, t.context.logger); |
| 77 | + await prepare({changelogTitle}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
73 | 78 |
|
74 | | - t.is((await readFile('CHANGELOG.md')).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`); |
| 79 | + t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`); |
75 | 80 | }); |
76 | 81 |
|
77 | 82 | test.serial('Create new changelog with title if specified', async t => { |
| 83 | + const cwd = tempy.directory(); |
78 | 84 | const notes = 'Test release note'; |
79 | 85 | const changelogTitle = '# My Changelog Title'; |
80 | 86 | const changelogFile = 'HISTORY.md'; |
| 87 | + const changelogPath = path.resolve(cwd, changelogFile); |
81 | 88 |
|
82 | | - await prepare({changelogTitle, changelogFile}, notes, t.context.logger); |
| 89 | + await prepare({changelogTitle, changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger}); |
83 | 90 |
|
84 | | - t.is((await readFile(changelogFile)).toString(), `${changelogTitle}\n\n${notes}\n`); |
| 91 | + t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`); |
85 | 92 | }); |
0 commit comments