-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement task environment APIs #12651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
521b9c3
Implement task environment APIs
AR-May 38b6b37
Update src/Framework/PathHelpers/AbsolutePath.cs
AR-May dec8a43
Have environment variables case sensitivity defined by the platform
AR-May cb3796a
Fix mac os tests failures
AR-May 3ee6525
Merge branch 'main' into implement-mt-apis
AR-May dbe28b6
Merge branch 'main' into implement-mt-apis
AR-May 3a53037
Address some PR comments.
AR-May 6e4af2d
Address PR comments - 2
AR-May e0bd256
Try to fix mac os tests.
AR-May 5a84f3b
Add more tests for absolute paths.
AR-May 5b28d23
Remove unnecessary test
AR-May 1cfba8c
fix test reseting
JanProvaznik 740d75e
fix macos symlinks
JanProvaznik 42a28ab
Address PR comments - 3
AR-May 1baaa4c
AbsolutePath naming and docs
JanProvaznik 8e97c0a
minor doc changes
JanProvaznik abb7f4f
Consolidate environment variable utilities
JanProvaznik abba41e
Add cached FrozenDictionary-based GetEnvironmentVariables for Framework
JanProvaznik 1a83cd4
add IEquatable, clarify docs of AbsolutePath
JanProvaznik dc771d0
move drivers to build from framework
JanProvaznik 4c89a50
undo environmentutilities refactors after moving drivers to build
JanProvaznik ccc8144
fun with case sensitivity of file systems
JanProvaznik 9c0a270
docs
JanProvaznik 8c162d6
env vars are case sensitive on unix
JanProvaznik 59ba721
remove unnecessary assemblyinfo
JanProvaznik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using Microsoft.Build.Framework; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Build.UnitTests | ||
| { | ||
| public class AbsolutePath_Tests | ||
| { | ||
| [Fact] | ||
| public void AbsolutePath_FromAbsolutePath_ShouldPreservePath() | ||
| { | ||
| string absolutePathString = Path.GetTempPath(); | ||
| var absolutePath = new AbsolutePath(absolutePathString); | ||
|
|
||
| absolutePath.Path.ShouldBe(absolutePathString); | ||
| Path.IsPathRooted(absolutePath.Path).ShouldBeTrue(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("subfolder")] | ||
| [InlineData("deep/nested/path")] | ||
| [InlineData(".")] | ||
| [InlineData("")] | ||
| [InlineData("..")] | ||
| public void AbsolutePath_FromRelativePath_ShouldResolveAgainstBase(string relativePath) | ||
| { | ||
| string baseDirectory = Path.Combine(Path.GetTempPath(), "testfolder"); | ||
| var basePath = new AbsolutePath(baseDirectory); | ||
| var absolutePath = new AbsolutePath(relativePath, basePath); | ||
|
|
||
| Path.IsPathRooted(absolutePath.Path).ShouldBeTrue(); | ||
|
|
||
| string expectedPath = Path.Combine(baseDirectory, relativePath); | ||
| absolutePath.Path.ShouldBe(expectedPath); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AbsolutePath_Equality_ShouldWorkCorrectly() | ||
| { | ||
| string testPath = Path.GetTempPath(); | ||
| var path1 = new AbsolutePath(testPath); | ||
| var path2 = new AbsolutePath(testPath); | ||
| var differentPath = new AbsolutePath(Path.Combine(testPath, "different")); | ||
|
|
||
| path1.ShouldBe(path2); | ||
| (path1 == path2).ShouldBeTrue(); | ||
| path1.ShouldNotBe(differentPath); | ||
| (path1 == differentPath).ShouldBeFalse(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("not/rooted/path", false, true)] | ||
| [InlineData("not/rooted/path", true, false)] | ||
| public void AbsolutePath_RootedValidation_ShouldBehaveProperly(string path, bool ignoreRootedCheck, bool shouldThrow) | ||
| { | ||
| if (shouldThrow) | ||
| { | ||
| Should.Throw<System.ArgumentException>(() => new AbsolutePath(path, ignoreRootedCheck: ignoreRootedCheck)); | ||
| } | ||
| else | ||
| { | ||
| var absolutePath = new AbsolutePath(path, ignoreRootedCheck: ignoreRootedCheck); | ||
| absolutePath.Path.ShouldBe(path); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Build.Framework; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Build.UnitTests | ||
| { | ||
| public class TaskEnvironmentTests | ||
| { | ||
| private const string StubEnvironmentName = "Stub"; | ||
| private const string MultithreadedEnvironmentName = "Multithreaded"; | ||
|
|
||
| public static TheoryData<string> EnvironmentTypes => | ||
| new TheoryData<string> | ||
| { | ||
| StubEnvironmentName, | ||
| MultithreadedEnvironmentName | ||
| }; | ||
|
|
||
| private static TaskEnvironment CreateTaskEnvironment(string environmentType) | ||
| { | ||
| return environmentType switch | ||
| { | ||
| StubEnvironmentName => new TaskEnvironment(StubTaskEnvironmentDriver.Instance), | ||
| MultithreadedEnvironmentName => new TaskEnvironment(new MultithreadedTaskEnvironmentDriver( | ||
| Path.GetTempPath(), | ||
| new Dictionary<string, string>(Environment.GetEnvironmentVariables().Cast<System.Collections.DictionaryEntry>() | ||
| .Where(e => e.Key is not null && e.Value is not null) | ||
| .ToDictionary(e => e.Key!.ToString()!, e => e.Value!.ToString()!)))), | ||
| _ => throw new ArgumentException($"Unknown environment type: {environmentType}") | ||
| }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_SetAndGetEnvironmentVariable_ShouldWork(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string testVarName = $"MSBUILD_TEST_VAR_{environmentType}_{Guid.NewGuid():N}"; | ||
| string testVarValue = $"test_value_{environmentType}"; | ||
|
|
||
| try | ||
| { | ||
| taskEnvironment.SetEnvironmentVariable(testVarName, testVarValue); | ||
| var retrievedValue = taskEnvironment.GetEnvironmentVariable(testVarName); | ||
|
|
||
| retrievedValue.ShouldBe(testVarValue); | ||
|
|
||
| var allVariables = taskEnvironment.GetEnvironmentVariables(); | ||
| allVariables.TryGetValue(testVarName, out string? actualValue).ShouldBeTrue(); | ||
| actualValue.ShouldBe(testVarValue); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(testVarName, null); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_SetEnvironmentVariableToNull_ShouldRemoveVariable(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string testVarName = $"MSBUILD_REMOVE_TEST_{environmentType}_{Guid.NewGuid():N}"; | ||
| string testVarValue = "value_to_remove"; | ||
|
|
||
| try | ||
| { | ||
| taskEnvironment.SetEnvironmentVariable(testVarName, testVarValue); | ||
| taskEnvironment.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); | ||
|
|
||
| taskEnvironment.SetEnvironmentVariable(testVarName, null); | ||
| taskEnvironment.GetEnvironmentVariable(testVarName).ShouldBeNull(); | ||
|
|
||
| var allVariables = taskEnvironment.GetEnvironmentVariables(); | ||
| allVariables.TryGetValue(testVarName, out string? actualValue).ShouldBeFalse(); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(testVarName, null); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_SetEnvironment_ShouldReplaceAllVariables(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string prefix = $"MSBUILD_SET_ENV_TEST_{environmentType}_{Guid.NewGuid():N}"; | ||
| string var1Name = $"{prefix}_VAR1"; | ||
| string var2Name = $"{prefix}_VAR2"; | ||
| string var3Name = $"{prefix}_VAR3"; | ||
|
|
||
| try | ||
| { | ||
| taskEnvironment.SetEnvironmentVariable(var1Name, "initial_value1"); | ||
| taskEnvironment.SetEnvironmentVariable(var2Name, "initial_value2"); | ||
|
|
||
| var newEnvironment = new Dictionary<string, string> | ||
| { | ||
| [var2Name] = "updated_value2", // Update existing | ||
| [var3Name] = "new_value3" // Add new | ||
| // var1Name is intentionally omitted to test removal | ||
| }; | ||
|
|
||
| taskEnvironment.SetEnvironment(newEnvironment); | ||
|
|
||
| taskEnvironment.GetEnvironmentVariable(var1Name).ShouldBeNull(); // Should be removed | ||
| taskEnvironment.GetEnvironmentVariable(var2Name).ShouldBe("updated_value2"); // Should be updated | ||
| taskEnvironment.GetEnvironmentVariable(var3Name).ShouldBe("new_value3"); // Should be added | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(var1Name, null); | ||
| Environment.SetEnvironmentVariable(var2Name, null); | ||
| Environment.SetEnvironmentVariable(var3Name, null); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_SetAndGetProjectDirectory_ShouldWork(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string originalDirectory = Directory.GetCurrentDirectory(); | ||
| string testDirectory = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar); | ||
| string alternateDirectory = Path.GetDirectoryName(testDirectory)!; | ||
|
|
||
| try | ||
| { | ||
| // Set project directory | ||
| taskEnvironment.ProjectDirectory = new AbsolutePath(testDirectory, ignoreRootedCheck: true); | ||
| var retrievedDirectory = taskEnvironment.ProjectDirectory; | ||
|
|
||
| retrievedDirectory.Path.ShouldBe(testDirectory); | ||
|
|
||
| // Change to alternate directory | ||
| taskEnvironment.ProjectDirectory = new AbsolutePath(alternateDirectory, ignoreRootedCheck: true); | ||
| var newRetrievedDirectory = taskEnvironment.ProjectDirectory; | ||
|
|
||
| newRetrievedDirectory.Path.ShouldBe(alternateDirectory); | ||
|
|
||
| // Verify behavior differs based on environment type | ||
| if (environmentType == StubEnvironmentName) | ||
| { | ||
| // Stub should change system current directory | ||
| Directory.GetCurrentDirectory().ShouldBe(alternateDirectory); | ||
| } | ||
| else | ||
| { | ||
| // Multithreaded should not change system current directory | ||
| Directory.GetCurrentDirectory().ShouldBe(originalDirectory); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| Directory.SetCurrentDirectory(originalDirectory); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_GetAbsolutePath_ShouldResolveCorrectly(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string baseDirectory = Path.GetTempPath(); | ||
| string relativePath = Path.Combine("subdir", "file.txt"); | ||
| string originalDirectory = Directory.GetCurrentDirectory(); | ||
|
|
||
| try | ||
| { | ||
| taskEnvironment.ProjectDirectory = new AbsolutePath(baseDirectory, ignoreRootedCheck: true); | ||
|
|
||
| var absolutePath = taskEnvironment.GetAbsolutePath(relativePath); | ||
|
|
||
| Path.IsPathRooted(absolutePath.Path).ShouldBeTrue(); | ||
| string expectedPath = Path.Combine(baseDirectory, relativePath); | ||
| absolutePath.Path.ShouldBe(expectedPath); | ||
| } | ||
| finally | ||
| { | ||
| Directory.SetCurrentDirectory(originalDirectory); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_GetAbsolutePath_WithAlreadyAbsolutePath_ShouldReturnUnchanged(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string absoluteInputPath = Path.Combine(Path.GetTempPath(), "already", "absolute", "path.txt"); | ||
|
|
||
| var resultPath = taskEnvironment.GetAbsolutePath(absoluteInputPath); | ||
|
|
||
| resultPath.Path.ShouldBe(absoluteInputPath); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(EnvironmentTypes))] | ||
| public void TaskEnvironment_GetProcessStartInfo_ShouldConfigureCorrectly(string environmentType) | ||
| { | ||
| var taskEnvironment = CreateTaskEnvironment(environmentType); | ||
| string testDirectory = Path.GetTempPath(); | ||
| string testVarName = $"MSBUILD_PROCESS_TEST_{environmentType}_{Guid.NewGuid():N}"; | ||
| string testVarValue = "process_test_value"; | ||
|
|
||
| try | ||
| { | ||
| taskEnvironment.ProjectDirectory = new AbsolutePath(testDirectory, ignoreRootedCheck: true); | ||
| taskEnvironment.SetEnvironmentVariable(testVarName, testVarValue); | ||
|
|
||
| var processStartInfo = taskEnvironment.GetProcessStartInfo(); | ||
|
|
||
| processStartInfo.ShouldNotBeNull(); | ||
|
|
||
| if (environmentType == StubEnvironmentName) | ||
| { | ||
| // Stub should reflect system environment, but working directory should be empty | ||
| processStartInfo.WorkingDirectory.ShouldBe(string.Empty); | ||
| } | ||
| else | ||
| { | ||
| // Multithreaded should reflect isolated environment | ||
| processStartInfo.WorkingDirectory.ShouldBe(testDirectory); | ||
| } | ||
|
|
||
| processStartInfo.Environment.TryGetValue(testVarName, out string? actualValue).ShouldBeTrue(); | ||
| actualValue.ShouldBe(testVarValue); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(testVarName, null); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TaskEnvironment_StubEnvironment_ShouldAffectSystemEnvironment() | ||
| { | ||
| string testVarName = $"MSBUILD_STUB_ISOLATION_TEST_{Guid.NewGuid():N}"; | ||
| string testVarValue = "stub_test_value"; | ||
|
|
||
| var stubEnvironment = new TaskEnvironment(StubTaskEnvironmentDriver.Instance); | ||
|
|
||
| try | ||
| { | ||
| // Set variable in stub environment | ||
| stubEnvironment.SetEnvironmentVariable(testVarName, testVarValue); | ||
|
|
||
| // Stub should affect system environment | ||
| Environment.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); | ||
| stubEnvironment.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); | ||
|
|
||
| // Remove from stub environment | ||
| stubEnvironment.SetEnvironmentVariable(testVarName, null); | ||
|
|
||
| // System environment should also be affected | ||
| Environment.GetEnvironmentVariable(testVarName).ShouldBeNull(); | ||
| stubEnvironment.GetEnvironmentVariable(testVarName).ShouldBeNull(); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(testVarName, null); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem() | ||
| { | ||
| string testVarName = $"MSBUILD_MULTITHREADED_ISOLATION_TEST_{Guid.NewGuid():N}"; | ||
| string testVarValue = "multithreaded_test_value"; | ||
|
|
||
| var multithreadedEnvironment = new TaskEnvironment(new MultithreadedTaskEnvironmentDriver( | ||
| Path.GetTempPath(), | ||
| new Dictionary<string, string>())); | ||
|
|
||
| try | ||
| { | ||
| // Verify system doesn't have the test variable initially | ||
| Environment.GetEnvironmentVariable(testVarName).ShouldBeNull(); | ||
|
|
||
| // Set variable in multithreaded environment | ||
| multithreadedEnvironment.SetEnvironmentVariable(testVarName, testVarValue); | ||
|
|
||
| // Multithreaded should have the value but system should not | ||
| multithreadedEnvironment.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); | ||
| Environment.GetEnvironmentVariable(testVarName).ShouldBeNull(); | ||
| } | ||
| finally | ||
| { | ||
| Environment.SetEnvironmentVariable(testVarName, null); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.