-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add to the
retrieve-codeowners
tool support for returning owners fo…
…r all paths matching given glob path. (#5134) This PR implements for the `retrieve-codeowners` tool the ability to return not only owners for a single path, but a list of all owners for all paths resolved when matching a glob path given as input. As such, this PR contributes to: - #5135 This work is necessary to be able to compare and diff the owners of all files in repository before and after the regex matcher is turned on, per this comment: - #5088 (review) In other words, this PR contributes to unblocking to the following PR: - #5088 And as such, also contributes to: - #2770 This PR will require some upstream changes, which are captured by: - #5103 ### Additional changes - Removed logger from `CodeOwnersParser`; replaced with `Console.Out` and `Console.Error`. This addresses the following comment: - #5063 (comment) - Prevented the new regex matcher from matching paths that have `*` in them - such paths are malformed anyway (at least on Windows). - A lot of assorted changes to surrounding production & test code - please see the file diff for details. ### Implementation notes This PR leverages [`Microsoft.Extensions.FileSystemGlobbing`](https://www.nuget.org/packages/Microsoft.Extensions.FileSystemGlobbing). Doc: https://learn.microsoft.com/en-us/dotnet/core/extensions/file-globbing
- Loading branch information
Konrad Jamrozik
authored
Jan 14, 2023
1 parent
b4ae814
commit 4cf0beb
Showing
22 changed files
with
490 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
150 changes: 150 additions & 0 deletions
150
tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/ProgramGlobPathTests.cs
This file contains 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,150 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using Azure.Sdk.Tools.CodeOwnersParser; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Sdk.Tools.RetrieveCodeOwners.Tests; | ||
|
||
/// <summary> | ||
/// Test class for Azure.Sdk.Tools.RetrieveCodeOwners.Program.Main(), | ||
/// for scenario in which targetPath is a glob path, i.e. | ||
/// targetPath.IsGlobPath() returns true. | ||
/// </summary> | ||
[TestFixture] | ||
public class ProgramGlobPathTests | ||
{ | ||
/// <summary> | ||
/// Given: | ||
/// <br/><br/> | ||
/// codeownersFilePathOrUrl contents of: | ||
/// <code> | ||
/// | ||
/// /** @2star | ||
/// /* @star | ||
/// /foo/**/a.txt @foo_2star_a | ||
/// /foo/*/a.txt @foo_star_a_1 @foo_star_a_2 | ||
/// | ||
/// </code> | ||
/// | ||
/// targetDir contents of: | ||
/// | ||
/// <code> | ||
/// | ||
/// /a.txt | ||
/// /b.txt | ||
/// /foo/a.txt | ||
/// /foo/b.txt | ||
/// /foo/bar/a.txt | ||
/// /foo/bar/b.txt | ||
/// | ||
/// </code> | ||
/// | ||
/// targetPath of: | ||
/// | ||
/// <code> | ||
/// | ||
/// /foo/** | ||
/// | ||
/// </code> | ||
/// | ||
/// excludeNonUserAliases set to false and useRegexMatcher set to true. | ||
/// <br/><br/> | ||
/// When: | ||
/// The retrieve-codeowners tool is executed on these inputs. | ||
/// <br/><br/> | ||
/// Then: | ||
/// The tool should return on STDOUT owners matched in following way: | ||
/// | ||
/// <code> | ||
/// /a.txt @star | ||
/// /b.txt @star | ||
/// /foo/a.txt @foo_2star_a | ||
/// /foo/b.txt @2star | ||
/// /foo/bar/a.txt @foo_star_a_1 @foo_star_a_2 | ||
/// /foo/bar/b.txt @2star | ||
/// </code> | ||
/// </summary> | ||
[Test] | ||
public void OutputsCodeownersForGlobPath() | ||
{ | ||
const string targetDir = "./TestData/InputDir"; | ||
const string targetPath = "/**"; | ||
const string codeownersFilePathOrUrl = "./TestData/glob_path_CODEOWNERS"; | ||
const bool excludeNonUserAliases = false; | ||
const bool useRegexMatcher = true; | ||
|
||
var expectedEntries = new Dictionary<string, CodeownersEntry> | ||
{ | ||
// @formatter:off | ||
["a.txt"] = new CodeownersEntry("/*", new List<string> { "star" }), | ||
["b.txt"] = new CodeownersEntry("/*", new List<string> { "star" }), | ||
["foo/a.txt"] = new CodeownersEntry("/foo/**/a.txt", new List<string> { "foo_2star_a" }), | ||
["foo/b.txt"] = new CodeownersEntry("/**", new List<string> { "2star" }), | ||
["foo/bar/a.txt"] = new CodeownersEntry("/foo/*/a.txt", new List<string> { "foo_star_a_1", "foo_star_a_2" }), | ||
["foo/bar/b.txt"] = new CodeownersEntry("/**", new List<string> { "2star" }), | ||
// @formatter:on | ||
}; | ||
|
||
string actualOutput, actualErr; | ||
int returnCode; | ||
using (var consoleOutput = new ConsoleOutput()) | ||
{ | ||
// Act | ||
returnCode = Program.Main( | ||
targetPath, | ||
codeownersFilePathOrUrl, | ||
excludeNonUserAliases, | ||
targetDir, | ||
useRegexMatcher); | ||
|
||
actualOutput = consoleOutput.GetStdout(); | ||
actualErr = consoleOutput.GetStderr(); | ||
} | ||
|
||
var actualEntries = TryDeserializeActualEntries(actualOutput, actualErr); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
AssertEntries(actualEntries, expectedEntries); | ||
Assert.That(returnCode, Is.EqualTo(0)); | ||
Assert.That(actualErr, Is.EqualTo(string.Empty)); | ||
}); | ||
} | ||
|
||
private static Dictionary<string, CodeownersEntry> TryDeserializeActualEntries( | ||
string actualOutput, | ||
string actualErr) | ||
{ | ||
Dictionary<string, CodeownersEntry> actualEntries; | ||
try | ||
{ | ||
actualEntries = | ||
JsonSerializer.Deserialize<Dictionary<string, CodeownersEntry>>(actualOutput)!; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
Console.WriteLine("actualOutput: " + actualOutput); | ||
Console.WriteLine("actualErr: " + actualErr); | ||
throw; | ||
} | ||
|
||
return actualEntries; | ||
} | ||
|
||
private static void AssertEntries( | ||
Dictionary<string, CodeownersEntry> actualEntries, | ||
Dictionary<string, CodeownersEntry> expectedEntries) | ||
{ | ||
foreach (KeyValuePair<string, CodeownersEntry> kvp in actualEntries) | ||
{ | ||
string path = kvp.Key; | ||
CodeownersEntry actualEntry = kvp.Value; | ||
Assert.That(expectedEntries[path], Is.EqualTo(actualEntry), $"path: {path}"); | ||
} | ||
|
||
Assert.That(actualEntries.Count, Is.EqualTo(expectedEntries.Count)); | ||
} | ||
} |
This file contains 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
Empty file.
12 changes: 12 additions & 0 deletions
12
tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/TestData/InputDir/b.txt
This file contains 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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.RetrieveCodeOwners.Tests.TestData | ||
{ | ||
class b | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/TestData/InputDir/foo/a.txt
This file contains 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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.RetrieveCodeOwners.Tests.TestData.foo | ||
{ | ||
class a | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/TestData/InputDir/foo/b.txt
This file contains 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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.RetrieveCodeOwners.Tests.TestData.foo | ||
{ | ||
class b | ||
{ | ||
} | ||
} |
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions
9
...code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/TestData/glob_path_CODEOWNERS
This file contains 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,9 @@ | ||
# This file is a test resoure for the test: | ||
# | ||
# Azure.Sdk.Tools.RetrieveCodeOwners.Tests.WildcardTests.TestWildcard | ||
# | ||
|
||
/** @2star | ||
/* @star | ||
/foo/**/a.txt @foo_2star_a | ||
/foo/*/a.txt @foo_star_a_1 @foo_star_a_2 |
File renamed without changes.
Oops, something went wrong.