Skip to content

Schematic rename/move fixes #11655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/angular_devkit/core/src/virtual-fs/host/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,16 @@ export class CordHost extends SimpleMemoryHost {
).pipe(
toArray(),
switchMap(([existTo, existFrom]) => {
if (existTo) {
return throwError(new FileAlreadyExistException(to));
}
if (!existFrom) {
return throwError(new FileDoesNotExistException(from));
}
if (from === to) {
return of();
}

if (existTo) {
return throwError(new FileAlreadyExistException(to));
}

// If we're renaming a file that's been created, shortcircuit to creating the `to` path.
if (this._filesToCreate.has(from)) {
Expand Down
23 changes: 23 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ describe('CordHost', () => {
done();
});

it('works (create -> rename (identity))', done => {
const base = new TestHost({
'/hello': 'world',
});

const host = new CordHost(base);
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
host.rename(path`/blue`, path`/blue`).subscribe(undefined, done.fail);

const target = new TestHost();
host.commit(target).subscribe(undefined, done.fail);

// Check that there's only 1 write done.
expect(target.records.filter(x => mutatingTestRecord.includes(x.kind))).toEqual([
{ kind: 'write', path: path`/blue` },
]);

expect(target.$exists('/hello')).toBe(false);
expect(target.$exists('/blue')).toBe(true);

done();
});

it('works (create -> rename -> rename)', done => {
const base = new TestHost({
'/hello': 'world',
Expand Down
5 changes: 5 additions & 0 deletions packages/angular_devkit/schematics/src/rules/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import { normalize } from '@angular-devkit/core';
import { Rule } from '../engine/interface';
import { noop } from './base';


export function move(from: string, to?: string): Rule {
Expand All @@ -18,6 +19,10 @@ export function move(from: string, to?: string): Rule {
const fromPath = normalize('/' + from);
const toPath = normalize('/' + to);

if (fromPath === toPath) {
return noop;
}

return tree => tree.visit(path => {
if (path.startsWith(fromPath)) {
tree.rename(path, toPath + '/' + path.substr(fromPath.length));
Expand Down
16 changes: 16 additions & 0 deletions packages/angular_devkit/schematics/src/rules/move_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ describe('move', () => {
})
.then(done, done.fail);
});

it('becomes a noop with identical from and to', done => {
const tree = new HostTree();
tree.create('a/b/file1', 'hello world');
tree.create('a/b/file2', 'hello world');
tree.create('a/c/file3', 'hello world');

callRule(move(''), observableOf(tree), context)
.toPromise()
.then(result => {
expect(result.exists('a/b/file1')).toBe(true);
expect(result.exists('a/b/file2')).toBe(true);
expect(result.exists('a/c/file3')).toBe(true);
})
.then(done, done.fail);
});
});