|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { test, after } from 'node:test'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import { execFileSync } from 'node:child_process'; |
| 6 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 7 | +import { tmpdir } from 'node:os'; |
| 8 | +import { dirname, join } from 'node:path'; |
| 9 | +import { headerSource, listFiles } from '../files.mjs'; |
| 10 | + |
| 11 | +const tempDirs = []; |
| 12 | +after(() => { |
| 13 | + for (const dir of tempDirs) rmSync(dir, { recursive: true, force: true }); |
| 14 | +}); |
| 15 | + |
| 16 | +function writeFiles(root, paths) { |
| 17 | + for (const path of paths) { |
| 18 | + const full = join(root, ...path.split('/')); |
| 19 | + mkdirSync(dirname(full), { recursive: true }); |
| 20 | + writeFileSync(full, ''); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function makeRepo(paths) { |
| 25 | + const root = mkdtempSync(join(tmpdir(), 'spdx-files-')); |
| 26 | + tempDirs.push(root); |
| 27 | + execFileSync('git', ['init', '-q'], { cwd: root }); |
| 28 | + writeFiles(root, paths); |
| 29 | + execFileSync('git', ['add', '-A'], { cwd: root }); |
| 30 | + return root; |
| 31 | +} |
| 32 | + |
| 33 | +function sorted(list) { |
| 34 | + return [...list].sort(); |
| 35 | +} |
| 36 | + |
| 37 | +test('*.py matches at root and at depth', () => { |
| 38 | + const cwd = makeRepo(['a.py', 'pkg/b.py', 'pkg/sub/c.py', 'README.md']); |
| 39 | + const files = listFiles({ include: ['*.py'], cwd }); |
| 40 | + assert.deepEqual(sorted(files), ['a.py', 'pkg/b.py', 'pkg/sub/c.py']); |
| 41 | +}); |
| 42 | + |
| 43 | +test('Dockerfile* matches root-level files only', () => { |
| 44 | + const cwd = makeRepo(['Dockerfile', 'Dockerfile.dev', 'deploy/Dockerfile']); |
| 45 | + const files = listFiles({ include: ['Dockerfile*'], cwd }); |
| 46 | + assert.deepEqual(sorted(files), ['Dockerfile', 'Dockerfile.dev']); |
| 47 | +}); |
| 48 | + |
| 49 | +test('*/Dockerfile* matches nested files only', () => { |
| 50 | + const cwd = makeRepo(['Dockerfile', 'deploy/Dockerfile', 'deploy/Dockerfile.prod']); |
| 51 | + const files = listFiles({ include: ['*/Dockerfile*'], cwd }); |
| 52 | + assert.deepEqual(sorted(files), ['deploy/Dockerfile', 'deploy/Dockerfile.prod']); |
| 53 | +}); |
| 54 | + |
| 55 | +test('scripts/bin/* matches an extensionless file', () => { |
| 56 | + const cwd = makeRepo(['scripts/bin/run', 'scripts/other']); |
| 57 | + const files = listFiles({ include: ['scripts/bin/*'], cwd }); |
| 58 | + assert.deepEqual(files, ['scripts/bin/run']); |
| 59 | +}); |
| 60 | + |
| 61 | +test('excludes remove matching files', () => { |
| 62 | + const cwd = makeRepo(['a.py', 'third_party/x.py', 'proto/m_pb2.py']); |
| 63 | + const files = listFiles({ |
| 64 | + include: ['*.py'], |
| 65 | + exclude: ['third_party/*', '*_pb2.py'], |
| 66 | + cwd, |
| 67 | + }); |
| 68 | + assert.deepEqual(files, ['a.py']); |
| 69 | +}); |
| 70 | + |
| 71 | +test('untracked files are ignored', () => { |
| 72 | + const cwd = makeRepo(['a.py']); |
| 73 | + writeFiles(cwd, ['b.py']); |
| 74 | + assert.deepEqual(listFiles({ include: ['*.py'], cwd }), ['a.py']); |
| 75 | +}); |
| 76 | + |
| 77 | +test('multiple include patterns combine', () => { |
| 78 | + const cwd = makeRepo(['a.py', 'b.mjs', 'c.md']); |
| 79 | + const files = listFiles({ include: ['*.py', '*.mjs'], cwd }); |
| 80 | + assert.deepEqual(sorted(files), ['a.py', 'b.mjs']); |
| 81 | +}); |
| 82 | + |
| 83 | +test('headerSource returns the sidecar when it exists', () => { |
| 84 | + const cwd = makeRepo(['x.json', 'x.json.license']); |
| 85 | + assert.equal(headerSource('x.json', cwd), 'x.json.license'); |
| 86 | +}); |
| 87 | + |
| 88 | +test('headerSource returns the path when there is no sidecar', () => { |
| 89 | + const cwd = makeRepo(['y.json']); |
| 90 | + assert.equal(headerSource('y.json', cwd), 'y.json'); |
| 91 | +}); |
| 92 | + |
| 93 | +test('headerSource works for nested paths', () => { |
| 94 | + const cwd = makeRepo(['conf/x.json', 'conf/x.json.license']); |
| 95 | + assert.equal(headerSource('conf/x.json', cwd), 'conf/x.json.license'); |
| 96 | +}); |
0 commit comments