Skip to content

fix(@angular-devkit/schematics): Fix merge that causes an overwrite #19878

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 1 commit into from
Jan 28, 2021
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
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
38 changes: 38 additions & 0 deletions packages/angular_devkit/schematics/src/tree/host-tree_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,47 @@
*/
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', 'a');
const tree2 = new HostTree();
tree2.create('/file2', 'a');
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', 'a');
const tree2 = new HostTree();
tree2.create('/file1', 'b');
tree.merge(tree2, MergeStrategy.Overwrite);
expect(tree.actions[0].kind).toEqual('c');
});

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

it('should not have a second action if the file content is the same', () => {
const tree = new HostTree();
tree.create('/file1', 'a');
const tree2 = new HostTree();
tree2.create('/file1', 'a');
tree.merge(tree2, MergeStrategy.Overwrite);
expect(tree.actions[0].kind).toEqual('c');
expect(tree.actions.length).toEqual(1);
});
});
});

describe('FilterHostTree', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { oneLine } from 'common-tags';
import { appendToFile } from '../../../utils/fs';
import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';
import { oneLine } from 'common-tags';

export default function () {
return ng('generate', 'component', 'test-component')
Expand All @@ -12,5 +13,6 @@ export default function () {
in ${output.stdout}.`);
}
})
.then(() => appendToFile('src/app/test-component/test-component.component.ts', '\n// new content'))
.then(() => expectToFail(() => ng('generate', 'component', 'test-component')));
}