|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { openRepository } from '../index'; |
| 5 | +import { useFixture } from './fixtures'; |
| 6 | + |
| 7 | +describe('ignore', () => { |
| 8 | + it('should handle basic ignore rules', async () => { |
| 9 | + const p = await useFixture('empty'); |
| 10 | + const repo = await openRepository(p); |
| 11 | + |
| 12 | + await fs.writeFile(path.join(p, 'server.log'), 'server.log'); |
| 13 | + await fs.writeFile(path.join(p, 'index.js'), 'index.js'); |
| 14 | + |
| 15 | + repo.addIgnoreRule('*.log'); |
| 16 | + expect(repo.isPathIgnored('server.log')).toBe(true); |
| 17 | + expect(repo.isPathIgnored('index.js')).toBe(false); |
| 18 | + |
| 19 | + repo.clearIgnoreRules(); |
| 20 | + expect(repo.isPathIgnored('server.log')).toBe(false); |
| 21 | + expect(repo.isPathIgnored('.git/config')).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should handle multiple ignore patterns', async () => { |
| 25 | + const p = await useFixture('empty'); |
| 26 | + const repo = await openRepository(p); |
| 27 | + |
| 28 | + await fs.writeFile(path.join(p, 'server.log'), 'server.log'); |
| 29 | + await fs.writeFile(path.join(p, 'index.js'), 'index.js'); |
| 30 | + await fs.mkdir(path.join(p, 'node_modules'), { recursive: true }); |
| 31 | + await fs.mkdir(path.join(p, 'dist'), { recursive: true }); |
| 32 | + await fs.writeFile(path.join(p, 'node_modules/package.json'), '{}'); |
| 33 | + await fs.writeFile(path.join(p, 'dist/bundle.js'), 'dist/bundle.js'); |
| 34 | + |
| 35 | + repo.addIgnoreRule('*.log\nnode_modules/\ndist/'); |
| 36 | + |
| 37 | + expect(repo.isPathIgnored('server.log')).toBe(true); |
| 38 | + expect(repo.isPathIgnored('node_modules/package.json')).toBe(true); |
| 39 | + expect(repo.isPathIgnored('dist/bundle.js')).toBe(true); |
| 40 | + expect(repo.isPathIgnored('index.js')).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should respect .gitignore files', async () => { |
| 44 | + const p = await useFixture('empty'); |
| 45 | + |
| 46 | + await fs.mkdir(path.join(p, 'dist'), { recursive: true }); |
| 47 | + await fs.mkdir(path.join(p, 'src'), { recursive: true }); |
| 48 | + await fs.writeFile(path.join(p, 'dist/bundle.js'), 'dist/bundle.js'); |
| 49 | + await fs.writeFile(path.join(p, 'src/main.js'), 'index.js'); |
| 50 | + await fs.writeFile(path.join(p, '.gitignore'), 'dist/\n*.js\n!src/main.js'); |
| 51 | + |
| 52 | + const repo = await openRepository(p); |
| 53 | + |
| 54 | + expect(repo.isPathIgnored('dist/bundle.js')).toBe(true); |
| 55 | + expect(repo.isPathIgnored('src/main.js')).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should combine added rules with .gitignore rules', async () => { |
| 59 | + const p = await useFixture('empty'); |
| 60 | + |
| 61 | + await fs.mkdir(path.join(p, 'dist'), { recursive: true }); |
| 62 | + await fs.mkdir(path.join(p, 'logs'), { recursive: true }); |
| 63 | + await fs.writeFile(path.join(p, 'dist/bundle.js'), 'dist/bundle.js'); |
| 64 | + await fs.writeFile(path.join(p, 'logs/server.log'), 'logs/server.log'); |
| 65 | + await fs.writeFile(path.join(p, '.gitignore'), 'dist/'); |
| 66 | + |
| 67 | + const repo = await openRepository(p); |
| 68 | + |
| 69 | + repo.addIgnoreRule('logs/'); |
| 70 | + |
| 71 | + expect(repo.isPathIgnored('dist/bundle.js')).toBe(true); |
| 72 | + expect(repo.isPathIgnored('logs/server.log')).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle invalid inputs gracefully', async () => { |
| 76 | + const p = await useFixture('empty'); |
| 77 | + const repo = await openRepository(p); |
| 78 | + await fs.writeFile(path.join(p, 'test.js'), 'test'); |
| 79 | + |
| 80 | + repo.addIgnoreRule(''); |
| 81 | + expect(repo.isPathIgnored('test.js')).toBe(false); |
| 82 | + |
| 83 | + expect(() => repo.isPathIgnored('')).not.toThrow(); |
| 84 | + expect(() => repo.isPathIgnored('../outside')).not.toThrow(); |
| 85 | + expect(() => repo.isPathIgnored('/absolute/path')).not.toThrow(); |
| 86 | + |
| 87 | + // @ts-expect-error |
| 88 | + expect(() => repo.isPathIgnored(null)).toThrow(); |
| 89 | + // @ts-expect-error |
| 90 | + expect(() => repo.isPathIgnored(undefined)).toThrow(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle special patterns correctly', async () => { |
| 94 | + const p = await useFixture('empty'); |
| 95 | + const repo = await openRepository(p); |
| 96 | + |
| 97 | + await fs.mkdir(path.join(p, 'logs'), { recursive: true }); |
| 98 | + await fs.writeFile(path.join(p, 'logs/error.log'), 'error'); |
| 99 | + await fs.writeFile(path.join(p, 'logs/important.log'), 'important'); |
| 100 | + await fs.mkdir(path.join(p, 'a/b/c'), { recursive: true }); |
| 101 | + await fs.writeFile(path.join(p, 'a/b/c/file.txt'), 'file'); |
| 102 | + await fs.writeFile(path.join(p, 'test[1].js'), 'test'); |
| 103 | + |
| 104 | + repo.addIgnoreRule('*.log\n!logs/important.log\na/**/c/*.txt\ntest[[]*.js'); |
| 105 | + |
| 106 | + expect(repo.isPathIgnored('logs/error.log')).toBe(true); |
| 107 | + expect(repo.isPathIgnored('logs/important.log')).toBe(false); |
| 108 | + expect(repo.isPathIgnored('a/b/c/file.txt')).toBe(true); |
| 109 | + expect(repo.isPathIgnored('test[1].js')).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should correctly handle path prefixes and comments', async () => { |
| 113 | + const p = await useFixture('empty'); |
| 114 | + const repo = await openRepository(p); |
| 115 | + |
| 116 | + await fs.mkdir(path.join(p, 'src/build'), { recursive: true }); |
| 117 | + await fs.mkdir(path.join(p, 'build'), { recursive: true }); |
| 118 | + await fs.mkdir(path.join(p, 'logs-2025-04-23'), { recursive: true }); |
| 119 | + await fs.writeFile(path.join(p, 'src/build/output.js'), 'output'); |
| 120 | + await fs.writeFile(path.join(p, 'build/output.js'), 'output'); |
| 121 | + await fs.writeFile(path.join(p, 'logs-2025-04-23/app.log'), 'log'); |
| 122 | + await fs.writeFile(path.join(p, 'temp.txt'), 'temp'); |
| 123 | + |
| 124 | + repo.addIgnoreRule('/build/\nlogs-*/\n# This is a comment\ntemp.txt'); |
| 125 | + |
| 126 | + expect(repo.isPathIgnored('build/output.js')).toBe(true); |
| 127 | + expect(repo.isPathIgnored('src/build/output.js')).toBe(false); |
| 128 | + expect(repo.isPathIgnored('logs-2025-04-23/app.log')).toBe(true); |
| 129 | + expect(repo.isPathIgnored('temp.txt')).toBe(true); |
| 130 | + }); |
| 131 | +}); |
0 commit comments