fix(devkit): tree.children should support writes to directories that have the same name as their parent (#16074)

This commit is contained in:
Craigory Coppola 2023-04-04 09:41:31 -04:00 committed by GitHub
parent cd2f41a110
commit 63e67bf142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -355,6 +355,14 @@ describe('tree', () => {
]);
});
it('should support nested dirs with same name as parent', () => {
tree.write('/parent-a/parent-a/parent-a-file.txt', 'parent content');
expect(tree.children('/parent-a')).toEqual(['parent-a']);
expect(tree.children('/parent-a/parent-a')).toEqual([
'parent-a-file.txt',
]);
});
describe('at the root', () => {
it('should return a list of children', () => {
expect(tree.children('')).toEqual(['parent', 'root-file.txt']);

View File

@ -403,8 +403,12 @@ export class FsTree implements Tree {
}
Object.keys(this.recordedChanges).forEach((f) => {
if (f.startsWith(`${path}/`)) {
const [_, file] = f.split(`${path}/`);
res[file.split('/')[0]] = true;
// Remove the current folder's path from the directory
const file = f.substring(path.length + 1);
// Split the path on segments, and take the first one
const basePath = file.split('/')[0];
// Mark it as a child of the current directory
res[basePath] = true;
}
});