Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
refactor: fix tests after new path semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Sep 15, 2017
1 parent b3aecaa commit 3d5805c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Schematic', () => {
collection,
name: 'test',
description: '',
path: 'a/b/c',
path: '/a/b/c',
factory: () => (tree: Tree) => {
inner = branch(tree);
tree.create('a/b/c', 'some content');
Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/schematics/src/rules/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function moveOp(from: string, to?: string): FileOperator {
from = '/';
}

const fromPath = normalize(from);
const toPath = normalize(to);
const fromPath = normalize('/' + from);
const toPath = normalize('/' + to);

return (entry: FileEntry) => {
if (entry.path.startsWith(fromPath)) {
Expand Down
22 changes: 11 additions & 11 deletions packages/angular_devkit/schematics/src/rules/template_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ describe('applyPathTemplate', () => {
}

it('works', () => {
expect(_applyPathTemplate('a/b/c/d', {})).toBe('/a/b/c/d');
expect(_applyPathTemplate('a/b/__c__/d', { c: 1 })).toBe('/a/b/1/d');
expect(_applyPathTemplate('a/b/__c__/d', { c: 'hello/world' })).toBe('/a/b/hello/world/d');
expect(_applyPathTemplate('a__c__b', { c: 'hello/world' })).toBe('/ahello/worldb');
expect(_applyPathTemplate('a__c__b__d__c', { c: '1', d: '2' })).toBe('/a1b2c');
expect(_applyPathTemplate('/a/b/c/d', {})).toBe('/a/b/c/d');
expect(_applyPathTemplate('/a/b/__c__/d', { c: 1 })).toBe('/a/b/1/d');
expect(_applyPathTemplate('/a/b/__c__/d', { c: 'hello/world' })).toBe('/a/b/hello/world/d');
expect(_applyPathTemplate('/a__c__b', { c: 'hello/world' })).toBe('/ahello/worldb');
expect(_applyPathTemplate('/a__c__b__d__c', { c: '1', d: '2' })).toBe('/a1b2c');
});

it('works with functions', () => {
let arg = '';
expect(_applyPathTemplate('a__c__b', {
expect(_applyPathTemplate('/a__c__b', {
c: (x: string) => (arg = x, 'hello'),
})).toBe('/ahellob');
expect(arg).toBe('/a__c__b');
Expand All @@ -61,13 +61,13 @@ describe('applyPathTemplate', () => {
let called = '';
let called2 = '';

expect(_applyPathTemplate('a__c@d__b', {
expect(_applyPathTemplate('/a__c@d__b', {
c: 1,
d: (x: string) => (called = x, 2),
})).toBe('/a2b');
expect(called).toBe('1');

expect(_applyPathTemplate('a__c@d@e__b', {
expect(_applyPathTemplate('/a__c@d@e__b', {
c: 10,
d: (x: string) => (called = x, 20),
e: (x: string) => (called2 = x, 30),
Expand All @@ -77,12 +77,12 @@ describe('applyPathTemplate', () => {
});

it('errors out on undefined values', () => {
expect(() => _applyPathTemplate('a__b__c', {})).toThrow(new OptionIsNotDefinedException('b'));
expect(() => _applyPathTemplate('/a__b__c', {})).toThrow(new OptionIsNotDefinedException('b'));
});

it('errors out on undefined or invalid pipes', () => {
expect(() => _applyPathTemplate('a__b@d__c', { b: 1 })).toThrow(new UnknownPipeException('d'));
expect(() => _applyPathTemplate('a__b@d__c', { b: 1, d: 1 }))
expect(() => _applyPathTemplate('/a__b@d__c', { b: 1 })).toThrow(new UnknownPipeException('d'));
expect(() => _applyPathTemplate('/a__b@d__c', { b: 1, d: 1 }))
.toThrow(new InvalidPipeException('d'));
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/schematics/src/tree/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class VirtualTree implements Tree {
* @returns {string} A path that is resolved and normalized.
*/
protected _normalizePath(path: string): Path {
return normalize(path);
return normalize('/' + path);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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 { Path, normalize, relative } from '@angular-devkit/core';
import { Path, normalize, relative, dirname } from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { dasherize } from '../strings';

Expand Down Expand Up @@ -37,7 +37,7 @@ export function findModuleFromOptions(host: Tree,
return normalize(findModule(host, pathToCheck));
} else {
const modulePath = normalize(
options.sourceDir + '/' + (options.appRoot || options.path) + '/' + options.module);
'/' + options.sourceDir + '/' + (options.appRoot || options.path) + '/' + options.module);
const moduleBaseName = normalize(modulePath).split('/').pop();

if (host.exists(modulePath)) {
Expand All @@ -58,19 +58,18 @@ export function findModuleFromOptions(host: Tree,
* Function to find the "closest" module to a generated file's path.
*/
export function findModule(host: Tree, generateDir: string): Path {
let closestModule: string = normalize(generateDir.replace(/[\\/]$/, ''));
let closestModule = normalize('/' + generateDir);
const allFiles = host.files;

let modulePath: string | null = null;
const moduleRe = /\.module\.ts$/;
const routingModuleRe = /-routing\.module\.ts/;

while (closestModule) {
const normalizedRoot = normalize(closestModule);
const matches = allFiles
.filter(p => moduleRe.test(p) &&
!routingModuleRe.test(p) &&
!/\//g.test(p.replace(normalizedRoot + '/', '')));
!/\//g.test(p.replace(closestModule + '/', '')));

if (matches.length == 1) {
modulePath = matches[0];
Expand All @@ -79,7 +78,7 @@ export function findModule(host: Tree, generateDir: string): Path {
throw new Error('More than one module matches. Use skip-import option to skip importing '
+ 'the component into the closest module.');
}
closestModule = closestModule.split('/').slice(0, -1).join('/');
closestModule = dirname(closestModule);
}

if (!modulePath) {
Expand Down

0 comments on commit 3d5805c

Please sign in to comment.