|
1 | 1 | import { vol } from 'memfs'; |
2 | 2 | import { readFile } from 'node:fs/promises'; |
3 | 3 | import { MEMFS_VOLUME } from '@code-pushup/test-utils'; |
4 | | -import { resolveGitignore, updateGitignore } from './gitignore.js'; |
| 4 | +import { resolveGitignore } from './gitignore.js'; |
| 5 | +import { createTree } from './virtual-fs.js'; |
5 | 6 |
|
6 | 7 | describe('resolveGitignore', () => { |
7 | | - it('should return CREATE change with comment when no .gitignore exists', async () => { |
| 8 | + it('should create .gitignore with comment when it does not exist', async () => { |
8 | 9 | vol.fromJSON({ 'package.json': '{}' }, MEMFS_VOLUME); |
| 10 | + const tree = createTree(MEMFS_VOLUME); |
9 | 11 |
|
10 | | - await expect(resolveGitignore()).resolves.toStrictEqual({ |
11 | | - type: 'CREATE', |
12 | | - path: '.gitignore', |
13 | | - content: '# Code PushUp reports\n.code-pushup\n', |
14 | | - }); |
| 12 | + await resolveGitignore(tree); |
| 13 | + |
| 14 | + expect(tree.listChanges()).toStrictEqual([ |
| 15 | + { |
| 16 | + type: 'CREATE', |
| 17 | + path: '.gitignore', |
| 18 | + content: '# Code PushUp reports\n.code-pushup\n', |
| 19 | + }, |
| 20 | + ]); |
15 | 21 | }); |
16 | 22 |
|
17 | | - it('should return UPDATE change with blank line separator for existing .gitignore', async () => { |
| 23 | + it('should update .gitignore with blank line separator when it already exists', async () => { |
18 | 24 | vol.fromJSON({ '.gitignore': 'node_modules\n' }, MEMFS_VOLUME); |
| 25 | + const tree = createTree(MEMFS_VOLUME); |
| 26 | + |
| 27 | + await resolveGitignore(tree); |
19 | 28 |
|
20 | | - await expect(resolveGitignore()).resolves.toStrictEqual({ |
21 | | - type: 'UPDATE', |
22 | | - path: '.gitignore', |
23 | | - content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
24 | | - }); |
| 29 | + expect(tree.listChanges()).toStrictEqual([ |
| 30 | + { |
| 31 | + type: 'UPDATE', |
| 32 | + path: '.gitignore', |
| 33 | + content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
| 34 | + }, |
| 35 | + ]); |
25 | 36 | }); |
26 | 37 |
|
27 | 38 | it('should preserve existing blank line before appending', async () => { |
28 | 39 | vol.fromJSON({ '.gitignore': 'node_modules\n\n' }, MEMFS_VOLUME); |
| 40 | + const tree = createTree(MEMFS_VOLUME); |
29 | 41 |
|
30 | | - await expect(resolveGitignore()).resolves.toStrictEqual({ |
31 | | - type: 'UPDATE', |
32 | | - path: '.gitignore', |
33 | | - content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
34 | | - }); |
| 42 | + await resolveGitignore(tree); |
| 43 | + |
| 44 | + expect(tree.listChanges()).toStrictEqual([ |
| 45 | + { |
| 46 | + type: 'UPDATE', |
| 47 | + path: '.gitignore', |
| 48 | + content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
| 49 | + }, |
| 50 | + ]); |
35 | 51 | }); |
36 | 52 |
|
37 | 53 | it('should add double newline separator when .gitignore has no trailing newline', async () => { |
38 | 54 | vol.fromJSON({ '.gitignore': 'node_modules' }, MEMFS_VOLUME); |
| 55 | + const tree = createTree(MEMFS_VOLUME); |
| 56 | + |
| 57 | + await resolveGitignore(tree); |
39 | 58 |
|
40 | | - await expect(resolveGitignore()).resolves.toStrictEqual({ |
41 | | - type: 'UPDATE', |
42 | | - path: '.gitignore', |
43 | | - content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
44 | | - }); |
| 59 | + expect(tree.listChanges()).toStrictEqual([ |
| 60 | + { |
| 61 | + type: 'UPDATE', |
| 62 | + path: '.gitignore', |
| 63 | + content: 'node_modules\n\n# Code PushUp reports\n.code-pushup\n', |
| 64 | + }, |
| 65 | + ]); |
45 | 66 | }); |
46 | 67 |
|
47 | | - it('should return null if entry already in .gitignore', async () => { |
| 68 | + it('should skip if entry already in .gitignore', async () => { |
48 | 69 | vol.fromJSON({ '.gitignore': '.code-pushup\n' }, MEMFS_VOLUME); |
| 70 | + const tree = createTree(MEMFS_VOLUME); |
49 | 71 |
|
50 | | - await expect(resolveGitignore()).resolves.toBeNull(); |
| 72 | + await resolveGitignore(tree); |
| 73 | + |
| 74 | + expect(tree.listChanges()).toStrictEqual([]); |
51 | 75 | }); |
52 | | -}); |
53 | 76 |
|
54 | | -describe('updateGitignore', () => { |
55 | | - it('should skip writing when change is null', async () => { |
56 | | - vol.fromJSON({ 'package.json': '{}' }, MEMFS_VOLUME); |
| 77 | + it('should skip if **/.code-pushup entry already in .gitignore', async () => { |
| 78 | + vol.fromJSON({ '.gitignore': '**/.code-pushup\n' }, MEMFS_VOLUME); |
| 79 | + const tree = createTree(MEMFS_VOLUME); |
| 80 | + |
| 81 | + await resolveGitignore(tree); |
| 82 | + |
| 83 | + expect(tree.listChanges()).toStrictEqual([]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should skip if entry exists among comments and other entries', async () => { |
| 87 | + vol.fromJSON( |
| 88 | + { '.gitignore': '# build output\ndist\n\n# reports\n.code-pushup\n' }, |
| 89 | + MEMFS_VOLUME, |
| 90 | + ); |
| 91 | + const tree = createTree(MEMFS_VOLUME); |
| 92 | + |
| 93 | + await resolveGitignore(tree); |
| 94 | + |
| 95 | + expect(tree.listChanges()).toStrictEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should skip if entry has leading and trailing whitespace', async () => { |
| 99 | + vol.fromJSON({ '.gitignore': ' .code-pushup \n' }, MEMFS_VOLUME); |
| 100 | + const tree = createTree(MEMFS_VOLUME); |
57 | 101 |
|
58 | | - await updateGitignore(null); |
| 102 | + await resolveGitignore(tree); |
59 | 103 |
|
60 | | - expect(vol.toJSON(MEMFS_VOLUME)).toStrictEqual({ |
61 | | - [`${MEMFS_VOLUME}/package.json`]: '{}', |
62 | | - }); |
| 104 | + expect(tree.listChanges()).toStrictEqual([]); |
63 | 105 | }); |
64 | 106 |
|
65 | | - it('should write .gitignore file to git root', async () => { |
| 107 | + it('should not match commented-out entry', async () => { |
| 108 | + vol.fromJSON({ '.gitignore': '# .code-pushup\n' }, MEMFS_VOLUME); |
| 109 | + const tree = createTree(MEMFS_VOLUME); |
| 110 | + |
| 111 | + await resolveGitignore(tree); |
| 112 | + |
| 113 | + expect(tree.listChanges()).toStrictEqual([ |
| 114 | + { |
| 115 | + type: 'UPDATE', |
| 116 | + path: '.gitignore', |
| 117 | + content: '# .code-pushup\n\n# Code PushUp reports\n.code-pushup\n', |
| 118 | + }, |
| 119 | + ]); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('resolveGitignore - flush', () => { |
| 124 | + it('should write .gitignore file to disk on flush', async () => { |
66 | 125 | vol.fromJSON({ 'package.json': '{}' }, MEMFS_VOLUME); |
| 126 | + const tree = createTree(MEMFS_VOLUME); |
67 | 127 |
|
68 | | - await updateGitignore({ |
69 | | - type: 'CREATE', |
70 | | - path: '.gitignore', |
71 | | - content: '# Code PushUp reports\n.code-pushup\n', |
72 | | - }); |
| 128 | + await resolveGitignore(tree); |
| 129 | + await tree.flush(); |
73 | 130 |
|
74 | 131 | await expect(readFile(`${MEMFS_VOLUME}/.gitignore`, 'utf8')).resolves.toBe( |
75 | 132 | '# Code PushUp reports\n.code-pushup\n', |
76 | 133 | ); |
77 | 134 | }); |
| 135 | + |
| 136 | + it('should skip writing when entry already exists', async () => { |
| 137 | + vol.fromJSON({ '.gitignore': '.code-pushup\n' }, MEMFS_VOLUME); |
| 138 | + const tree = createTree(MEMFS_VOLUME); |
| 139 | + |
| 140 | + await resolveGitignore(tree); |
| 141 | + await tree.flush(); |
| 142 | + |
| 143 | + await expect(readFile(`${MEMFS_VOLUME}/.gitignore`, 'utf8')).resolves.toBe( |
| 144 | + '.code-pushup\n', |
| 145 | + ); |
| 146 | + }); |
78 | 147 | }); |
0 commit comments