|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import { glob as nodeGlob } from 'glob'; |
| 3 | +import { loadGloblin, createTestFixture, cleanupFixture, FixtureConfig } from '../harness'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Task 4.4.1: Test ignore with dot files |
| 8 | + * |
| 9 | + * Key behavior: |
| 10 | + * - Ignore patterns always operate in dot: true mode internally |
| 11 | + * - Can ignore .hidden files even when the main dot option is false |
| 12 | + * - Ignore patterns can match dotfiles that the main pattern would skip |
| 13 | + */ |
| 14 | +describe('ignore option with dotfiles', () => { |
| 15 | + let globlin: Awaited<ReturnType<typeof loadGloblin>>; |
| 16 | + let fixturePath: string; |
| 17 | + |
| 18 | + beforeAll(async () => { |
| 19 | + globlin = await loadGloblin(); |
| 20 | + |
| 21 | + // Create a fixture with dotfiles and directories |
| 22 | + const config: FixtureConfig = { |
| 23 | + files: [ |
| 24 | + 'visible/a.txt', |
| 25 | + 'visible/b.txt', |
| 26 | + '.hidden/a.txt', |
| 27 | + '.hidden/b.txt', |
| 28 | + '.dotfile', |
| 29 | + 'dir/.config', |
| 30 | + 'dir/.env', |
| 31 | + 'dir/normal.txt', |
| 32 | + 'nested/deep/.secret', |
| 33 | + 'nested/visible.txt', |
| 34 | + ], |
| 35 | + }; |
| 36 | + |
| 37 | + fixturePath = await createTestFixture('ignore-dotfiles-test', config); |
| 38 | + }); |
| 39 | + |
| 40 | + afterAll(async () => { |
| 41 | + if (fixturePath) { |
| 42 | + await cleanupFixture(fixturePath); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + describe('ignore patterns can match dotfiles even with dot: false', () => { |
| 47 | + it('should ignore a dotfile even when dot: false (glob behavior)', async () => { |
| 48 | + // With dot: false, .dotfile is not matched by the pattern |
| 49 | + // But if it were, the ignore would apply |
| 50 | + const results = await globlin.glob('*', { |
| 51 | + cwd: fixturePath, |
| 52 | + dot: false, |
| 53 | + ignore: ['.dotfile'], |
| 54 | + }); |
| 55 | + |
| 56 | + // .dotfile should not be in results (not matched due to dot: false) |
| 57 | + expect(results).not.toContain('.dotfile'); |
| 58 | + expect(results).toContain('visible'); |
| 59 | + expect(results).toContain('dir'); |
| 60 | + expect(results).toContain('nested'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should ignore a dotfile pattern with dot: true', async () => { |
| 64 | + const results = await globlin.glob('*', { |
| 65 | + cwd: fixturePath, |
| 66 | + dot: true, |
| 67 | + ignore: ['.dotfile'], |
| 68 | + }); |
| 69 | + |
| 70 | + // .dotfile should be excluded by ignore pattern |
| 71 | + expect(results).not.toContain('.dotfile'); |
| 72 | + // But .hidden should still be included |
| 73 | + expect(results).toContain('.hidden'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should ignore dotfiles matching a pattern', async () => { |
| 77 | + const results = await globlin.glob('**/*', { |
| 78 | + cwd: fixturePath, |
| 79 | + dot: true, |
| 80 | + ignore: ['**/.*'], |
| 81 | + }); |
| 82 | + |
| 83 | + // Files ending in dotname should be excluded (like .dotfile, .config, .env, .secret) |
| 84 | + // But .hidden/a.txt is NOT excluded because a.txt doesn't start with . |
| 85 | + expect(results).not.toContain('.dotfile'); |
| 86 | + expect(results).not.toContain('dir/.config'); |
| 87 | + expect(results).not.toContain('dir/.env'); |
| 88 | + expect(results).not.toContain('nested/deep/.secret'); |
| 89 | + |
| 90 | + // Files in hidden directories are still found (the ignore pattern doesn't match them) |
| 91 | + expect(results).toContain('.hidden/a.txt'); |
| 92 | + expect(results).toContain('.hidden/b.txt'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('ignore patterns operate in dot: true mode internally', () => { |
| 97 | + it('should match hidden directories with ignore pattern', async () => { |
| 98 | + const results = await globlin.glob('**/*.txt', { |
| 99 | + cwd: fixturePath, |
| 100 | + dot: false, |
| 101 | + ignore: ['.hidden/**'], |
| 102 | + }); |
| 103 | + |
| 104 | + // With dot: false, .hidden files are not matched anyway |
| 105 | + // Verify visible files are found |
| 106 | + expect(results).toContain('visible/a.txt'); |
| 107 | + expect(results).toContain('visible/b.txt'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should match hidden directories with ignore pattern when dot: true', async () => { |
| 111 | + const results = await globlin.glob('**/*.txt', { |
| 112 | + cwd: fixturePath, |
| 113 | + dot: true, |
| 114 | + ignore: ['.hidden/**'], |
| 115 | + }); |
| 116 | + |
| 117 | + // .hidden/a.txt and .hidden/b.txt should be excluded by ignore |
| 118 | + expect(results).not.toContain('.hidden/a.txt'); |
| 119 | + expect(results).not.toContain('.hidden/b.txt'); |
| 120 | + // But visible files should still be found |
| 121 | + expect(results).toContain('visible/a.txt'); |
| 122 | + expect(results).toContain('visible/b.txt'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should match files starting with dot in ignore patterns', async () => { |
| 126 | + const results = await globlin.glob('dir/*', { |
| 127 | + cwd: fixturePath, |
| 128 | + dot: true, |
| 129 | + ignore: ['dir/.*'], |
| 130 | + }); |
| 131 | + |
| 132 | + // .config and .env should be excluded by ignore |
| 133 | + expect(results).not.toContain('dir/.config'); |
| 134 | + expect(results).not.toContain('dir/.env'); |
| 135 | + // But normal.txt should be found |
| 136 | + expect(results).toContain('dir/normal.txt'); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('comparison with glob package', () => { |
| 141 | + it('should match glob behavior for ignore with dotfiles', async () => { |
| 142 | + const pattern = '**/*'; |
| 143 | + const options = { |
| 144 | + cwd: fixturePath, |
| 145 | + dot: true, |
| 146 | + ignore: ['**/.*'], |
| 147 | + }; |
| 148 | + |
| 149 | + const [globResults, globlinResults] = await Promise.all([ |
| 150 | + nodeGlob(pattern, options), |
| 151 | + globlin.glob(pattern, options), |
| 152 | + ]); |
| 153 | + |
| 154 | + expect(new Set(globlinResults)).toEqual(new Set(globResults)); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should match glob behavior for ignore hidden directory', async () => { |
| 158 | + const pattern = '**/*.txt'; |
| 159 | + const options = { |
| 160 | + cwd: fixturePath, |
| 161 | + dot: true, |
| 162 | + ignore: ['.hidden/**'], |
| 163 | + }; |
| 164 | + |
| 165 | + const [globResults, globlinResults] = await Promise.all([ |
| 166 | + nodeGlob(pattern, options), |
| 167 | + globlin.glob(pattern, options), |
| 168 | + ]); |
| 169 | + |
| 170 | + expect(new Set(globlinResults)).toEqual(new Set(globResults)); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should match glob behavior when ignoring dotfile in directory', async () => { |
| 174 | + const pattern = 'dir/*'; |
| 175 | + const options = { |
| 176 | + cwd: fixturePath, |
| 177 | + dot: true, |
| 178 | + ignore: ['dir/.config'], |
| 179 | + }; |
| 180 | + |
| 181 | + const [globResults, globlinResults] = await Promise.all([ |
| 182 | + nodeGlob(pattern, options), |
| 183 | + globlin.glob(pattern, options), |
| 184 | + ]); |
| 185 | + |
| 186 | + expect(new Set(globlinResults)).toEqual(new Set(globResults)); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('sync API', () => { |
| 191 | + it('should work identically with globSync', () => { |
| 192 | + const pattern = '**/*'; |
| 193 | + const options = { |
| 194 | + cwd: fixturePath, |
| 195 | + dot: true, |
| 196 | + ignore: ['**/.*'], |
| 197 | + }; |
| 198 | + |
| 199 | + const globResults = nodeGlob.sync(pattern, options); |
| 200 | + const globlinResults = globlin.globSync(pattern, options); |
| 201 | + |
| 202 | + expect(new Set(globlinResults)).toEqual(new Set(globResults)); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('edge cases', () => { |
| 207 | + it('should handle ignore pattern that is just a dot', async () => { |
| 208 | + const results = await globlin.glob('**/*', { |
| 209 | + cwd: fixturePath, |
| 210 | + dot: true, |
| 211 | + ignore: ['.'], |
| 212 | + }); |
| 213 | + |
| 214 | + // The current directory "." should not appear in results |
| 215 | + // (it's not in a non-mark glob anyway with **/* pattern) |
| 216 | + expect(results.every((r) => r !== '.')).toBe(true); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should handle nested dotfile ignore patterns', async () => { |
| 220 | + const results = await globlin.glob('**/*', { |
| 221 | + cwd: fixturePath, |
| 222 | + dot: true, |
| 223 | + ignore: ['**/.secret'], |
| 224 | + }); |
| 225 | + |
| 226 | + // nested/deep/.secret should be excluded |
| 227 | + expect(results).not.toContain('nested/deep/.secret'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should handle multiple dotfile ignore patterns', async () => { |
| 231 | + const results = await globlin.glob('**/*', { |
| 232 | + cwd: fixturePath, |
| 233 | + dot: true, |
| 234 | + ignore: ['.dotfile', '.hidden/**', '**/.*'], |
| 235 | + }); |
| 236 | + |
| 237 | + // .dotfile should be excluded |
| 238 | + expect(results).not.toContain('.dotfile'); |
| 239 | + // .hidden directory and contents should be excluded |
| 240 | + expect(results.filter((r) => r.startsWith('.hidden'))).toHaveLength(0); |
| 241 | + // All dotfiles (files starting with .) should be excluded |
| 242 | + expect(results).not.toContain('dir/.config'); |
| 243 | + expect(results).not.toContain('dir/.env'); |
| 244 | + expect(results).not.toContain('nested/deep/.secret'); |
| 245 | + }); |
| 246 | + }); |
| 247 | +}); |
0 commit comments