Skip to content

Commit e7fd1bd

Browse files
Thanatos9404alan-agius4
authored andcommitted
fix(@angular-devkit/schematics): support delegating trees in partition (#34539)
`partition()`, which backs the `partitionApplyMerge` rule, threw "Tree type is not supported." for a `UnitTestTree` and other delegating trees. Branch the tree they wrap instead, as `filter()` does. PR Close #34539
1 parent 1854c17 commit e7fd1bd

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎packages/angular_devkit/schematics/src/rules/base_spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,32 @@ describe('partitionApplyMerge', () => {
226226
expect(result.exists('/test1')).toBe(false);
227227
expect(result.exists('/test2')).toBe(false);
228228
});
229+
230+
it('works with a UnitTestTree', async () => {
231+
const tree = new HostTree();
232+
tree.create('/test1', '');
233+
tree.create('/test2', '');
234+
const predicate = (path: Path) => path.indexOf('1') != -1;
235+
236+
const ruleYes: Rule = (tree: Tree) => {
237+
expect(tree.exists('/test1')).toBe(true);
238+
expect(tree.exists('/test2')).toBe(false);
239+
};
240+
const ruleNo: Rule = (tree: Tree) => {
241+
expect(tree.exists('/test1')).toBe(false);
242+
expect(tree.exists('/test2')).toBe(true);
243+
};
244+
245+
const result = await lastValueFrom(
246+
callRule(
247+
partitionApplyMerge(predicate, ruleYes, ruleNo),
248+
observableOf(new UnitTestTree(new UnitTestTree(tree))),
249+
context,
250+
),
251+
);
252+
expect(result.exists('/test1')).toBe(true);
253+
expect(result.exists('/test2')).toBe(true);
254+
});
229255
});
230256

231257
describe('applyToSubtree', () => {

‎packages/angular_devkit/schematics/src/tree/static.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { SchematicsException } from '../exception/exception';
10+
import { DelegateTree } from './delegate';
1011
import { FilterHostTree, HostTree } from './host-tree';
1112
import { FilePredicate, MergeStrategy, Tree } from './interface';
1213

@@ -29,6 +30,10 @@ export function merge(
2930
}
3031

3132
export function partition(tree: Tree, predicate: FilePredicate<boolean>): [Tree, Tree] {
33+
if (tree instanceof DelegateTree) {
34+
tree = tree.branch();
35+
}
36+
3237
if (tree instanceof HostTree) {
3338
return [
3439
new FilterHostTree(tree, predicate),

0 commit comments

Comments
 (0)