Skip to content

Commit

Permalink
#69 Feature request: validate scopes - show error
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 20, 2024
1 parent 44263b8 commit 37c55af
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/LifetimesValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public bool Validate(DependencyGraph dependencyGraph)
return false;
}

var warnings = new HashSet<object>();
var errors = new HashSet<object>();
var graph = dependencyGraph.Graph;
foreach (var root in dependencyGraph.Roots)
{
pathsWalker.Walk(
warnings,
errors,
graph,
root.Value.Node,
visitor,
cancellationToken);
}

return true;
return errors.Count == 0;
}
}
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/LifetimesValidatorVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public bool Visit(HashSet<object> errors, in ImmutableArray<DependencyNode> path
var dependencyNode = path[i];
if (!ValidateLifetimes(actualTargetLifetimeNode.Lifetime, dependencyNode.Lifetime))
{
if (errors.Add(new WarningKey(actualTargetLifetimeNode, dependencyNode)))
if (errors.Add(new ErrorKey(actualTargetLifetimeNode, dependencyNode)))
{
logger.CompileWarning($"Type {actualTargetLifetimeNode.Type} with lifetime {actualTargetLifetimeNode.Lifetime} requires direct or transitive dependency injection of type {dependencyNode.Type} with lifetime {dependencyNode.Lifetime}, which can lead to data leakage and inconsistent behavior.", dependencyNode.Binding.Source.GetLocation(), LogId.WarningLifetimeDefect);
logger.CompileError($"Type {actualTargetLifetimeNode.Type} with lifetime {actualTargetLifetimeNode.Lifetime} requires direct or transitive dependency injectionion of type {dependencyNode.Type} with lifetime {dependencyNode.Lifetime}, which can lead to data leakage and inconsistent behavior.", dependencyNode.Binding.Source.GetLocation(), LogId.ErrorLifetimeDefect);
}
}

Expand All @@ -42,5 +42,5 @@ private static bool ValidateLifetimes(Lifetime actualTargetLifetime, Lifetime de
!(actualTargetLifetime == Lifetime.Singleton && dependencyLifetime == Lifetime.Scoped);

[SuppressMessage("ReSharper", "NotAccessedPositionalProperty.Local")]
private record WarningKey(DependencyNode TargetNode, DependencyNode SourceNode);
private record ErrorKey(DependencyNode TargetNode, DependencyNode SourceNode);
}
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/LogId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ internal static class LogId
public const string ErrorCannotFindSetup = "DIE002";
public const string ErrorCyclicDependency = "DIE003";
public const string ErrorNotSupportedLanguageVersion = "DIE004";
public const string ErrorLifetimeDefect = "DIW005";
public const string ErrorUnhandled = "DIE999";

// Warning
public const string WarningOverriddenBinding = "DIW000";
public const string WarningMetadataDefect = "DIW001";
public const string WarningRootArgInResolveMethod = "DIW002";
public const string WarningTypeArgInResolveMethod = "DIW003";
public const string WarningLifetimeDefect = "DIW004";

// Info
public const string InfoGenerationInterrupted = "DII000";
Expand Down
14 changes: 6 additions & 8 deletions tests/Pure.DI.IntegrationTests/LifetimesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,7 @@ public static void Main()
}

[Fact]
public async Task ShouldShowWarningWhenSingletonInjectsScoped()
public async Task ShouldShowErrorWhenSingletonInjectsScoped()
{
// Given

Expand Down Expand Up @@ -2661,13 +2661,12 @@ public static void Main()

// Then
result.Success.ShouldBeFalse(result);
result.StdOut.ShouldBe(["True", "True", "True", "True", "True", "True", "True", "True", "True"], result);
result.Warnings.Count.ShouldBe(1, result);
result.Warnings.Count(i => i.Id == Core.LogId.WarningLifetimeDefect).ShouldBe(1, result);
result.Errors.Count.ShouldBe(1, result);
result.Errors.Count(i => i.Id == Core.LogId.ErrorLifetimeDefect).ShouldBe(1, result);
}

[Fact]
public async Task ShouldShowWarningWhenSingletonIndirectInjectsScoped()
public async Task ShouldShowErrorWhenSingletonIndirectInjectsScoped()
{
// Given

Expand Down Expand Up @@ -2823,8 +2822,7 @@ public static void Main()

// Then
result.Success.ShouldBeFalse(result);
result.StdOut.ShouldBe(["True", "True", "True", "True", "True", "True", "True", "True", "True"], result);
result.Warnings.Count.ShouldBe(1, result);
result.Warnings.Count(i => i.Id == Core.LogId.WarningLifetimeDefect).ShouldBe(1, result);
result.Errors.Count.ShouldBe(1, result);
result.Errors.Count(i => i.Id == Core.LogId.ErrorLifetimeDefect).ShouldBe(1, result);
}
}

0 comments on commit 37c55af

Please sign in to comment.