-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
147 lines (114 loc) · 4.31 KB
/
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
const { readFileSync, writeFileSync } = require("fs");
const { dirSync, setGracefulCleanup } = require("tmp");
const { exec } = require("shelljs");
setGracefulCleanup();
const options = { cwd: __dirname, fatal: true, silent: true };
let result;
let tmp;
let dir;
let simple;
let monorepo;
beforeEach(() => {
result = undefined;
tmp = dirSync();
dir = `${tmp.name}`;
simple = `${dir}/simple`;
monorepo = `${dir}/monorepo`;
// just in case...
exec(`rm -Rf ./examples/simple/node_modules`, options);
exec(`rm -Rf ./examples/monorepo/node_modules`, options);
exec("yarn cache clean git-aliases-semantic-commits");
exec(`cp -Rf ./examples/simple/ ${simple}`, options);
exec(`cp -Rf ./examples/monorepo/ ${monorepo}`, options);
exec(`cd ${simple}; git init`, options);
exec(`cd ${monorepo}; git init`, options);
});
const install = example => {
result = exec(
`cd ${example}; INIT_CWD=${example} yarn add --dev ${__dirname}`,
options
);
};
describe("simple", () => {
beforeEach(() => install(simple));
it("should install git aliases", () => {
expect(result.code).toBe(0);
// check config setup
const config = readFileSync(`${simple}/.git/config`).toString();
expect(config).toContain("[alias]");
expect(config).toContain(
"chore = !node_modules/git-aliases-semantic-commits/git-aliases/chore"
);
});
it("should be possible to use 'chore' alias", () => {
const execOptions = { ...options, cwd: simple };
result = exec('git add .; git chore "initial commit"', execOptions);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("chore: initial commit");
});
it("should be possible to use 'feat' alias", () => {
const execOptions = { ...options, cwd: simple };
result = exec("git add .; git feat 'initial commit'", execOptions);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("feat: initial commit");
});
it("should be possible to use namespacing", () => {
const execOptions = { ...options, cwd: simple };
result = exec(
"git add .; git chore -s scope 'initial commit'",
execOptions
);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("chore(scope): initial commit");
});
});
describe("monorepo", () => {
const setScopeStrategy = scope => {
const packageJson = require(`${monorepo}/package.json`);
packageJson.semanticCommits = { scope: scope };
writeFileSync(
`${monorepo}/package.json`,
JSON.stringify(packageJson, null, 2)
);
};
beforeEach(() => install(monorepo));
describe("scope strategy: package.json", () => {
beforeEach(() => setScopeStrategy("package.json"));
it("should add non-scoped messages commit when files outside scopes", () => {
const execOptions = { ...options, cwd: monorepo };
result = exec(
"git add package.json; git chore 'initial commit'",
execOptions
);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("chore: initial commit");
});
it("should add scoped messages commit when files inside scopes", () => {
const execOptions = { ...options, cwd: monorepo };
result = exec(
"git add ./package-json/first; git chore 'initial commit'",
execOptions
);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("chore(first): initial commit");
});
it("should add multiple scopes when changing files are in multiple places", () => {
const execOptions = { ...options, cwd: monorepo };
result = exec("git add . ; git chore 'initial commit'", execOptions);
expect(result.code).toBe(0);
result = exec("git --no-pager log -1 --pretty=%B", execOptions);
expect(result.code).toBe(0);
expect(result.stdout).toContain("chore(@root, first): initial commit");
});
});
});