-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Migrate WriteAppConfigWithSupportedRuntime to IMultiThreadableTask #53957
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
Draft
SimaTian
wants to merge
10
commits into
dotnet:main
Choose a base branch
from
SimaTian:migrate-write-app-config-supported-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e22870f
Migrate WriteAppConfigWithSupportedRuntime to IMultiThreadableTask
SimaTian 4922e1a
Address review: fix FullPath metadata and cross-platform test path
SimaTian 9fe6f27
Address WriteAppConfig task environment isolation
SimaTian 1319c51
Use xUnit cancellation token in WriteAppConfig test
SimaTian c7e4cc3
correct pattern for TaskEnvironment
JanProvaznik 3747d86
remove useless test
JanProvaznik 802c92a
simplify tests
JanProvaznik 47ea797
inheritdoc
JanProvaznik 81b01e1
Merge branch 'main' into migrate-write-app-config-supported-runtime
JanProvaznik b8794de
Replace SDK multi-threading shims with TaskEnvironment.Fallback
SimaTian 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 was deleted.
Oops, something went wrong.
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
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
157 changes: 157 additions & 0 deletions
157
...Microsoft.NET.Build.Tasks.Tests/GivenAWriteAppConfigWithSupportedRuntimeMultiThreading.cs
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,157 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
| using FluentAssertions; | ||
| using Microsoft.Build.Framework; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.NET.Build.Tasks.UnitTests | ||
| { | ||
| [CollectionDefinition("CwdSensitive", DisableParallelization = true)] | ||
| public sealed class CwdSensitiveCollection | ||
| { | ||
| } | ||
|
|
||
| [Collection("CwdSensitive")] | ||
| public class GivenAWriteAppConfigWithSupportedRuntimeMultiThreading : IDisposable | ||
| { | ||
| private readonly List<string> _tempDirs = new(); | ||
|
|
||
| [Fact] | ||
| public void DecoyCwdPathResolutionUsesTaskEnvironment() | ||
| { | ||
| string realWorkDir = CreateTempDirectory(); | ||
| string decoyWorkDir = CreateTempDirectory(); | ||
|
|
||
| string relativeAppConfigPath = "input.config"; | ||
| string appConfigPath = Path.Combine(realWorkDir, relativeAppConfigPath); | ||
| File.WriteAllText(appConfigPath, @"<?xml version=""1.0"" encoding=""utf-8""?> | ||
| <configuration> | ||
| </configuration>"); | ||
|
|
||
| string relativeOutputPath = Path.Combine("obj", "Debug", "output.config"); | ||
| string outputPath = Path.Combine(realWorkDir, relativeOutputPath); | ||
| Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); | ||
|
|
||
| var taskEnv = TaskEnvironmentHelper.CreateForTest(realWorkDir); | ||
| taskEnv.SetEnvironmentVariable("CWD_DECOY_TEST", decoyWorkDir); | ||
|
|
||
| var engine = new MockBuildEngine(); | ||
| var task = new WriteAppConfigWithSupportedRuntime | ||
| { | ||
| BuildEngine = engine, | ||
| TaskEnvironment = taskEnv, | ||
| AppConfigFile = new MockTaskItem(relativeAppConfigPath, new Dictionary<string, string>()), | ||
| OutputAppConfigFile = new MockTaskItem(relativeOutputPath, new Dictionary<string, string>()), | ||
| TargetFrameworkIdentifier = ".NETFramework", | ||
| TargetFrameworkVersion = "v4.7.2" | ||
| }; | ||
|
|
||
| string originalCwd = Directory.GetCurrentDirectory(); | ||
| try | ||
| { | ||
| Directory.SetCurrentDirectory(decoyWorkDir); | ||
| task.Execute().Should().BeTrue("task should succeed even with decoy CWD"); | ||
| File.Exists(outputPath).Should().BeTrue("output should be written to TaskEnvironment-resolved path"); | ||
| File.Exists(Path.Combine(decoyWorkDir, relativeOutputPath)).Should().BeFalse("output should not be written to process CWD"); | ||
| } | ||
| finally | ||
| { | ||
| Directory.SetCurrentDirectory(originalCwd); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ConcurrentExecutionWithDifferentFrameworkVersions() | ||
| { | ||
| const int concurrency = 8; | ||
| const string relativeAppConfigPath = "app.config"; | ||
| string relativeOutputPath = Path.Combine("obj", "Debug", "output.config"); | ||
| var tasks = new WriteAppConfigWithSupportedRuntime[concurrency]; | ||
| var executeTasks = new Task<bool>[concurrency]; | ||
| using var readyGate = new CountdownEvent(concurrency); | ||
| using var startGate = new ManualResetEventSlim(false); | ||
| var versions = Enumerable.Range(0, concurrency).Select(i => $"v4.{i}").ToArray(); | ||
|
|
||
| for (int i = 0; i < concurrency; i++) | ||
| { | ||
| string workDir = CreateTempDirectory(); | ||
| string appConfigPath = Path.Combine(workDir, relativeAppConfigPath); | ||
| File.WriteAllText(appConfigPath, $@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
| <configuration> | ||
| <appSettings> | ||
| <add key=""Project"" value=""{i}"" /> | ||
| </appSettings> | ||
| </configuration>"); | ||
|
|
||
| string outputPath = Path.Combine(workDir, relativeOutputPath); | ||
| Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); | ||
|
|
||
| tasks[i] = new WriteAppConfigWithSupportedRuntime | ||
| { | ||
| BuildEngine = new MockBuildEngine(), | ||
| TaskEnvironment = TaskEnvironmentHelper.CreateForTest(workDir), | ||
| AppConfigFile = new MockTaskItem(relativeAppConfigPath, new Dictionary<string, string>()), | ||
| OutputAppConfigFile = new MockTaskItem(relativeOutputPath, new Dictionary<string, string>()), | ||
| TargetFrameworkIdentifier = ".NETFramework", | ||
| TargetFrameworkVersion = versions[i] | ||
| }; | ||
| } | ||
|
|
||
| for (int i = 0; i < concurrency; i++) | ||
| { | ||
| var t = tasks[i]; | ||
| executeTasks[i] = Task.Run(() => | ||
| { | ||
| readyGate.Signal(); | ||
| startGate.Wait(); | ||
| return t.Execute(); | ||
| }); | ||
| } | ||
|
|
||
| bool allWorkersReady = readyGate.Wait(TimeSpan.FromSeconds(30), TestContext.Current.CancellationToken); | ||
| startGate.Set(); | ||
|
|
||
| await Task.WhenAll(executeTasks); | ||
| allWorkersReady.Should().BeTrue("all workers should be ready before the start gate opens"); | ||
|
|
||
| for (int i = 0; i < concurrency; i++) | ||
| { | ||
| (await executeTasks[i]).Should().BeTrue($"task {i} should succeed"); | ||
|
|
||
| string outputPath = tasks[i].OutputAppConfigFile.ItemSpec; | ||
| Path.IsPathRooted(outputPath).Should().BeFalse($"task {i} should keep the shared relative output ItemSpec"); | ||
| AbsolutePath resolvedPath = tasks[i].TaskEnvironment.GetAbsolutePath(outputPath); | ||
| File.Exists(resolvedPath.Value).Should().BeTrue($"output {i} should exist"); | ||
|
|
||
| string content = File.ReadAllText(resolvedPath.Value); | ||
| content.Should().Contain($@"value=""{i}""", $"output {i} should come from its own ProjectDirectory"); | ||
| var doc = XDocument.Parse(content); | ||
| var supportedRuntime = doc.Descendants("supportedRuntime").Single(); | ||
|
|
||
| string expectedSku = $".NETFramework,Version={versions[i]}"; | ||
| supportedRuntime.Attribute("sku").Value.Should().Be(expectedSku, | ||
| $"output {i} should have correct SKU for version {versions[i]}"); | ||
| } | ||
| } | ||
|
|
||
| private string CreateTempDirectory([CallerMemberName] string testName = null) | ||
| { | ||
| string tempDir = Path.Combine(Path.GetTempPath(), $"WriteAppConfigTest_{testName}_{Guid.NewGuid():N}"); | ||
| Directory.CreateDirectory(tempDir); | ||
| _tempDirs.Add(tempDir); | ||
| return tempDir; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| foreach (var dir in _tempDirs) | ||
| { | ||
| try { Directory.Delete(dir, recursive: true); } catch { } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.