Skip to content
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

[Tests only] Refactor PropertiesUsageAnalyzerTest #10511

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 37 additions & 41 deletions src/BuildCheck.UnitTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<Project DefaultTargets="PrintEnvVar">

<!-- MyProp4 is not defined - but it's checked against empty - which is allowed -->
<PropertyGroup Condition="'$(MyProp4)' == ''">
<!-- MyProp3 defined here - but not used anywhere -->
<!-- MyProp1 used here - but not defined -->
<MyProp3>$(MyProp1)</MyProp3>
</PropertyGroup>


<Target Name="PrintEnvVar">
<!-- MyProp2 used here - but defined later -->
<Message Text="MyProp2 has value $(MyProp2)" Importance="High" Condition="'$(MyProp2)' == ''" />
<PropertyGroup>
<MyProp2>$(MyProp2);xxx</MyProp2>
</PropertyGroup>
</Target>

</Project>
""";
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]
Expand Down Expand Up @@ -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<string>? 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>())
{
string supplementalContent = ReadAndAdjustProjectContent(supplementalAssetName);
TransientTestFile supplementalFile = _env.CreateFile(workFolder, supplementalAssetName, supplementalContent);
}

_env.CreateFile(workFolder, ".editorconfig", ReadEditorConfig(ruleToSeverity, ruleToCustomConfig, testAssetsFolderName));

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<Target Name="Hello">
<Message Importance="High" Condition="$(Test2) == true" Text="XYZABC" />
<Copy SourceFiles="TestFilePath" DestinationFolder="WorkFolderPath" />
<MSBuild Projects=".\FooBar-Copy.csproj" Targets="Hello" />
<MSBuild Projects=".\Project2.csproj" Targets="Hello" />

<PropertyGroup>
<ReadFromEnvVariable>$(TEST)</ReadFromEnvVariable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="bin/foo.dll" />
<Reference Include="bin/Project1.dll" />
</ItemGroup>

<Target Name="Hello">
<Message Importance="High" Condition="$(Test2) == true" Text="XYZABC" />
<Copy SourceFiles="TestFilePath" DestinationFolder="WorkFolderPath" />
</Target>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project DefaultTargets="PrintEnvVar">
<PropertyGroup>
<!--
<MyProp1>value-of-prop1</MyProp1>
<MyProp2>$(MyProp1)</MyProp2>
<MyProp3>blah</MyProp3>
-->
</PropertyGroup>

<PropertyGroup Condition="'$(MyProp12)' == ''">
<MyProp13>$(MyProp11)</MyProp13>
</PropertyGroup>

<!--
<ItemGroup>
<a Include="$(nonexistent)" />
</ItemGroup>
-->

<Target Name="PrintEnvVar">
<Message Text="MyPropT2 has value $(MyPropT2)" Importance="High" Condition="'$(MyPropT2)' == ''" />
<PropertyGroup>
<MyPropT2>$(MyPropT2);xxx</MyPropT2>
</PropertyGroup>
</Target>
</Project>