Skip to content

Commit

Permalink
fix(@schematics/angular): buildRelativePath handles files in root
Browse files Browse the repository at this point in the history
Before, if one of the arguments was a file in root (eg "/module")
code would fail with: "" must be an absolute path.
  • Loading branch information
ukrukarg authored and mgechev committed Apr 1, 2019
1 parent 80e3d46 commit 21202f4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/angular_devkit/core/src/virtual-fs/path.ts
Expand Up @@ -144,8 +144,8 @@ export function relative(from: Path, to: Path): Path {
if (from == to) {
p = '';
} else {
const splitFrom = from.split(NormalizedSep);
const splitTo = to.split(NormalizedSep);
const splitFrom = split(from);
const splitTo = split(to);

while (splitFrom.length > 0 && splitTo.length > 0 && splitFrom[0] == splitTo[0]) {
splitFrom.shift();
Expand Down
3 changes: 3 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/path_spec.ts
Expand Up @@ -133,6 +133,8 @@ describe('path', () => {
'/src/app/sub1/test1', '/src/app/sub2/test2',
'../../sub2/test2',
],
['/', '/a/b/c', 'a/b/c'],
['/a/b/c', '/d', '../../../d'],
];

for (const [from, to, result] of tests) {
Expand All @@ -141,6 +143,7 @@ describe('path', () => {
const t = normalize(to);

expect(relative(f, t)).toBe(result);
expect(join(f, relative(f, t))).toBe(t);
});
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/angular/utility/find-module.ts
Expand Up @@ -130,7 +130,8 @@ export function buildRelativePath(from: string, to: string): string {
fromParts.pop();
const toFileName = toParts.pop();

const relativePath = relative(normalize(fromParts.join('/')), normalize(toParts.join('/')));
const relativePath = relative(normalize(fromParts.join('/') || '/'),
normalize(toParts.join('/') || '/'));
let pathPrefix = '';

// Set the path prefix for same dir or child dir, parent dir starts with `..`
Expand Down
15 changes: 14 additions & 1 deletion packages/schematics/angular/utility/find-module_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/
import { Path } from '@angular-devkit/core';
import { EmptyTree, Tree } from '@angular-devkit/schematics';
import { ModuleOptions, findModule, findModuleFromOptions } from './find-module';
import { ModuleOptions, buildRelativePath, findModule, findModuleFromOptions } from './find-module';


describe('find-module', () => {
Expand Down Expand Up @@ -189,4 +189,17 @@ describe('find-module', () => {
expect(modPath).toBe('/projects/my-proj/src/app.module.ts' as Path);
});
});

describe('buildRelativePath', () => {
it('works', () => {
expect(buildRelativePath('/test/module', '/test/service'))
.toEqual('./service');
expect(buildRelativePath('/test/module', '/other/service'))
.toEqual('../other/service');
expect(buildRelativePath('/module', '/test/service'))
.toEqual('./test/service');
expect(buildRelativePath('/test/service', '/module'))
.toEqual('../module');
});
});
});

0 comments on commit 21202f4

Please sign in to comment.