From a50f77604d2ecdde26f83b8aed8319cbb3d73ba4 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 25 Jul 2018 12:28:24 -0400 Subject: [PATCH 1/2] fix(@angular-devkit/core): rename to itself should be a noop --- .../core/src/virtual-fs/host/record.ts | 10 +++++--- .../core/src/virtual-fs/host/record_spec.ts | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/angular_devkit/core/src/virtual-fs/host/record.ts b/packages/angular_devkit/core/src/virtual-fs/host/record.ts index a03276e45f48..9c7d2597061d 100644 --- a/packages/angular_devkit/core/src/virtual-fs/host/record.ts +++ b/packages/angular_devkit/core/src/virtual-fs/host/record.ts @@ -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)) { diff --git a/packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts b/packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts index 96ccf4ed7503..96b6b0e43eaa 100644 --- a/packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts +++ b/packages/angular_devkit/core/src/virtual-fs/host/record_spec.ts @@ -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', From 4eb0bda6fb3947739210357ce6026112bf3948e7 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 25 Jul 2018 12:35:41 -0400 Subject: [PATCH 2/2] fix(@angular-devkit/schematics): move rule with identity is a noop --- .../angular_devkit/schematics/src/rules/move.ts | 5 +++++ .../schematics/src/rules/move_spec.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/angular_devkit/schematics/src/rules/move.ts b/packages/angular_devkit/schematics/src/rules/move.ts index b22e29b7cd76..96cfe15737fc 100644 --- a/packages/angular_devkit/schematics/src/rules/move.ts +++ b/packages/angular_devkit/schematics/src/rules/move.ts @@ -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 { @@ -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)); diff --git a/packages/angular_devkit/schematics/src/rules/move_spec.ts b/packages/angular_devkit/schematics/src/rules/move_spec.ts index 113e90769fff..c9c67a23b357 100644 --- a/packages/angular_devkit/schematics/src/rules/move_spec.ts +++ b/packages/angular_devkit/schematics/src/rules/move_spec.ts @@ -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); + }); });