loqatevars
Version:
Locate JavaScript files with 'const' or 'process.env' usage in LLM-generated codebases
61 lines (53 loc) • 2.22 kB
JavaScript
const { findMatchingFiles } = require('../lib/utils');
const globby = require('globby').default;
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
jest.mock('globby', () => {
const mockGlobby = jest.fn();
mockGlobby.stream = jest.fn();
return { __esModule: true, default: mockGlobby, globby: mockGlobby, stream: mockGlobby.stream };
});
describe('Glob pattern tests', () => {
let tempDir;
beforeAll(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'glob-test-'));
fs.writeFileSync(path.join(tempDir, 'file1.js'), 'const a = 1;');
fs.writeFileSync(path.join(tempDir, 'file2.ts'), 'const b = 2;');
fs.writeFileSync(path.join(tempDir, 'file3.js'), 'const c = 3;');
});
afterAll(() => {
fs.removeSync(tempDir);
});
it('should find .js files only', async () => {
const mockStream = async function* () {
yield path.join(tempDir, 'file1.js');
yield path.join(tempDir, 'file3.js');
};
globby.stream.mockReturnValue(mockStream());
const matches = await findMatchingFiles(tempDir, 'ignore.txt', ['.js']);
expect(matches).toEqual(['file1.js', 'file3.js']);
});
it('should find .ts files only', async () => {
const mockStream = async function* () { yield path.join(tempDir, 'file2.ts'); };
globby.stream.mockReturnValue(mockStream());
const matches = await findMatchingFiles(tempDir, 'ignore.txt', ['.ts']);
expect(matches).toEqual(['file2.ts']);
});
it('should find both .js and .ts files', async () => {
const mockStream = async function* () {
yield path.join(tempDir, 'file1.js');
yield path.join(tempDir, 'file2.ts');
yield path.join(tempDir, 'file3.js');
};
globby.stream.mockReturnValue(mockStream());
const matches = await findMatchingFiles(tempDir, 'ignore.txt', ['.js', '.ts']);
expect(matches).toEqual(['file1.js', 'file2.ts', 'file3.js']);
});
it('should ignore specified files', async () => {
const mockStream = async function* () { yield path.join(tempDir, 'file3.js'); };
globby.stream.mockReturnValue(mockStream());
const matches = await findMatchingFiles(tempDir, ['file1.js', 'ignore.txt'], ['.js']);
expect(matches).toEqual(['file3.js']);
});
});