Skip to content

Commit be81563

Browse files
boumenotgrvillic
andauthored
Support case-insensitive on non-Windows OSs. (#75)
* Support case-insensitive on non-Windows OSs. The environment variable EnableGoCliScan existence gates usage of the Go CLI tools for determing what modules are in-use. The current check does a get of the environment variable, and if it exists behavior is enabled. On Windows this is case-insensitive, but on Linux (or MacOS) this is case-sensitive so the user must exactly use the casing of 'EnableGoCliScan'. Our CI system automatically capitalizes all environment variables when they are defined, so EnableGoCliScan becomes ENABLEGOCLISCAN. I am not aware of a way to control this behavior, so there is no way to enable Go CLI tooling. My fix is to treat all environment variable exitence checks as case-insensitive. * New components can be detected with Env Variable change Co-authored-by: Greg Villicana <[email protected]>
1 parent edb9df2 commit be81563

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/Microsoft.ComponentDetection.Common/EnvironmentVariableService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Composition;
34
using Microsoft.ComponentDetection.Contracts;
45

@@ -9,8 +10,12 @@ public class EnvironmentVariableService : IEnvironmentVariableService
910
{
1011
public bool DoesEnvironmentVariableExist(string name)
1112
{
12-
var enabledVar = Environment.GetEnvironmentVariable(name);
13-
return !string.IsNullOrEmpty(enabledVar);
13+
// Environment variables are case-insensitive on Windows, and case-sensitive on
14+
// Linux and MacOS.
15+
// https://docs.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable
16+
return Environment.GetEnvironmentVariables().Keys
17+
.OfType<string>()
18+
.FirstOrDefault(x => string.Compare(x, name, true) == 0) != null;
1419
}
1520
}
1621
}

src/Microsoft.ComponentDetection.Detectors/go/GoComponentDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GoComponentDetector : FileComponentDetector
3333

3434
public override IEnumerable<ComponentType> SupportedComponentTypes { get; } = new[] { ComponentType.Go };
3535

36-
public override int Version => 2;
36+
public override int Version => 3;
3737

3838
private HashSet<string> projectRoots = new HashSet<string>();
3939

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.ComponentDetection.Common;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
using System;
5+
6+
namespace Microsoft.ComponentDetection.Common.Tests
7+
{
8+
[TestClass]
9+
public class EnvironmentVariableServiceTests
10+
{
11+
public const string MyEnvVar = nameof(MyEnvVar);
12+
13+
[TestInitialize]
14+
public void TestInitialize()
15+
{
16+
Environment.SetEnvironmentVariable(EnvironmentVariableServiceTests.MyEnvVar, "true");
17+
}
18+
19+
[TestCleanup]
20+
public void TestCleanup()
21+
{
22+
Environment.SetEnvironmentVariable(EnvironmentVariableServiceTests.MyEnvVar, null);
23+
}
24+
25+
[TestMethod]
26+
public void EnvironmentVariableService_ChecksAreCaseInsensitive()
27+
{
28+
var testSubject = new EnvironmentVariableService();
29+
30+
Assert.IsFalse(testSubject.DoesEnvironmentVariableExist("THIS_ENVIRONMENT_VARIABLE_DOES_NOT_EXIST"));
31+
32+
Assert.IsTrue(testSubject.DoesEnvironmentVariableExist(MyEnvVar));
33+
Assert.IsTrue(testSubject.DoesEnvironmentVariableExist(MyEnvVar.ToLower()));
34+
Assert.IsTrue(testSubject.DoesEnvironmentVariableExist(MyEnvVar.ToUpper()));
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)