From 3b1da002f0bacb0a9f668f511aca3e8923269484 Mon Sep 17 00:00:00 2001 From: Jan Krivanek Date: Thu, 15 Aug 2024 17:45:19 +0200 Subject: [PATCH] [Tests only] Refactor PropertiesUsageAnalyzerTest (#10511) * Refactor tests * Fix failing test --- src/BuildCheck.UnitTests/EndToEndTests.cs | 78 +++++++++---------- .../.editorconfigtest | 4 + .../{ImportedFile1 => ImportedFile1.props} | 0 .../{Project1 => Project1.csproj} | 2 +- .../{Project2 => Project2.csproj} | 4 +- .../PropsCheckTest.csproj | 26 +++++++ 6 files changed, 70 insertions(+), 44 deletions(-) rename src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/{ImportedFile1 => ImportedFile1.props} (100%) rename src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/{Project1 => Project1.csproj} (92%) rename src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/{Project2 => Project2.csproj} (91%) create mode 100644 src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/PropsCheckTest.csproj diff --git a/src/BuildCheck.UnitTests/EndToEndTests.cs b/src/BuildCheck.UnitTests/EndToEndTests.cs index 2ccc4a88c32..1237dcf131f 100644 --- a/src/BuildCheck.UnitTests/EndToEndTests.cs +++ b/src/BuildCheck.UnitTests/EndToEndTests.cs @@ -38,49 +38,29 @@ public EndToEndTests(ITestOutputHelper output) public void Dispose() => _env.Dispose(); - [Fact] - public void PropertiesUsageAnalyzerTest() + [Theory] + [InlineData(true)] + [InlineData(false)] + public void PropertiesUsageAnalyzerTest(bool buildInOutOfProcessNode) { - using TestEnvironment env = TestEnvironment.Create(); - string contents = """ - - - - - - - $(MyProp1) - - - - - - - - $(MyProp2);xxx - - - - - """; - TransientTestFolder logFolder = env.CreateFolder(createFolder: true); - TransientTestFile projectFile = env.CreateFile(logFolder, "myProj.proj", contents); - - string output = RunnerUtilities.ExecBootstrapedMSBuild($"{projectFile.Path} -check /v:detailed", out bool success); + PrepareSampleProjectsAndConfig( + buildInOutOfProcessNode, + out TransientTestFile projectFile, + "PropsCheckTest.csproj"); + + string output = RunnerUtilities.ExecBootstrapedMSBuild($"{projectFile.Path} -check", out bool success); _env.Output.WriteLine(output); _env.Output.WriteLine("========================="); success.ShouldBeTrue(output); - output.ShouldMatch(@"BC0201: .* Property: \[MyProp1\]"); - output.ShouldMatch(@"BC0202: .* Property: \[MyProp2\]"); - // since it's just suggestion, it doesn't have a colon ':' - output.ShouldMatch(@"BC0203 .* Property: \[MyProp3\]"); + output.ShouldMatch(@"BC0201: .* Property: \[MyProp11\]"); + output.ShouldMatch(@"BC0202: .* Property: \[MyPropT2\]"); + output.ShouldMatch(@"BC0203: .* Property: \[MyProp13\]"); // each finding should be found just once - but reported twice, due to summary Regex.Matches(output, "BC0201: .* Property").Count.ShouldBe(2); Regex.Matches(output, "BC0202: .* Property").Count.ShouldBe(2); - // since it's not an error - it's not in summary - Regex.Matches(output, "BC0203 .* Property").Count.ShouldBe(1); + Regex.Matches(output, "BC0203 .* Property").Count.ShouldBe(2); } [Theory] @@ -420,20 +400,23 @@ private void PopulateXmlAttribute(XmlDocument doc, XmlNode node, string attribut private void PrepareSampleProjectsAndConfig( bool buildInOutOfProcessNode, out TransientTestFile projectFile, - IEnumerable<(string RuleId, string Severity)>? ruleToSeverity, + string entryProjectAssetName, + IEnumerable? supplementalAssetNames = null, + IEnumerable<(string RuleId, string Severity)>? ruleToSeverity = null, IEnumerable<(string RuleId, (string ConfigKey, string Value) CustomConfig)>? ruleToCustomConfig = null) { string testAssetsFolderName = "SampleCheckIntegrationTest"; TransientTestFolder workFolder = _env.CreateFolder(createFolder: true); TransientTestFile testFile = _env.CreateFile(workFolder, "somefile"); - string contents = ReadAndAdjustProjectContent("Project1"); - string contents2 = ReadAndAdjustProjectContent("Project2"); - string contentsImported = ReadAndAdjustProjectContent("ImportedFile1"); + string contents = ReadAndAdjustProjectContent(entryProjectAssetName); + projectFile = _env.CreateFile(workFolder, entryProjectAssetName, contents); - projectFile = _env.CreateFile(workFolder, "FooBar.csproj", contents); - TransientTestFile projectFile2 = _env.CreateFile(workFolder, "FooBar-Copy.csproj", contents2); - TransientTestFile importedFile1 = _env.CreateFile(workFolder, "ImportedFile1.props", contentsImported); + foreach (string supplementalAssetName in supplementalAssetNames ?? Enumerable.Empty()) + { + string supplementalContent = ReadAndAdjustProjectContent(supplementalAssetName); + TransientTestFile supplementalFile = _env.CreateFile(workFolder, supplementalAssetName, supplementalContent); + } _env.CreateFile(workFolder, ".editorconfig", ReadEditorConfig(ruleToSeverity, ruleToCustomConfig, testAssetsFolderName)); @@ -454,6 +437,19 @@ string ReadAndAdjustProjectContent(string fileName) => .Replace("WorkFolderPath", workFolder.Path); } + private void PrepareSampleProjectsAndConfig( + bool buildInOutOfProcessNode, + out TransientTestFile projectFile, + IEnumerable<(string RuleId, string Severity)>? ruleToSeverity, + IEnumerable<(string RuleId, (string ConfigKey, string Value) CustomConfig)>? ruleToCustomConfig = null) + => PrepareSampleProjectsAndConfig( + buildInOutOfProcessNode, + out projectFile, + "Project1.csproj", + new[] { "Project2.csproj", "ImportedFile1.props" }, + ruleToSeverity, + ruleToCustomConfig); + private string ReadEditorConfig( IEnumerable<(string RuleId, string Severity)>? ruleToSeverity, IEnumerable<(string RuleId, (string ConfigKey, string Value) CustomConfig)>? ruleToCustomConfig, diff --git a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/.editorconfigtest b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/.editorconfigtest index 57d36981808..9b63ef6a700 100644 --- a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/.editorconfigtest +++ b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/.editorconfigtest @@ -11,3 +11,7 @@ build_check.BC0103.CustomConfig=dummy build_check.COND0543.Severity=Error build_check.COND0543.EvaluationCheckScope=CheckedProjectOnly build_check.COND0543.CustomSwitch=QWERTY + +build_check.BC0201.Severity=warning +build_check.BC0202.Severity=warning +build_check.BC0203.Severity=warning diff --git a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/ImportedFile1 b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/ImportedFile1.props similarity index 100% rename from src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/ImportedFile1 rename to src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/ImportedFile1.props diff --git a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1 b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1.csproj similarity index 92% rename from src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1 rename to src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1.csproj index 4412879248c..918173e191c 100644 --- a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1 +++ b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project1.csproj @@ -16,7 +16,7 @@ - + $(TEST) diff --git a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2 b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2.csproj similarity index 91% rename from src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2 rename to src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2.csproj index 17b8d3da249..b8355528363 100644 --- a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2 +++ b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/Project2.csproj @@ -11,7 +11,7 @@ - + @@ -19,4 +19,4 @@ - \ No newline at end of file + diff --git a/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/PropsCheckTest.csproj b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/PropsCheckTest.csproj new file mode 100644 index 00000000000..4e5bb2af0e3 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/SampleCheckIntegrationTest/PropsCheckTest.csproj @@ -0,0 +1,26 @@ + + + + + + + $(MyProp11) + + + + + + + + $(MyPropT2);xxx + + +