Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): Fix merge that causes an overwrite
Browse files Browse the repository at this point in the history
This fixes angular#11337 to allow for merging of a tree with another when the the file already exists in the tree being merged into.
  • Loading branch information
Brocco committed Jan 26, 2021
1 parent cc51432 commit 788a2fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/angular_devkit/schematics/src/tree/host-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class HostDirEntry implements DirEntry {
readonly path: Path,
protected _host: virtualFs.SyncDelegateHost,
protected _tree: Tree,
) {}
) { }

get subdirs(): PathFragment[] {
return this._host.list(this.path)
Expand Down Expand Up @@ -180,7 +180,7 @@ export class HostTree implements Tree {
case 'c': {
const { path, content } = action;

if ((this._willCreate(path) || this._willOverwrite(path))) {
if ((this._willCreate(path) || this._willOverwrite(path) || this.exists(path))) {
const existingContent = this.read(path);
if (existingContent && content.equals(existingContent)) {
// Identical outcome; no action required
Expand Down
31 changes: 30 additions & 1 deletion packages/angular_devkit/schematics/src/tree/host-tree_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,43 @@
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* Use of this source code is governed by an ZIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { normalize, virtualFs } from '@angular-devkit/core';
import { FilterHostTree, HostTree } from './host-tree';
import { MergeStrategy } from './interface';

describe('HostTree', () => {
describe('merge', () => {
it('should create files from each tree', () => {
const tree = new HostTree();
tree.create('/file1', '');
const tree2 = new HostTree();
tree.create('/file2', '');
tree.merge(tree2);
expect(tree.actions[0].kind).toEqual('c');
expect(tree.actions[1].kind).toEqual('c');
});

it('should overwrite if the file exists in one tree', () => {
const tree = new HostTree();
tree.create('/file1', '');
const tree2 = new HostTree();
tree.create('/file1', '');
tree.merge(tree2, MergeStrategy.Overwrite);
expect(tree.actions[0].kind).toEqual('c');
expect(tree.actions[1].kind).toEqual('o');
});

it('should throw if the file exists in one tree', () => {
const tree = new HostTree();
tree.create('/file1', '');
const tree2 = new HostTree();
tree.create('/file1', '');
expect(() => tree.merge(tree2)).toThrow();
});
});
});

describe('FilterHostTree', () => {
Expand Down

0 comments on commit 788a2fa

Please sign in to comment.