Skip to content

Commit 694e312

Browse files
committed
fix(@angular-devkit/schematics): Fix merge that causes an overwrite
This fixes #11337 to allow for merging of a tree with another when the the file already exists in the tree being merged into.
1 parent cc51432 commit 694e312

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/angular_devkit/schematics/src/tree/host-tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class HostDirEntry implements DirEntry {
5656
readonly path: Path,
5757
protected _host: virtualFs.SyncDelegateHost,
5858
protected _tree: Tree,
59-
) {}
59+
) { }
6060

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

183-
if ((this._willCreate(path) || this._willOverwrite(path))) {
183+
if ((this._willCreate(path) || this._willOverwrite(path) || this.exists(path))) {
184184
const existingContent = this.read(path);
185185
if (existingContent && content.equals(existingContent)) {
186186
// Identical outcome; no action required

packages/angular_devkit/schematics/src/tree/host-tree_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,38 @@
77
*/
88
import { normalize, virtualFs } from '@angular-devkit/core';
99
import { FilterHostTree, HostTree } from './host-tree';
10+
import { MergeStrategy } from './interface';
1011

1112
describe('HostTree', () => {
13+
describe('merge', () => {
14+
it('should create files from each tree', () => {
15+
const tree = new HostTree();
16+
tree.create('/file1', '');
17+
const tree2 = new HostTree();
18+
tree.create('/file2', '');
19+
tree.merge(tree2);
20+
expect(tree.actions[0].kind).toEqual('c');
21+
expect(tree.actions[1].kind).toEqual('c');
22+
});
23+
24+
it('should overwrite if the file exists in one tree', () => {
25+
const tree = new HostTree();
26+
tree.create('/file1', '');
27+
const tree2 = new HostTree();
28+
tree.create('/file1', '');
29+
tree.merge(tree2, MergeStrategy.Overwrite);
30+
expect(tree.actions[0].kind).toEqual('c');
31+
expect(tree.actions[1].kind).toEqual('o');
32+
});
1233

34+
it('should throw if the file exists in one tree', () => {
35+
const tree = new HostTree();
36+
tree.create('/file1', '');
37+
const tree2 = new HostTree();
38+
tree.create('/file1', '');
39+
expect(() => tree.merge(tree2)).toThrow();
40+
});
41+
});
1342
});
1443

1544
describe('FilterHostTree', () => {

0 commit comments

Comments
 (0)