Alex Rickabaugh a529f53031 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
2019-02-13 19:13:11 -08:00

54 lines
2.2 KiB
TypeScript

/**
* @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);
}