-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgitignore.unit.test.ts
More file actions
147 lines (114 loc) · 4.28 KB
/
gitignore.unit.test.ts
File metadata and controls
147 lines (114 loc) · 4.28 KB
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
import { vol } from 'memfs';
import { readFile } from 'node:fs/promises';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { createTree } from '@code-pushup/utils';
import { resolveGitignore } from './gitignore.js';
describe('resolveGitignore', () => {
it('should create .gitignore with comment when it does not exist', async () => {
vol.fromJSON({ 'package.json': '{}' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([
{
type: 'CREATE',
path: '.gitignore',
content: '# Code PushUp reports\n.code-pushup\n',
},
]);
});
it('should update .gitignore with blank line separator when it already exists', async () => {
vol.fromJSON({ '.gitignore': 'node_modules\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([
{
type: 'UPDATE',
path: '.gitignore',
content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n',
},
]);
});
it('should preserve existing blank line before appending', async () => {
vol.fromJSON({ '.gitignore': 'node_modules\n\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([
{
type: 'UPDATE',
path: '.gitignore',
content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n',
},
]);
});
it('should add double newline separator when .gitignore has no trailing newline', async () => {
vol.fromJSON({ '.gitignore': 'node_modules' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([
{
type: 'UPDATE',
path: '.gitignore',
content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n',
},
]);
});
it('should skip if entry already in .gitignore', async () => {
vol.fromJSON({ '.gitignore': '.code-pushup\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([]);
});
it('should skip if **/.code-pushup entry already in .gitignore', async () => {
vol.fromJSON({ '.gitignore': '**/.code-pushup\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([]);
});
it('should skip if entry exists among comments and other entries', async () => {
vol.fromJSON(
{ '.gitignore': '# build output\ndist\n\n# reports\n.code-pushup\n' },
MEMFS_VOLUME,
);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([]);
});
it('should skip if entry has leading and trailing whitespace', async () => {
vol.fromJSON({ '.gitignore': ' .code-pushup \n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([]);
});
it('should not match commented-out entry', async () => {
vol.fromJSON({ '.gitignore': '# .code-pushup\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
expect(tree.listChanges()).toStrictEqual([
{
type: 'UPDATE',
path: '.gitignore',
content: '# .code-pushup\n\n# Code PushUp reports\n.code-pushup\n',
},
]);
});
});
describe('resolveGitignore - flush', () => {
it('should write .gitignore file to disk on flush', async () => {
vol.fromJSON({ 'package.json': '{}' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
await tree.flush();
await expect(readFile(`${MEMFS_VOLUME}/.gitignore`, 'utf8')).resolves.toBe(
'# Code PushUp reports\n.code-pushup\n',
);
});
it('should skip writing when entry already exists', async () => {
vol.fromJSON({ '.gitignore': '.code-pushup\n' }, MEMFS_VOLUME);
const tree = createTree(MEMFS_VOLUME);
await resolveGitignore(tree);
await tree.flush();
await expect(readFile(`${MEMFS_VOLUME}/.gitignore`, 'utf8')).resolves.toBe(
'.code-pushup\n',
);
});
});