|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import { findConfigFile } from '../../../../src/core/helpers/findConfigFile.js'; |
| 6 | + |
| 7 | +describe('findConfigFile', () => { |
| 8 | + let tmpDir: string; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'find-config-test-')); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns the path when the config file exists in the given directory', () => { |
| 19 | + const file = 'dotenv-diff.config.json'; |
| 20 | + fs.writeFileSync(path.join(tmpDir, file), '{}'); |
| 21 | + |
| 22 | + const result = findConfigFile(tmpDir, file); |
| 23 | + |
| 24 | + expect(result).toBe(path.resolve(tmpDir, file)); |
| 25 | + }); |
| 26 | + |
| 27 | + it('finds the config file in a parent directory', () => { |
| 28 | + const file = 'dotenv-diff.config.json'; |
| 29 | + fs.writeFileSync(path.join(tmpDir, file), '{}'); |
| 30 | + |
| 31 | + const child = path.join(tmpDir, 'subdir'); |
| 32 | + fs.mkdirSync(child); |
| 33 | + |
| 34 | + const result = findConfigFile(child, file); |
| 35 | + |
| 36 | + expect(result).toBe(path.resolve(tmpDir, file)); |
| 37 | + }); |
| 38 | + |
| 39 | + it('finds the config file multiple levels up', () => { |
| 40 | + const file = 'dotenv-diff.config.json'; |
| 41 | + fs.writeFileSync(path.join(tmpDir, file), '{}'); |
| 42 | + |
| 43 | + const deep = path.join(tmpDir, 'a', 'b', 'c'); |
| 44 | + fs.mkdirSync(deep, { recursive: true }); |
| 45 | + |
| 46 | + const result = findConfigFile(deep, file); |
| 47 | + |
| 48 | + expect(result).toBe(path.resolve(tmpDir, file)); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns null when the config file does not exist anywhere in the tree', () => { |
| 52 | + const child = path.join(tmpDir, 'subdir'); |
| 53 | + fs.mkdirSync(child); |
| 54 | + |
| 55 | + const result = findConfigFile(child, 'nonexistent.config.json'); |
| 56 | + |
| 57 | + expect(result).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('prefers the config file in the starting directory over a parent one', () => { |
| 61 | + const file = 'dotenv-diff.config.json'; |
| 62 | + fs.writeFileSync(path.join(tmpDir, file), '{"root":true}'); |
| 63 | + |
| 64 | + const child = path.join(tmpDir, 'subdir'); |
| 65 | + fs.mkdirSync(child); |
| 66 | + fs.writeFileSync(path.join(child, file), '{"child":true}'); |
| 67 | + |
| 68 | + const result = findConfigFile(child, file); |
| 69 | + |
| 70 | + expect(result).toBe(path.resolve(child, file)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('works with different file names', () => { |
| 74 | + const file = 'dotenv-diff.baseline.json'; |
| 75 | + fs.writeFileSync(path.join(tmpDir, file), '{}'); |
| 76 | + |
| 77 | + const result = findConfigFile(tmpDir, file); |
| 78 | + |
| 79 | + expect(result).toBe(path.resolve(tmpDir, file)); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns null when starting from a directory with no ancestors containing the file', () => { |
| 83 | + // Use a real path that definitely won't have the file |
| 84 | + const result = findConfigFile( |
| 85 | + os.tmpdir(), |
| 86 | + '__nonexistent_config_xyz__.json', |
| 87 | + ); |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments