|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as fs from 'fs-extra'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { cleanSourcesForWatch, normalizeWatchFilePath } from '../../../../src/utils/utils'; |
| 6 | + |
| 7 | +describe('Watch path utilities', () => { |
| 8 | + describe('normalizeWatchFilePath', () => { |
| 9 | + it('should resolve relative paths from the provided base path', () => { |
| 10 | + const basePath = path.resolve(process.cwd(), 'test/fixtures'); |
| 11 | + const result = normalizeWatchFilePath('sample-files/index.ts', basePath); |
| 12 | + |
| 13 | + expect(result).to.equal(path.resolve(basePath, 'sample-files/index.ts')); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should keep absolute POSIX paths as-is', () => { |
| 17 | + const absolutePath = path.resolve(process.cwd(), 'test/fixtures/sample-files/index.ts'); |
| 18 | + const result = normalizeWatchFilePath(absolutePath, process.cwd()); |
| 19 | + |
| 20 | + expect(result).to.equal(path.normalize(absolutePath)); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should keep absolute Windows-style paths as-is', () => { |
| 24 | + const windowsPath = 'C:\\repo\\src\\app.component.ts'; |
| 25 | + const result = normalizeWatchFilePath(windowsPath, process.cwd()); |
| 26 | + |
| 27 | + expect(result).to.equal(path.normalize(windowsPath)); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('cleanSourcesForWatch', () => { |
| 32 | + const tempRoot = path.join(process.cwd(), '.tmp-watch-path-utils'); |
| 33 | + const absoluteFile = path.join(tempRoot, 'absolute.ts'); |
| 34 | + const relativeFile = '.tmp-watch-path-utils/relative.md'; |
| 35 | + |
| 36 | + before(() => { |
| 37 | + fs.ensureDirSync(tempRoot); |
| 38 | + fs.writeFileSync(absoluteFile, 'export const absolute = true;'); |
| 39 | + fs.writeFileSync(path.join(process.cwd(), relativeFile), '# relative'); |
| 40 | + }); |
| 41 | + |
| 42 | + after(() => { |
| 43 | + fs.removeSync(tempRoot); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should keep existing absolute and relative sources', () => { |
| 47 | + const result = cleanSourcesForWatch([absoluteFile, relativeFile, 'missing-file.ts']); |
| 48 | + |
| 49 | + expect(result).to.include(absoluteFile); |
| 50 | + expect(result).to.include(relativeFile); |
| 51 | + expect(result).to.not.include('missing-file.ts'); |
| 52 | + }); |
| 53 | + }); |
| 54 | +}); |
0 commit comments