|
9 | 9 | ensureDirectoryExists,
|
10 | 10 | filePathToCliArg,
|
11 | 11 | findLineNumberInText,
|
| 12 | + findNearestFile, |
12 | 13 | logMultipleFileResults,
|
13 | 14 | projectToFilename,
|
14 | 15 | } from './file-system';
|
@@ -57,9 +58,9 @@ describe('crawlFileSystem', () => {
|
57 | 58 | beforeEach(() => {
|
58 | 59 | vol.fromJSON(
|
59 | 60 | {
|
60 |
| - ['README.md']: '# Markdown', |
61 |
| - ['src/README.md']: '# Markdown', |
62 |
| - ['src/index.ts']: 'const var = "markdown";', |
| 61 | + 'README.md': '# Markdown', |
| 62 | + 'src/README.md': '# Markdown', |
| 63 | + 'src/index.ts': 'const var = "markdown";', |
63 | 64 | },
|
64 | 65 | MEMFS_VOLUME,
|
65 | 66 | );
|
@@ -110,6 +111,108 @@ describe('crawlFileSystem', () => {
|
110 | 111 | });
|
111 | 112 | });
|
112 | 113 |
|
| 114 | +describe('findNearestFile', () => { |
| 115 | + it('should find file in current working directory', async () => { |
| 116 | + vol.fromJSON( |
| 117 | + { |
| 118 | + 'eslint.config.js': '', |
| 119 | + }, |
| 120 | + MEMFS_VOLUME, |
| 121 | + ); |
| 122 | + await expect(findNearestFile(['eslint.config.js'])).resolves.toBe( |
| 123 | + join(MEMFS_VOLUME, 'eslint.config.js'), |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should find first matching file in array', async () => { |
| 128 | + vol.fromJSON( |
| 129 | + { |
| 130 | + 'eslint.config.cjs': '', |
| 131 | + 'eslint.config.mjs': '', |
| 132 | + }, |
| 133 | + MEMFS_VOLUME, |
| 134 | + ); |
| 135 | + await expect( |
| 136 | + findNearestFile([ |
| 137 | + 'eslint.config.js', |
| 138 | + 'eslint.config.cjs', |
| 139 | + 'eslint.config.mjs', |
| 140 | + ]), |
| 141 | + ).resolves.toBe(join(MEMFS_VOLUME, 'eslint.config.cjs')); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should resolve to undefined if file not found', async () => { |
| 145 | + vol.fromJSON({ '.eslintrc.json': '' }, MEMFS_VOLUME); |
| 146 | + await expect( |
| 147 | + findNearestFile([ |
| 148 | + 'eslint.config.js', |
| 149 | + 'eslint.config.cjs', |
| 150 | + 'eslint.config.mjs', |
| 151 | + ]), |
| 152 | + ).resolves.toBeUndefined(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should find file in parent directory', async () => { |
| 156 | + vol.fromJSON( |
| 157 | + { |
| 158 | + 'eslint.config.js': '', |
| 159 | + 'e2e/main.spec.js': '', |
| 160 | + }, |
| 161 | + MEMFS_VOLUME, |
| 162 | + ); |
| 163 | + await expect( |
| 164 | + findNearestFile( |
| 165 | + ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'], |
| 166 | + join(MEMFS_VOLUME, 'e2e'), |
| 167 | + ), |
| 168 | + ).resolves.toBe(join(MEMFS_VOLUME, 'eslint.config.js')); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should find file in directory multiple levels up', async () => { |
| 172 | + vol.fromJSON( |
| 173 | + { |
| 174 | + 'eslint.config.cjs': '', |
| 175 | + 'packages/core/package.json': '', |
| 176 | + }, |
| 177 | + MEMFS_VOLUME, |
| 178 | + ); |
| 179 | + await expect( |
| 180 | + findNearestFile( |
| 181 | + ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'], |
| 182 | + join(MEMFS_VOLUME, 'packages/core'), |
| 183 | + ), |
| 184 | + ).resolves.toBe(join(MEMFS_VOLUME, 'eslint.config.cjs')); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should find file that's nearest to current folder", async () => { |
| 188 | + vol.fromJSON( |
| 189 | + { |
| 190 | + 'eslint.config.js': '', |
| 191 | + 'packages/core/eslint.config.js': '', |
| 192 | + 'packages/core/package.json': '', |
| 193 | + }, |
| 194 | + MEMFS_VOLUME, |
| 195 | + ); |
| 196 | + await expect( |
| 197 | + findNearestFile( |
| 198 | + ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'], |
| 199 | + join(MEMFS_VOLUME, 'packages/core'), |
| 200 | + ), |
| 201 | + ).resolves.toBe(join(MEMFS_VOLUME, 'packages/core/eslint.config.js')); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should not find file in sub-folders of current folder', async () => { |
| 205 | + vol.fromJSON({ 'packages/core/eslint.config.js': '' }, MEMFS_VOLUME); |
| 206 | + await expect( |
| 207 | + findNearestFile([ |
| 208 | + 'eslint.config.js', |
| 209 | + 'eslint.config.cjs', |
| 210 | + 'eslint.config.mjs', |
| 211 | + ]), |
| 212 | + ).resolves.toBeUndefined(); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
113 | 216 | describe('findLineNumberInText', () => {
|
114 | 217 | it('should return correct line number', () => {
|
115 | 218 | expect(
|
|
0 commit comments