feat(ivy): introduce concrete types for paths in ngtsc (#28523)
This commit introduces a new ngtsc sub-library, 'path', which contains branded string types for the different kind of paths that ngtsc manipulates. Having static types for these paths will reduce the number of path-related bugs (especially on Windows) and will eliminate unnecessary defensive normalizing. See the README.md file for more detail. PR Close #28523
This commit is contained in:

committed by
Misko Hevery

parent
99d8582882
commit
a529f53031
53
packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts
Normal file
53
packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts
Normal file
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {LogicalFileSystem, LogicalProjectPath} from '../src/logical';
|
||||
import {AbsoluteFsPath} from '../src/types';
|
||||
|
||||
describe('logical paths', () => {
|
||||
describe('LogicalFileSystem', () => {
|
||||
it('should determine logical paths in a single root file system', () => {
|
||||
const fs = new LogicalFileSystem([abs('/test')]);
|
||||
expect(fs.logicalPathOfFile(abs('/test/foo/foo.ts')))
|
||||
.toEqual('/foo/foo' as LogicalProjectPath);
|
||||
expect(fs.logicalPathOfFile(abs('/test/bar/bar.ts')))
|
||||
.toEqual('/bar/bar' as LogicalProjectPath);
|
||||
expect(fs.logicalPathOfFile(abs('/not-test/bar.ts'))).toBeNull();
|
||||
});
|
||||
|
||||
it('should determine logical paths in a multi-root file system', () => {
|
||||
const fs = new LogicalFileSystem([abs('/test/foo'), abs('/test/bar')]);
|
||||
expect(fs.logicalPathOfFile(abs('/test/foo/foo.ts'))).toEqual('/foo' as LogicalProjectPath);
|
||||
expect(fs.logicalPathOfFile(abs('/test/bar/bar.ts'))).toEqual('/bar' as LogicalProjectPath);
|
||||
});
|
||||
|
||||
it('should continue to work when one root is a child of another', () => {
|
||||
const fs = new LogicalFileSystem([abs('/test'), abs('/test/dist')]);
|
||||
expect(fs.logicalPathOfFile(abs('/test/foo.ts'))).toEqual('/foo' as LogicalProjectPath);
|
||||
expect(fs.logicalPathOfFile(abs('/test/dist/foo.ts'))).toEqual('/foo' as LogicalProjectPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('utilities', () => {
|
||||
it('should give a relative path between two adjacent logical files', () => {
|
||||
const res = LogicalProjectPath.relativePathBetween(
|
||||
'/foo' as LogicalProjectPath, '/bar' as LogicalProjectPath);
|
||||
expect(res).toEqual('./bar');
|
||||
});
|
||||
|
||||
it('should give a relative path between two non-adjacent logical files', () => {
|
||||
const res = LogicalProjectPath.relativePathBetween(
|
||||
'/foo/index' as LogicalProjectPath, '/bar/index' as LogicalProjectPath);
|
||||
expect(res).toEqual('../bar/index');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function abs(file: string): AbsoluteFsPath {
|
||||
return AbsoluteFsPath.from(file);
|
||||
}
|
Reference in New Issue
Block a user