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

Added test to for ConfigurationContext #10535

Merged
merged 6 commits into from
Aug 26, 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
32 changes: 32 additions & 0 deletions src/BuildCheck.UnitTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,38 @@ public void EditorConfig_SeverityAppliedCorrectly(string BC0101Severity, string
}
}

[Fact]
public void CheckHasAccessToAllConfigs()
{
using (var env = TestEnvironment.Create())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need env here and not the global _env?

{
string checkCandidatePath = Path.Combine(TestAssetsRootPath, "CheckCandidate");
string message = ": An extra message for the analyzer";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ": An extra message for the check"

string severity = "warning";

// Can't use Transitive environment due to the need to dogfood local nuget packages.
AddCustomDataSourceToNugetConfig(checkCandidatePath);
string editorConfigName = Path.Combine(checkCandidatePath, EditorConfigFileName);
File.WriteAllText(editorConfigName, ReadEditorConfig(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i beleive you can use the existing ReadEditorConfig() method located in the same file EndToEndTests.cs in order to populate config

new List<(string, string)>() { ("X01234", severity) },
new List<(string, (string, string))>
{
("X01234",("setMessage", message))
},
checkCandidatePath));

string projectCheckBuildLog = RunnerUtilities.ExecBootstrapedMSBuild(
$"{Path.Combine(checkCandidatePath, $"CheckCandidate.csproj")} /m:1 -nr:False -restore -check -verbosity:n", out bool success, timeoutMilliseconds: 1200_0000);
success.ShouldBeTrue();

projectCheckBuildLog.ShouldContain("warning X01234");
projectCheckBuildLog.ShouldContain(severity + message);

// Cleanup
File.Delete(editorConfigName);
}
}

[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ root = true

[*.csproj]
build_check.X01234.Severity=X01234Severity
build_check.X01234.CustomConfig=dummy
12 changes: 11 additions & 1 deletion src/BuildCheck.UnitTests/TestAssets/CustomCheck/Check1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ public sealed class Check1 : Check

public override IReadOnlyList<CheckRule> SupportedRules { get; } = new List<CheckRule>() { SupportedRule };

private string message = "Argument for the message format";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: message -> _message


public override void Initialize(ConfigurationContext configurationContext)
{
var infraData = configurationContext.CheckConfig[0];
var customData = configurationContext.CustomConfigurationData[0].ConfigurationData;
// configurationContext to be used only if check needs external configuration data.
if (customData is not null &&
configurationContext.CustomConfigurationData[0].RuleId == "X01234" &&
customData.TryGetValue("setmessage", out string? setMessage))
{
message = infraData.Severity + setMessage;
}
}

public override void RegisterActions(IBuildCheckRegistrationContext registrationContext)
Expand All @@ -32,7 +42,7 @@ private void EvaluatedPropertiesAction(BuildCheckDataContext<EvaluatedProperties
context.ReportResult(BuildCheckResult.Create(
SupportedRule,
ElementLocation.EmptyLocation,
"Argument for the message format"));
message));
}
}
}