To improve cross platform support, all file access (and path manipulation) is now done through a well known interface (`FileSystem`). For testing a number of `MockFileSystem` implementations are provided. These provide an in-memory file-system which emulates operating systems like OS/X, Unix and Windows. The current file system is always available via the static method, `FileSystem.getFileSystem()`. This is also used by a number of static methods on `AbsoluteFsPath` and `PathSegment`, to avoid having to pass `FileSystem` objects around all the time. The result of this is that one must be careful to ensure that the file-system has been initialized before using any of these static methods. To prevent this happening accidentally the current file system always starts out as an instance of `InvalidFileSystem`, which will throw an error if any of its methods are called. You can set the current file-system by calling `FileSystem.setFileSystem()`. During testing you can call the helper function `initMockFileSystem(os)` which takes a string name of the OS to emulate, and will also monkey-patch aspects of the TypeScript library to ensure that TS is also using the current file-system. Finally there is the `NgtscCompilerHost` to be used for any TypeScript compilation, which uses a given file-system. All tests that interact with the file-system should be tested against each of the mock file-systems. A series of helpers have been provided to support such tests: * `runInEachFileSystem()` - wrap your tests in this helper to run all the wrapped tests in each of the mock file-systems. * `addTestFilesToFileSystem()` - use this to add files and their contents to the mock file system for testing. * `loadTestFilesFromDisk()` - use this to load a mirror image of files on disk into the in-memory mock file-system. * `loadFakeCore()` - use this to load a fake version of `@angular/core` into the mock file-system. All ngcc and ngtsc source and tests now use this virtual file-system setup. PR Close #30921
206 lines
9.9 KiB
TypeScript
206 lines
9.9 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 {absoluteFrom, getFileSystem} from '../../../src/ngtsc/file_system';
|
|
import {runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
|
import {loadTestFiles} from '../../../test/helpers';
|
|
import {ModuleResolver, ResolvedDeepImport, ResolvedExternalModule, ResolvedRelativeModule} from '../../src/dependencies/module_resolver';
|
|
|
|
runInEachFileSystem(() => {
|
|
describe('ModuleResolver', () => {
|
|
let _: typeof absoluteFrom;
|
|
|
|
beforeEach(() => {
|
|
_ = absoluteFrom;
|
|
loadTestFiles([
|
|
{name: _('/libs/local-package/package.json'), contents: 'PACKAGE.JSON for local-package'},
|
|
{name: _('/libs/local-package/index.js'), contents: `import {X} from './x';`},
|
|
{name: _('/libs/local-package/x.js'), contents: `export class X {}`},
|
|
{name: _('/libs/local-package/sub-folder/index.js'), contents: `import {X} from '../x';`},
|
|
{
|
|
name: _('/libs/local-package/node_modules/package-1/sub-folder/index.js'),
|
|
contents: `export class Z {}`
|
|
},
|
|
{
|
|
name: _('/libs/local-package/node_modules/package-1/package.json'),
|
|
contents: 'PACKAGE.JSON for package-1'
|
|
},
|
|
{
|
|
name: _('/libs/node_modules/package-2/package.json'),
|
|
contents: 'PACKAGE.JSON for package-2'
|
|
},
|
|
{
|
|
name: _('/libs/node_modules/package-2/node_modules/package-3/package.json'),
|
|
contents: 'PACKAGE.JSON for package-3'
|
|
},
|
|
{name: _('/dist/package-4/x.js'), contents: `export class X {}`},
|
|
{name: _('/dist/package-4/package.json'), contents: 'PACKAGE.JSON for package-4'},
|
|
{
|
|
name: _('/dist/package-4/sub-folder/index.js'),
|
|
contents: `import {X} from '@shared/package-4/x';`
|
|
},
|
|
{
|
|
name: _('/dist/sub-folder/package-4/package.json'),
|
|
contents: 'PACKAGE.JSON for package-4'
|
|
},
|
|
{
|
|
name: _('/dist/sub-folder/package-5/package.json'),
|
|
contents: 'PACKAGE.JSON for package-5'
|
|
},
|
|
{
|
|
name: _('/dist/sub-folder/package-5/post-fix/package.json'),
|
|
contents: 'PACKAGE.JSON for package-5/post-fix'
|
|
},
|
|
{
|
|
name: _('/node_modules/top-package/package.json'),
|
|
contents: 'PACKAGE.JSON for top-package'
|
|
},
|
|
]);
|
|
});
|
|
|
|
describe('resolveModule()', () => {
|
|
describe('with relative paths', () => {
|
|
it('should resolve sibling, child and aunt modules', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('./x', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedRelativeModule(_('/libs/local-package/x.js')));
|
|
expect(resolver.resolveModuleImport('./sub-folder', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedRelativeModule(_('/libs/local-package/sub-folder/index.js')));
|
|
expect(resolver.resolveModuleImport('../x', _('/libs/local-package/sub-folder/index.js')))
|
|
.toEqual(new ResolvedRelativeModule(_('/libs/local-package/x.js')));
|
|
});
|
|
|
|
it('should return `null` if the resolved module relative module does not exist', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('./y', _('/libs/local-package/index.js'))).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe('with non-mapped external paths', () => {
|
|
it('should resolve to the package.json of a local node_modules package', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('package-1', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/libs/local-package/node_modules/package-1')));
|
|
expect(resolver.resolveModuleImport(
|
|
'package-1', _('/libs/local-package/sub-folder/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/libs/local-package/node_modules/package-1')));
|
|
expect(resolver.resolveModuleImport('package-1', _('/libs/local-package/x.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/libs/local-package/node_modules/package-1')));
|
|
});
|
|
|
|
it('should resolve to the package.json of a higher node_modules package', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('package-2', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/libs/node_modules/package-2')));
|
|
expect(resolver.resolveModuleImport('top-package', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/node_modules/top-package')));
|
|
});
|
|
|
|
it('should return `null` if the package cannot be found', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('missing-2', _('/libs/local-package/index.js')))
|
|
.toBe(null);
|
|
});
|
|
|
|
it('should return `null` if the package is not accessible because it is in a inner node_modules package',
|
|
() => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport('package-3', _('/libs/local-package/index.js')))
|
|
.toBe(null);
|
|
});
|
|
|
|
it('should identify deep imports into an external module', () => {
|
|
const resolver = new ModuleResolver(getFileSystem());
|
|
expect(resolver.resolveModuleImport(
|
|
'package-1/sub-folder', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedDeepImport(
|
|
_('/libs/local-package/node_modules/package-1/sub-folder')));
|
|
});
|
|
});
|
|
|
|
describe('with mapped path external modules', () => {
|
|
it('should resolve to the package.json of simple mapped packages', () => {
|
|
const resolver = new ModuleResolver(
|
|
getFileSystem(), {baseUrl: '/dist', paths: {'*': ['*', 'sub-folder/*']}});
|
|
|
|
expect(resolver.resolveModuleImport('package-4', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/package-4')));
|
|
|
|
expect(resolver.resolveModuleImport('package-5', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-5')));
|
|
});
|
|
|
|
it('should select the best match by the length of prefix before the *', () => {
|
|
const resolver = new ModuleResolver(getFileSystem(), {
|
|
baseUrl: '/dist',
|
|
paths: {
|
|
'@lib/*': ['*'],
|
|
'@lib/sub-folder/*': ['*'],
|
|
}
|
|
});
|
|
|
|
// We should match the second path (e.g. `'@lib/sub-folder/*'`), which will actually map
|
|
// to `*` and so the final resolved path will not include the `sub-folder` segment.
|
|
expect(resolver.resolveModuleImport(
|
|
'@lib/sub-folder/package-4', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/package-4')));
|
|
});
|
|
|
|
it('should follow the ordering of `paths` when matching mapped packages', () => {
|
|
let resolver: ModuleResolver;
|
|
|
|
const fs = getFileSystem();
|
|
resolver =
|
|
new ModuleResolver(fs, {baseUrl: '/dist', paths: {'*': ['*', 'sub-folder/*']}});
|
|
expect(resolver.resolveModuleImport('package-4', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/package-4')));
|
|
|
|
resolver =
|
|
new ModuleResolver(fs, {baseUrl: '/dist', paths: {'*': ['sub-folder/*', '*']}});
|
|
expect(resolver.resolveModuleImport('package-4', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-4')));
|
|
});
|
|
|
|
it('should resolve packages when the path mappings have post-fixes', () => {
|
|
const resolver = new ModuleResolver(
|
|
getFileSystem(), {baseUrl: '/dist', paths: {'*': ['sub-folder/*/post-fix']}});
|
|
expect(resolver.resolveModuleImport('package-5', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-5/post-fix')));
|
|
});
|
|
|
|
it('should match paths against complex path matchers', () => {
|
|
const resolver = new ModuleResolver(
|
|
getFileSystem(), {baseUrl: '/dist', paths: {'@shared/*': ['sub-folder/*']}});
|
|
expect(
|
|
resolver.resolveModuleImport('@shared/package-4', _('/libs/local-package/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-4')));
|
|
expect(resolver.resolveModuleImport('package-5', _('/libs/local-package/index.js')))
|
|
.toBe(null);
|
|
});
|
|
|
|
it('should resolve path as "relative" if the mapped path is inside the current package',
|
|
() => {
|
|
const resolver = new ModuleResolver(
|
|
getFileSystem(), {baseUrl: '/dist', paths: {'@shared/*': ['*']}});
|
|
expect(resolver.resolveModuleImport(
|
|
'@shared/package-4/x', _('/dist/package-4/sub-folder/index.js')))
|
|
.toEqual(new ResolvedRelativeModule(_('/dist/package-4/x.js')));
|
|
});
|
|
|
|
it('should resolve paths where the wildcard matches more than one path segment', () => {
|
|
const resolver = new ModuleResolver(
|
|
getFileSystem(), {baseUrl: '/dist', paths: {'@shared/*/post-fix': ['*/post-fix']}});
|
|
expect(resolver.resolveModuleImport(
|
|
'@shared/sub-folder/package-5/post-fix',
|
|
_('/dist/package-4/sub-folder/index.js')))
|
|
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-5/post-fix')));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|