-
Notifications
You must be signed in to change notification settings - Fork 47
/
load-changelog-config.test.js
187 lines (157 loc) · 8.13 KB
/
load-changelog-config.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import test from "ava";
import importFrom from "import-from-esm";
import sinon from "sinon";
import conventionalChangelogAngular from "conventional-changelog-angular";
import loadChangelogConfig from "../lib/load-changelog-config.js";
const cwd = process.cwd();
/**
* assertion to compare loaded writerOpts with the expected writerOpts from the angular preset
*
* @param {Object} t AVA assertion library.
* @param {Object} loadedWriterOpts
* @param {Object} angularPresetWriterOpts
*/
function assertWriterOptsAreFromAngularPreset(t, loadedWriterOpts, angularPresetWriterOpts) {
const { transform: loadedTransform, ...loadedWriterOptsWithoutTransform } = loadedWriterOpts;
const { transform: angularPresetTransform, ...angularPresetWriterOptsWithoutTransform } = angularPresetWriterOpts;
t.deepEqual(loadedWriterOptsWithoutTransform, angularPresetWriterOptsWithoutTransform);
t.is(loadedTransform.toString(), angularPresetTransform.toString());
}
/**
* AVA macro to verify that `loadChangelogConfig` return a config object with parserOpts and writerOpts.
*
* @method loadPreset
* @param {Object} t AVA assertion library.
* @param {String} preset the `conventional-changelog` preset to test.
* @param {Object} pluginOptions The plugin configuration.
*/
async function loadPreset(t, preset, pluginOptions) {
const changelogConfig = await loadChangelogConfig({ ...pluginOptions, preset }, { cwd });
t.truthy(changelogConfig.parserOpts.headerPattern);
t.truthy(changelogConfig.writerOpts.groupBy);
}
loadPreset.title = (providedTitle, preset) => `${providedTitle} Load "${preset}" preset`.trim();
/**
* AVA macro to verify that `loadChangelogConfig` return a config object with parserOpts and writerOpts.
*
* @method loadPreset
* @param {Object} t AVA assertion library.
* @param {String} config the `conventional-changelog` config to test.
* @param {Object} pluginOptions The plugin configuration.
*/
async function loadConfig(t, config, pluginOptions) {
const changelogConfig = await loadChangelogConfig(
{ ...pluginOptions, config: `conventional-changelog-${config}` },
{ cwd }
);
t.truthy(changelogConfig.parserOpts.headerPattern);
t.truthy(changelogConfig.writerOpts.groupBy);
}
loadConfig.title = (providedTitle, config) => `${providedTitle} Load "${config}" config`.trim();
test('Load "conventional-changelog-angular" by default', async (t) => {
const changelogConfig = await loadChangelogConfig({}, { cwd });
const angularChangelogConfig = await conventionalChangelogAngular();
t.deepEqual(changelogConfig.parserOpts, angularChangelogConfig.parserOpts);
assertWriterOptsAreFromAngularPreset(t, changelogConfig.writerOpts, angularChangelogConfig.writerOpts);
});
test('Accept a "parserOpts" object as option', async (t) => {
const customParserOptions = {
headerPattern: /^##(?<tag>.*?)## (?<shortDesc>.*)$/,
headerCorrespondence: ["tag", "shortDesc"],
};
const changelogConfig = await loadChangelogConfig({ parserOpts: customParserOptions }, { cwd });
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customParserOptions.headerPattern, changelogConfig.parserOpts.headerPattern);
t.deepEqual(customParserOptions.headerCorrespondence, changelogConfig.parserOpts.headerCorrespondence);
t.deepEqual(changelogConfig.parserOpts.noteKeywords, angularChangelogConfig.parserOpts.noteKeywords);
assertWriterOptsAreFromAngularPreset(t, changelogConfig.writerOpts, angularChangelogConfig.writerOpts);
});
test('Accept a "writerOpts" object as option', async (t) => {
const customWriterOptions = { commitGroupsSort: "title", commitsSort: ["scope", "subject"] };
const changelogConfig = await loadChangelogConfig({ writerOpts: customWriterOptions }, { cwd });
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customWriterOptions.commitGroupsSort, changelogConfig.writerOpts.commitGroupsSort);
t.deepEqual(customWriterOptions.commitsSort, changelogConfig.writerOpts.commitsSort);
t.deepEqual(changelogConfig.writerOpts.noteGroupsSort, angularChangelogConfig.writerOpts.noteGroupsSort);
t.deepEqual(changelogConfig.parserOpts, angularChangelogConfig.parserOpts);
});
test('Accept a partial "parserOpts" object as option that overwrite a preset', async (t) => {
const customParserOptions = {
headerPattern: /^##(?<tag>.*?)## (?<shortDesc>.*)$/,
headerCorrespondence: ["tag", "shortDesc"],
};
const changelogConfig = await loadChangelogConfig({ parserOpts: customParserOptions, preset: "angular" }, { cwd });
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customParserOptions.headerPattern, changelogConfig.parserOpts.headerPattern);
t.deepEqual(customParserOptions.headerCorrespondence, changelogConfig.parserOpts.headerCorrespondence);
t.truthy(changelogConfig.parserOpts.noteKeywords);
assertWriterOptsAreFromAngularPreset(t, changelogConfig.writerOpts, angularChangelogConfig.writerOpts);
});
test('Accept a "writerOpts" object as option that overwrite a preset', async (t) => {
const customWriterOptions = { commitGroupsSort: "title", commitsSort: ["scope", "subject"] };
const changelogConfig = await loadChangelogConfig({ writerOpts: customWriterOptions, preset: "angular" }, { cwd });
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customWriterOptions.commitGroupsSort, changelogConfig.writerOpts.commitGroupsSort);
t.deepEqual(customWriterOptions.commitsSort, changelogConfig.writerOpts.commitsSort);
t.truthy(changelogConfig.writerOpts.noteGroupsSort);
t.deepEqual(changelogConfig.parserOpts, angularChangelogConfig.parserOpts);
});
test('Accept a partial "parserOpts" object as option that overwrite a config', async (t) => {
const customParserOptions = {
headerPattern: /^##(?<tag>.*?)## (?<shortDesc>.*)$/,
headerCorrespondence: ["tag", "shortDesc"],
};
const changelogConfig = await loadChangelogConfig(
{
parserOpts: customParserOptions,
config: "conventional-changelog-angular",
},
{ cwd }
);
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customParserOptions.headerPattern, changelogConfig.parserOpts.headerPattern);
t.deepEqual(customParserOptions.headerCorrespondence, changelogConfig.parserOpts.headerCorrespondence);
t.truthy(changelogConfig.parserOpts.noteKeywords);
assertWriterOptsAreFromAngularPreset(t, changelogConfig.writerOpts, angularChangelogConfig.writerOpts);
});
test('Accept a "writerOpts" object as option that overwrite a config', async (t) => {
const customWriterOptions = { commitGroupsSort: "title", commitsSort: ["scope", "subject"] };
const changelogConfig = await loadChangelogConfig(
{
writerOpts: customWriterOptions,
config: "conventional-changelog-angular",
},
{ cwd }
);
const angularChangelogConfig = await conventionalChangelogAngular();
t.is(customWriterOptions.commitGroupsSort, changelogConfig.writerOpts.commitGroupsSort);
t.deepEqual(customWriterOptions.commitsSort, changelogConfig.writerOpts.commitsSort);
t.truthy(changelogConfig.writerOpts.noteGroupsSort);
t.deepEqual(changelogConfig.parserOpts, angularChangelogConfig.parserOpts);
});
test(loadPreset, "angular");
test(loadConfig, "angular");
test(loadPreset, "atom");
test(loadConfig, "atom");
test(loadPreset, "ember");
test(loadConfig, "ember");
test(loadPreset, "eslint");
test(loadConfig, "eslint");
test(loadPreset, "express");
test(loadConfig, "express");
test(loadPreset, "jshint");
test(loadConfig, "jshint");
test(loadPreset, "conventionalcommits", { presetConfig: {} });
test(loadConfig, "conventionalcommits", { presetConfig: {} });
test('Throw error if "config" doesn`t exist', async (t) => {
await t.throwsAsync(loadChangelogConfig({ config: "unknown-config" }, { cwd }), { code: "MODULE_NOT_FOUND" });
});
test('Throw error if "preset" doesn`t exist', async (t) => {
await t.throwsAsync(loadChangelogConfig({ preset: "unknown-preset" }, { cwd }), { code: "MODULE_NOT_FOUND" });
});
test.serial("Load preset and config correctly when importFrom.silent fails", async (t) => {
sinon.stub(importFrom, "silent").returns(undefined);
await loadPreset(t, "angular");
await loadConfig(t, "angular");
sinon.restore();
});