-
Notifications
You must be signed in to change notification settings - Fork 140
Migrate Tasks.Git.GetUntrackedFiles to multithreaded task model #1735
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
jankratochvilcz
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
jankratochvilcz:mt/get-untracked-files
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.
+169
−1
Draft
Changes from all commits
Commits
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
149 changes: 149 additions & 0 deletions
149
src/Microsoft.Build.Tasks.Git.UnitTests/GetUntrackedFilesTests.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,149 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the License.txt file in the project root for more information. | ||
|
|
||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Build.Framework; | ||
| using TestUtilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Build.Tasks.Git.UnitTests | ||
| { | ||
| public class GetUntrackedFilesTests | ||
| { | ||
| /// <summary> | ||
| /// Verifies historical behavior is preserved: a fully-qualified <see cref="GetUntrackedFiles.ProjectDirectory"/> | ||
| /// is used as the base for resolving the relative file item specs, and only the file ignored by the | ||
| /// repository is reported as untracked. The original (relative) ItemSpec is preserved on the output item. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AbsoluteProjectDirectory_ReportsIgnoredFileAsUntracked() | ||
| { | ||
| using var temp = new TempRoot(); | ||
| var repoDir = CreateRepository(temp); | ||
|
|
||
| var engine = new MockEngine(); | ||
| var task = new GetUntrackedFiles | ||
| { | ||
| BuildEngine = engine, | ||
| ConfigurationScope = "local", | ||
| ProjectDirectory = repoDir.Path, | ||
| Files = new ITaskItem[] | ||
| { | ||
| new MockItem("ignored_file.cs"), | ||
| new MockItem("included_file.cs"), | ||
| }, | ||
| }; | ||
|
|
||
| Assert.True(task.Execute()); | ||
| Assert.DoesNotContain("ERROR", engine.Log); | ||
| Assert.NotNull(task.UntrackedFiles); | ||
| AssertEx.Equal( | ||
| new[] { MockItem.AdjustSeparators("ignored_file.cs") }, | ||
| task.UntrackedFiles.Select(item => item.ItemSpec)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that a relative <see cref="GetUntrackedFiles.ProjectDirectory"/> (with no | ||
| /// <see cref="GetUntrackedFiles.RepositoryId"/>) is rejected end-to-end rather than resolved against the | ||
| /// process current working directory. The CWD is pointed at a real git repository; a CWD-dependent | ||
| /// (pre-migration) implementation would resolve <c>"."</c> against it, locate the repository, populate the | ||
| /// output, and log no warning. The migrated code rejects the relative path and reports the "missing | ||
| /// repository" warning instead. | ||
| /// | ||
| /// Note: on this path (RepositoryId not set) either the base <see cref="RepositoryTask"/> guard or the | ||
| /// <see cref="GetUntrackedFiles"/> guard suffices, so this test does not isolate the base guard on its own | ||
| /// (the sibling LocateRepository PR covers the base guard in isolation, and | ||
| /// <see cref="RelativeProjectDirectory_OnCachedRepository_IsRejected"/> covers the task guard on the | ||
| /// cached path). It documents the observable end-to-end behavior. | ||
| /// </summary> | ||
| [Fact] | ||
| public void RelativeProjectDirectory_IsRejected_IndependentOfProcessCwd() | ||
| { | ||
| using var temp = new TempRoot(); | ||
| var repoDir = CreateRepository(temp); | ||
|
|
||
| var originalCurrentDirectory = Directory.GetCurrentDirectory(); | ||
| try | ||
| { | ||
| Directory.SetCurrentDirectory(repoDir.Path); | ||
|
|
||
| var engine = new MockEngine(); | ||
| var task = new GetUntrackedFiles | ||
| { | ||
| BuildEngine = engine, | ||
| ConfigurationScope = "local", | ||
| ProjectDirectory = ".", | ||
| Files = new ITaskItem[] { new MockItem("ignored_file.cs") }, | ||
| }; | ||
|
|
||
| Assert.True(task.Execute()); | ||
| Assert.DoesNotContain("ERROR", engine.Log); | ||
| Assert.Contains("WARNING", engine.Log); | ||
| Assert.Null(task.UntrackedFiles); | ||
| } | ||
| finally | ||
| { | ||
| Directory.SetCurrentDirectory(originalCurrentDirectory); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies the belt-and-suspenders guard in <see cref="GetUntrackedFiles"/>: when the repository is taken | ||
| /// from the cache (RepositoryId provided), the base <see cref="RepositoryTask"/> does not validate the | ||
| /// initial path, so the task itself must reject a relative <see cref="GetUntrackedFiles.ProjectDirectory"/> | ||
| /// before using it to resolve the file item specs. A LocateRepository run primes the shared cache, then a | ||
| /// GetUntrackedFiles run reuses it with a relative project directory. Because ProjectDirectory is expected | ||
| /// to be absolute on this path, a non-absolute value is reported as an error (Execute returns false). | ||
| /// </summary> | ||
| [Fact] | ||
| public void RelativeProjectDirectory_OnCachedRepository_IsRejected() | ||
| { | ||
| using var temp = new TempRoot(); | ||
| var repoDir = CreateRepository(temp); | ||
|
|
||
| var engine = new MockEngine(); | ||
|
|
||
| // Prime the repository cache and obtain the RepositoryId used as the cache key. | ||
| var locate = new LocateRepository | ||
| { | ||
| BuildEngine = engine, | ||
| ConfigurationScope = "local", | ||
| NoWarnOnMissingInfo = true, | ||
| Path = repoDir.Path, | ||
| }; | ||
| Assert.True(locate.Execute()); | ||
| Assert.NotNull(locate.RepositoryId); | ||
|
|
||
| var task = new GetUntrackedFiles | ||
| { | ||
| BuildEngine = engine, | ||
| ConfigurationScope = "local", | ||
| RepositoryId = locate.RepositoryId, | ||
| ProjectDirectory = ".", | ||
| Files = new ITaskItem[] { new MockItem("ignored_file.cs") }, | ||
| }; | ||
|
|
||
| Assert.False(task.Execute()); | ||
| Assert.Contains("ERROR", engine.Log); | ||
| Assert.Null(task.UntrackedFiles); | ||
| } | ||
|
|
||
| private static TempDirectory CreateRepository(TempRoot temp) | ||
| { | ||
| // A real on-disk git repository that ignores 'ignored_file.cs' but not 'included_file.cs'. | ||
| var repoDir = temp.CreateDirectory(); | ||
| var gitDir = repoDir.CreateDirectory(".git"); | ||
| gitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/master"); | ||
| gitDir.CreateFile("config").WriteAllText(""); | ||
| gitDir.CreateDirectory("objects"); | ||
| gitDir.CreateDirectory("refs").CreateDirectory("heads").CreateFile("master").WriteAllText("0000000000000000000000000000000000000000"); | ||
|
|
||
| repoDir.CreateFile(".gitignore").WriteAllText("ignored_file.cs\n"); | ||
| repoDir.CreateFile("ignored_file.cs").WriteAllText(""); | ||
| repoDir.CreateFile("included_file.cs").WriteAllText(""); | ||
| return repoDir; | ||
| } | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to validate setting CWD.