Skip to content

Commit 418c005

Browse files
committed
Defensive approach to avoid duplicates. Duplicate check test
1 parent fef29e3 commit 418c005

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

libraries/src/AWS.Lambda.Powertools.Common/Core/SystemWrapper.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,25 @@ public void SetEnvironmentVariable(string variable, string value)
9494
public void SetExecutionEnvironment<T>(T type)
9595
{
9696
const string envName = Constants.AwsExecutionEnvironmentVariableName;
97-
9897
var envValue = new StringBuilder();
99-
98+
var currentEnvValue = GetEnvironmentVariable(envName);
99+
var assemblyName = ParseAssemblyName(_powertoolsEnvironment.GetAssemblyName(type));
100+
100101
// If there is an existing execution environment variable add the annotations package as a suffix.
101-
if(!string.IsNullOrEmpty(GetEnvironmentVariable(envName)))
102+
if(!string.IsNullOrEmpty(currentEnvValue))
102103
{
103-
envValue.Append($"{GetEnvironmentVariable(envName)} ");
104+
// Avoid duplication - should not happen since the calling Instances are Singletons - defensive purposes
105+
if (currentEnvValue.Contains(assemblyName))
106+
{
107+
return;
108+
}
109+
110+
envValue.Append($"{currentEnvValue} ");
104111
}
105112

106113
var assemblyVersion = _powertoolsEnvironment.GetAssemblyVersion(type);
107-
var assemblyName =_powertoolsEnvironment.GetAssemblyName(type);
108-
109-
envValue.Append($"{ParseAssemblyName(assemblyName)}/{assemblyVersion}");
114+
115+
envValue.Append($"{assemblyName}/{assemblyVersion}");
110116

111117
SetEnvironmentVariable(envName, envValue.ToString());
112118
}

libraries/tests/AWS.Lambda.Powertools.Common.Tests/Core/PowertoolsEnvironmentTest.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
4+
using Moq;
35
using Xunit;
46

57
namespace AWS.Lambda.Powertools.Common.Tests;
@@ -42,10 +44,9 @@ public void Set_Multiple_Execution_Environment()
4244

4345
// Act
4446
systemWrapper.SetExecutionEnvironment(this);
45-
systemWrapper.SetExecutionEnvironment(this);
4647

4748
// Assert
48-
Assert.Equal($"{Constants.FeatureContextIdentifier}/Fake/1.0.0 {Constants.FeatureContextIdentifier}/Fake/1.0.0", systemWrapper.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
49+
Assert.Equal($"{Constants.FeatureContextIdentifier}/Fake/1.0.0", systemWrapper.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
4950
}
5051

5152
[Fact]
@@ -63,6 +64,20 @@ public void Set_Execution_Real_Environment()
6364

6465
[Fact]
6566
public void Set_Execution_Real_Environment_Multiple()
67+
{
68+
// Arrange
69+
var systemWrapper = new SystemWrapper(new PowertoolsEnvironment());
70+
71+
// Act
72+
systemWrapper.SetExecutionEnvironment(this);
73+
systemWrapper.SetExecutionEnvironment(systemWrapper);
74+
75+
// Assert
76+
Assert.Equal($"{Constants.FeatureContextIdentifier}/Tests/1.0.0 {Constants.FeatureContextIdentifier}/Common/0.0.1", systemWrapper.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
77+
}
78+
79+
[Fact]
80+
public void Set_Execution_Real_Environment_Multiple_Avoid_Duplicate()
6681
{
6782
// Arrange
6883
var systemWrapper = new SystemWrapper(new PowertoolsEnvironment());
@@ -72,7 +87,7 @@ public void Set_Execution_Real_Environment_Multiple()
7287
systemWrapper.SetExecutionEnvironment(this);
7388

7489
// Assert
75-
Assert.Equal($"{Constants.FeatureContextIdentifier}/Tests/1.0.0 {Constants.FeatureContextIdentifier}/Tests/1.0.0", systemWrapper.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
90+
Assert.Equal($"{Constants.FeatureContextIdentifier}/Tests/1.0.0", systemWrapper.GetEnvironmentVariable("AWS_EXECUTION_ENV"));
7691
}
7792

7893
public void Dispose()

0 commit comments

Comments
 (0)