Skip to content

Commit

Permalink
feat: detect grandchild dependency (#5094)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Oct 19, 2023
1 parent f22c15e commit de237d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/lib/features/dependent-features/dependent-features-service.ts
Expand Up @@ -96,18 +96,26 @@ export class DependentFeaturesService {
);
}

const [children, parentExists, sameProject] = await Promise.all([
this.dependentFeaturesReadModel.getChildren([child]),
this.featuresReadModel.featureExists(parent),
this.featuresReadModel.featuresInTheSameProject(child, parent),
]);

if (children.length > 0) {
const [grandchildren, grandparents, parentExists, sameProject] =
await Promise.all([
this.dependentFeaturesReadModel.getChildren([child]),
this.dependentFeaturesReadModel.getParents(parent),
this.featuresReadModel.featureExists(parent),
this.featuresReadModel.featuresInTheSameProject(child, parent),
]);

if (grandchildren.length > 0) {
throw new InvalidOperationError(
'Transitive dependency detected. Cannot add a dependency to the feature that other features depend on.',
);
}

if (grandparents.length > 0) {
throw new InvalidOperationError(
'Transitive dependency detected. Cannot add a dependency to the feature that has parent dependency.',
);
}

if (!parentExists) {
throw new InvalidOperationError(
`No active feature ${parent} exists`,
Expand Down
24 changes: 22 additions & 2 deletions src/lib/features/dependent-features/dependent.features.e2e.test.ts
Expand Up @@ -138,7 +138,7 @@ test('should add and delete feature dependencies', async () => {
]);
});

test('should not allow to add a parent dependency to a feature that already has children', async () => {
test('should not allow to add grandparent', async () => {
const grandparent = uuidv4();
const parent = uuidv4();
const child = uuidv4();
Expand All @@ -158,8 +158,28 @@ test('should not allow to add a parent dependency to a feature that already has
);
});

test('should not allow to add non-existent parent dependency', async () => {
test('should not allow to add grandchild', async () => {
const grandparent = uuidv4();
const parent = uuidv4();
const child = uuidv4();
await app.createFeature(grandparent);
await app.createFeature(parent);
await app.createFeature(child);

await addFeatureDependency(parent, {
feature: grandparent,
});

await addFeatureDependency(
child,
{
feature: parent,
},
403,
);
});

test('should not allow to add non-existent parent dependency', async () => {
const parent = uuidv4();
const child = uuidv4();
await app.createFeature(child);
Expand Down

0 comments on commit de237d8

Please sign in to comment.