Skip to content
Merged
Show file tree
Hide file tree
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 Oct 15, 2025
38b6b37
Update src/Framework/PathHelpers/AbsolutePath.cs
AR-May Oct 15, 2025
dec8a43
Have environment variables case sensitivity defined by the platform
AR-May Oct 16, 2025
cb3796a
Fix mac os tests failures
AR-May Oct 16, 2025
3ee6525
Merge branch 'main' into implement-mt-apis
AR-May Oct 16, 2025
dbe28b6
Merge branch 'main' into implement-mt-apis
AR-May Nov 21, 2025
3a53037
Address some PR comments.
AR-May Nov 21, 2025
6e4af2d
Address PR comments - 2
AR-May Nov 24, 2025
e0bd256
Try to fix mac os tests.
AR-May Nov 24, 2025
5a84f3b
Add more tests for absolute paths.
AR-May Nov 24, 2025
5b28d23
Remove unnecessary test
AR-May Nov 24, 2025
1cfba8c
fix test reseting
JanProvaznik Nov 24, 2025
740d75e
fix macos symlinks
JanProvaznik Nov 25, 2025
42a28ab
Address PR comments - 3
AR-May Nov 25, 2025
1baaa4c
AbsolutePath naming and docs
JanProvaznik Nov 26, 2025
8e97c0a
minor doc changes
JanProvaznik Nov 26, 2025
abb7f4f
Consolidate environment variable utilities
JanProvaznik Nov 26, 2025
abba41e
Add cached FrozenDictionary-based GetEnvironmentVariables for Framework
JanProvaznik Nov 26, 2025
1a83cd4
add IEquatable, clarify docs of AbsolutePath
JanProvaznik Nov 26, 2025
dc771d0
move drivers to build from framework
JanProvaznik Nov 26, 2025
4c89a50
undo environmentutilities refactors after moving drivers to build
JanProvaznik Nov 26, 2025
ccc8144
fun with case sensitivity of file systems
JanProvaznik Nov 26, 2025
9c0a270
docs
JanProvaznik Nov 26, 2025
8c162d6
env vars are case sensitive on unix
JanProvaznik Nov 27, 2025
59ba721
remove unnecessary assemblyinfo
JanProvaznik Nov 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Framework.UnitTests/AbsolutePath_Tests.cs
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);
}
}
}
}
300 changes: 300 additions & 0 deletions src/Framework.UnitTests/TaskEnvironment_Tests.cs
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
Comment thread
AR-May marked this conversation as resolved.
Outdated
{
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);
}
}
}
}
Loading
Loading