|
1 | | -import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 1 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
2 | 2 | import * as path from 'path'; |
3 | 3 | import * as os from 'os'; |
4 | 4 | import * as fs from 'fs'; |
@@ -134,3 +134,76 @@ describe('getFilesToDetect — shebang detection', () => { |
134 | 134 | }); |
135 | 135 | }); |
136 | 136 |
|
| 137 | +describe('getFilesToDetect — ignore patterns with relative paths (issue #611)', () => { |
| 138 | + let tmpDir: string; |
| 139 | + let originalCwd: string; |
| 140 | + |
| 141 | + beforeEach(() => { |
| 142 | + originalCwd = process.cwd(); |
| 143 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jscpd-ignore-test-')); |
| 144 | + fs.mkdirSync(path.join(tmpDir, 'patches'), { recursive: true }); |
| 145 | + fs.mkdirSync(path.join(tmpDir, 'src'), { recursive: true }); |
| 146 | + // Each file needs enough lines to pass minLines filter |
| 147 | + const content = Array.from({ length: 10 }, (_, i) => `const v${i} = ${i};`).join('\n'); |
| 148 | + fs.writeFileSync(path.join(tmpDir, 'patches', 'patch.js'), content); |
| 149 | + fs.writeFileSync(path.join(tmpDir, 'src', 'main.js'), content); |
| 150 | + process.chdir(tmpDir); |
| 151 | + }); |
| 152 | + |
| 153 | + afterEach(() => { |
| 154 | + process.chdir(originalCwd); |
| 155 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 156 | + }); |
| 157 | + |
| 158 | + const makeOptions = (dir: string, ignore: string[]): any => ({ |
| 159 | + path: [dir], |
| 160 | + format: ['javascript'], |
| 161 | + pattern: '**/*', |
| 162 | + minLines: 1, |
| 163 | + maxLines: 10000, |
| 164 | + maxSize: '100mb', |
| 165 | + ignore, |
| 166 | + noSymlinks: false, |
| 167 | + absolute: false, |
| 168 | + }); |
| 169 | + |
| 170 | + it('relative ignore pattern "patches/**" works when scan path is absolute (issue #611)', () => { |
| 171 | + // Simulate the exact issue #611 scenario: default path=[process.cwd()] is absolute, |
| 172 | + // and relative ignore patterns must still work. |
| 173 | + const files = getFilesToDetect(makeOptions(process.cwd(), ['patches/**'])); |
| 174 | + const filePaths = files.map(f => f.path); |
| 175 | + expect(filePaths.some(p => p.includes('patches'))).toBe(false); |
| 176 | + expect(filePaths.some(p => p.includes('src'))).toBe(true); |
| 177 | + }); |
| 178 | + |
| 179 | + it('relative ignore pattern "./patches/**" works when scan path is absolute', () => { |
| 180 | + const files = getFilesToDetect(makeOptions(process.cwd(), ['./patches/**'])); |
| 181 | + const filePaths = files.map(f => f.path); |
| 182 | + expect(filePaths.some(p => p.includes('patches'))).toBe(false); |
| 183 | + expect(filePaths.some(p => p.includes('src'))).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + it('relative ignore pattern "patches/**" works when scan path is "." (relative)', () => { |
| 187 | + const files = getFilesToDetect(makeOptions('.', ['patches/**'])); |
| 188 | + const filePaths = files.map(f => f.path); |
| 189 | + expect(filePaths.some(p => p.includes('patches'))).toBe(false); |
| 190 | + expect(filePaths.some(p => p.includes('src'))).toBe(true); |
| 191 | + }); |
| 192 | + |
| 193 | + it('relative ignore pattern "./ada/**" works when scanning a subdirectory (issue #611)', () => { |
| 194 | + // Create a sub-fixture: tmpDir/subdir/{ada,src} |
| 195 | + fs.mkdirSync(path.join(tmpDir, 'subdir', 'ada'), { recursive: true }); |
| 196 | + fs.mkdirSync(path.join(tmpDir, 'subdir', 'src'), { recursive: true }); |
| 197 | + const content = Array.from({ length: 10 }, (_, i) => `const v${i} = ${i};`).join('\n'); |
| 198 | + fs.writeFileSync(path.join(tmpDir, 'subdir', 'ada', 'ada.js'), content); |
| 199 | + fs.writeFileSync(path.join(tmpDir, 'subdir', 'src', 'main.js'), content); |
| 200 | + |
| 201 | + // Scan path is a relative subdirectory; user writes "./ada/**" meaning |
| 202 | + // "ada within what I am scanning", not "ada at cwd level". |
| 203 | + const files = getFilesToDetect(makeOptions('./subdir', ['./ada/**'])); |
| 204 | + const filePaths = files.map(f => f.path); |
| 205 | + expect(filePaths.some(p => p.includes('ada'))).toBe(false); |
| 206 | + expect(filePaths.some(p => p.includes('src'))).toBe(true); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
0 commit comments