/
scm-hg.test.js
155 lines (122 loc) · 3.76 KB
/
scm-hg.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
import mock from 'mock-fs';
import execa from 'execa';
import fs from 'fs';
import prettyQuick from '..';
jest.mock('execa');
afterEach(() => {
mock.restore();
jest.clearAllMocks();
});
const mockHgFs = () => {
mock({
'/.hg': {},
'/foo.js': 'foo()',
'/bar.md': '# foo',
});
execa.sync.mockImplementation((command, args) => {
if (command !== 'hg') {
throw new Error(`unexpected command: ${command}`);
}
switch (args[0]) {
case 'status':
return { stdout: './foo.js\n' + './bar.md\n' };
case 'diff':
return { stdout: './foo.js\n' + './bar.md\n' };
case 'add':
return { stdout: '' };
case 'log':
return { stdout: '' };
default:
throw new Error(`unexpected arg0: ${args[0]}`);
}
});
};
describe('with hg', () => {
test('calls `hg debugancestor`', () => {
mock({
'/.hg': {},
});
prettyQuick('root');
expect(execa.sync).toHaveBeenCalledWith(
'hg',
['debugancestor', 'tip', 'default'],
{ cwd: '/' }
);
});
test('calls `hg debugancestor` with root hg directory', () => {
mock({
'/.hg': {},
'/other-dir': {},
});
prettyQuick('/other-dir');
expect(execa.sync).toHaveBeenCalledWith(
'hg',
['debugancestor', 'tip', 'default'],
{ cwd: '/' }
);
});
test('calls `hg status` with revision', () => {
mock({
'/.hg': {},
});
prettyQuick('root', { since: 'banana' });
expect(execa.sync).toHaveBeenCalledWith(
'hg',
['status', '-n', '-a', '-m', '--rev', 'banana'],
{ cwd: '/' }
);
});
test('calls onFoundSinceRevision with return value from `hg debugancestor`', () => {
const onFoundSinceRevision = jest.fn();
mock({
'/.hg': {},
});
execa.sync.mockReturnValue({ stdout: 'banana' });
prettyQuick('root', { onFoundSinceRevision });
expect(onFoundSinceRevision).toHaveBeenCalledWith('hg', 'banana');
});
test('calls onFoundChangedFiles with changed files', () => {
const onFoundChangedFiles = jest.fn();
mockHgFs();
prettyQuick('root', { since: 'banana', onFoundChangedFiles });
expect(onFoundChangedFiles).toHaveBeenCalledWith(['./foo.js', './bar.md']);
});
test('calls onWriteFile with changed files', () => {
const onWriteFile = jest.fn();
mockHgFs();
prettyQuick('root', { since: 'banana', onWriteFile });
expect(onWriteFile).toHaveBeenCalledWith('./foo.js');
expect(onWriteFile).toHaveBeenCalledWith('./bar.md');
});
test('writes formatted files to disk', () => {
const onWriteFile = jest.fn();
mockHgFs();
prettyQuick('root', { since: 'banana', onWriteFile });
expect(fs.readFileSync('/foo.js', 'utf8')).toEqual('formatted:foo()');
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});
test('without --staged does NOT stage changed files', () => {
mockHgFs();
prettyQuick('root', { since: 'banana' });
expect(execa.sync).not.toHaveBeenCalledWith('hg', ['add', './foo.js'], {
cwd: '/',
});
expect(execa.sync).not.toHaveBeenCalledWith('hg', ['add', './bar.md'], {
cwd: '/',
});
});
test('with --verbose calls onExamineFile', () => {
const onExamineFile = jest.fn();
mockHgFs();
prettyQuick('root', { since: 'banana', verbose: true, onExamineFile });
expect(onExamineFile).toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).toHaveBeenCalledWith('./bar.md');
});
test('without --verbose does NOT call onExamineFile', () => {
const onExamineFile = jest.fn();
mockHgFs();
prettyQuick('root', { since: 'banana', onExamineFile });
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
});
});