forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-resolver.spec.ts
More file actions
169 lines (162 loc) · 8.32 KB
/
module-resolver.spec.ts
File metadata and controls
169 lines (162 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const mockFs = require('mock-fs');
import * as ts from 'typescript';
import * as path from 'path';
import * as dependentFilesUtils from '@angular/cli/utilities/get-dependent-files';
import { expect } from 'chai';
import { ModuleResolver } from '@angular/cli/utilities/module-resolver';
describe('ModuleResolver', () => {
let rootPath = 'src/app';
beforeEach(() => {
let mockDrive = {
'src/app': {
'foo': {
'foo.component.ts': `import * from "../bar/baz/baz.component";`,
},
'bar': {
'baz': {
'baz.component.ts': `import * from "../bar.component"
import * from '../../foo-baz/qux/quux/foobar/foobar.component'
`,
'baz.component.spec.ts': 'import * from "./baz.component";'
},
'bar.component.ts': `import * from './baz/baz.component'
import * from '../foo/foo.component'`,
},
'foo-baz': {
'qux': {
'quux': {
'foobar': {
'foobar.component.ts': `import * from "../../../../foo/foo.component"
import * from '../fooqux.component'
`,
},
'fooqux': {
'fooqux.component.ts': 'import * from "../foobar/foobar.component"'
}
}
},
'no-module.component.ts': '',
'foo-baz.component.ts': 'import * from \n"../foo/foo.component"\n'
},
'empty-dir': {}
}
};
mockFs(mockDrive);
});
afterEach(() => {
mockFs.restore();
});
describe('Rewrite imports', () => {
// Normalize paths for platform specific delimeter.
let barFile = path.join(rootPath, 'bar/bar.component.ts');
let fooFile = path.join(rootPath, 'foo/foo.component.ts');
let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts');
let fooBazFile = path.join(rootPath, 'foo-baz/foo-baz.component.ts');
let fooBarFile = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts');
let fooQuxFile = path.join(rootPath, 'foo-baz/qux/quux/fooqux/fooqux.component.ts');
it('when there is no index.ts in oldPath', () => {
let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts');
let newFilePath = path.join(rootPath, 'foo');
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
return resolver.resolveDependentFiles()
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(barFile))
.then((tsFileBar: ts.SourceFile) => {
let contentsBar = dependentFilesUtils.getImportClauses(tsFileBar);
let bazExpectedContent = path.normalize('../foo/baz.component');
expect(contentsBar[0].specifierText).to.equal(bazExpectedContent);
})
.then(() => dependentFilesUtils.createTsSourceFile(fooFile))
.then((tsFileFoo: ts.SourceFile) => {
let contentsFoo = dependentFilesUtils.getImportClauses(tsFileFoo);
let bazExpectedContent = './baz.component'.replace('/', path.sep);
expect(contentsFoo[0].specifierText).to.equal(bazExpectedContent);
})
.then(() => resolver.resolveOwnImports())
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(bazFile))
.then((tsFileBaz: ts.SourceFile) => {
let contentsBaz = dependentFilesUtils.getImportClauses(tsFileBaz);
let barExpectedContent = path.normalize('../bar/bar.component');
let fooBarExpectedContent = path.normalize('../foo-baz/qux/quux/foobar/foobar.component');
expect(contentsBaz[0].specifierText).to.equal(barExpectedContent);
expect(contentsBaz[1].specifierText).to.equal(fooBarExpectedContent);
});
});
it('when no files are importing the given file', () => {
let oldFilePath = path.join(rootPath, 'foo-baz/foo-baz.component.ts');
let newFilePath = path.join(rootPath, 'bar');
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
return resolver.resolveDependentFiles()
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => resolver.resolveOwnImports())
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(fooBazFile))
.then((tsFile: ts.SourceFile) => {
let contents = dependentFilesUtils.getImportClauses(tsFile);
let fooExpectedContent = path.normalize('../foo/foo.component');
expect(contents[0].specifierText).to.equal(fooExpectedContent);
});
});
it('when oldPath and newPath both do not have index.ts', () => {
let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts');
let newFilePath = path.join(rootPath, 'foo-baz');
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
return resolver.resolveDependentFiles()
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(barFile))
.then((tsFileBar: ts.SourceFile) => {
let contentsBar = dependentFilesUtils.getImportClauses(tsFileBar);
let bazExpectedContent = path.normalize('../foo-baz/baz.component');
expect(contentsBar[0].specifierText).to.equal(bazExpectedContent);
})
.then(() => dependentFilesUtils.createTsSourceFile(fooFile))
.then((tsFileFoo: ts.SourceFile) => {
let contentsFoo = dependentFilesUtils.getImportClauses(tsFileFoo);
let bazExpectedContent = path.normalize('../foo-baz/baz.component');
expect(contentsFoo[0].specifierText).to.equal(bazExpectedContent);
})
.then(() => resolver.resolveOwnImports())
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(bazFile))
.then((tsFile: ts.SourceFile) => {
let contentsBaz = dependentFilesUtils.getImportClauses(tsFile);
let barExpectedContent = path.normalize('../bar/bar.component');
let fooBarExpectedContent =
`.${path.sep}qux${path.sep}quux${path.sep}foobar${path.sep}foobar.component`;
expect(contentsBaz[0].specifierText).to.equal(barExpectedContent);
expect(contentsBaz[1].specifierText).to.equal(fooBarExpectedContent);
});
});
it('when there are multiple spaces between symbols and specifier', () => {
let oldFilePath = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts');
let newFilePath = path.join(rootPath, 'foo');
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
return resolver.resolveDependentFiles()
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(fooQuxFile))
.then((tsFileFooQux: ts.SourceFile) => {
let contentsFooQux = dependentFilesUtils.getImportClauses(tsFileFooQux);
let fooQuxExpectedContent = path.normalize('../../../../foo/foobar.component');
expect(contentsFooQux[0].specifierText).to.equal(fooQuxExpectedContent);
})
.then(() => dependentFilesUtils.createTsSourceFile(bazFile))
.then((tsFileBaz: ts.SourceFile) => {
let contentsBaz = dependentFilesUtils.getImportClauses(tsFileBaz);
let bazExpectedContent = path.normalize('../../foo/foobar.component');
expect(contentsBaz[1].specifierText).to.equal(bazExpectedContent);
})
.then(() => resolver.resolveOwnImports())
.then((changes) => resolver.applySortedChangePromise(changes))
.then(() => dependentFilesUtils.createTsSourceFile(fooBarFile))
.then((tsFileFooBar: ts.SourceFile) => {
let contentsFooBar = dependentFilesUtils.getImportClauses(tsFileFooBar);
let fooExpectedContent = `.${path.sep}foo.component`;
let fooQuxExpectedContent = path.normalize('../foo-baz/qux/quux/fooqux.component');
expect(contentsFooBar[0].specifierText).to.equal(fooExpectedContent);
expect(contentsFooBar[1].specifierText).to.equal(fooQuxExpectedContent);
});
});
});
});