Skip to content

Commit

Permalink
Use some StringSyntax attributes (dotnet#10813)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainersigwald authored Dec 4, 2024
1 parent e5669f0 commit 5491064
Show file tree
Hide file tree
Showing 22 changed files with 234 additions and 136 deletions.
10 changes: 5 additions & 5 deletions src/Build.UnitTests/BackEnd/NodePackets_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,31 +289,31 @@ public void TestTranslation()
CreateTargetSkipped(),
new ExtendedBuildErrorEventArgs("extError", "SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender", DateTime.UtcNow, "arg1")
{
ExtendedData = "{'long-json':'mostly-strings'}",
ExtendedData = /*lang=json*/ "{'long-json':'mostly-strings'}",
ExtendedMetadata = new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } },
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6, 7)
},
new ExtendedBuildWarningEventArgs("extWarn", "SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender", DateTime.UtcNow, "arg1")
{
ExtendedData = "{'long-json':'mostly-strings'}",
ExtendedData = /*lang=json*/ "{'long-json':'mostly-strings'}",
ExtendedMetadata = new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } },
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6, 7)
},
new ExtendedBuildMessageEventArgs("extWarn", "SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender", MessageImportance.Normal, DateTime.UtcNow, "arg1")
{
ExtendedData = "{'long-json':'mostly-strings'}",
ExtendedData = /*lang=json*/ "{'long-json':'mostly-strings'}",
ExtendedMetadata = new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } },
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6, 7)
},
new ExtendedCustomBuildEventArgs("extCustom", "message", "help", "sender", DateTime.UtcNow, "arg1")
{
ExtendedData = "{'long-json':'mostly-strings'}",
ExtendedData = /*lang=json*/ "{'long-json':'mostly-strings'}",
ExtendedMetadata = new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } },
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6, 7)
},
new ExtendedCriticalBuildMessageEventArgs("extCritMsg", "Subcategory", "Code", "File", 1, 2, 3, 4, "{0}", "HelpKeyword", "Sender", DateTime.Now, "arg1")
{
ExtendedData = "{'long-json':'mostly-strings'}",
ExtendedData = /*lang=json*/ "{'long-json':'mostly-strings'}",
ExtendedMetadata = new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } },
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6, 7)
},
Expand Down
10 changes: 5 additions & 5 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void RoundtripExtendedErrorEventArgs_SerializedAsError(bool withOptionalD
DateTime.Parse("9/1/2021 12:02:07 PM"),
withOptionalData ? new object[] { "argument0" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -414,7 +414,7 @@ public void RoundtripExtendedWarningEventArgs_SerializedAsWarning(bool withOptio
DateTime.Parse("9/1/2021 12:02:07 PM"),
withOptionalData ? new object[] { "argument0" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -490,7 +490,7 @@ public void RoundtripExtendedBuildMessageEventArgs_SerializedAsMessage(bool with
DateTime.Parse("12/12/2015 06:11:56 PM"),
withOptionalData ? new object[] { "argument0" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -578,7 +578,7 @@ public void ExtendedCustomBuildEventArgs_SerializedAsMessage(bool withOptionalDa
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -673,7 +673,7 @@ public void RoundtripExtendedCriticalBuildMessageEventArgs(bool withOptionalData
DateTime.Parse("12/12/2015 06:11:56 PM"),
withOptionalData ? new object[] { "argument0" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down
126 changes: 64 additions & 62 deletions src/Build.UnitTests/Construction/SolutionFilter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -108,18 +109,19 @@ public void SolutionFilterFiltersProjects(bool graphBuild)
EndGlobal
""");
TransientTestFile filterFile = testEnvironment.CreateFile(folder, "solutionFilter.slnf",
@"
{
""solution"": {
// I'm a comment
""path"": "".\\SimpleProject\\SimpleProject.sln"",
""projects"": [
/* ""..\\ClassLibrary\\ClassLibrary\\ClassLibrary.csproj"", */
""SimpleProject\\SimpleProject.csproj"",
]
}
}
");
/*lang=json*/
"""
{
"solution": {
// I'm a comment
"path": ".\\SimpleProject\\SimpleProject.sln",
"projects": [
/* "..\\ClassLibrary\\ClassLibrary\\ClassLibrary.csproj", */
"SimpleProject\\SimpleProject.csproj",
]
}
}
""");
Directory.GetCurrentDirectory().ShouldNotBe(Path.GetDirectoryName(filterFile.Path));
if (graphBuild)
{
Expand Down Expand Up @@ -151,59 +153,59 @@ public void SolutionFilterFiltersProjects(bool graphBuild)
}

[Theory]
[InlineData(@"
{
""solution"": {
""path"": ""C:\\notAPath\\MSBuild.Dev.sln"",
""projects2"": [
""src\\Build\\Microsoft.Build.csproj"",
""src\\Framework\\Microsoft.Build.Framework.csproj"",
""src\\MSBuild\\MSBuild.csproj"",
""src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj""
]
}
[InlineData(/*lang=json,strict*/ """
{
"solution": {
"path": "C:\\notAPath\\MSBuild.Dev.sln",
"projects2": [
"src\\Build\\Microsoft.Build.csproj",
"src\\Framework\\Microsoft.Build.Framework.csproj",
"src\\MSBuild\\MSBuild.csproj",
"src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj"
]
}
", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(@"
[{
""solution"": {
""path"": ""C:\\notAPath\\MSBuild.Dev.sln"",
""projects"": [
""src\\Build\\Microsoft.Build.csproj"",
""src\\Framework\\Microsoft.Build.Framework.csproj"",
""src\\MSBuild\\MSBuild.csproj"",
""src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj""
]
}
}]
", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(@"
{
""solution"": {
""path"": ""C:\\notAPath\\MSBuild.Dev.sln"",
""projects"": [
{""path"": ""src\\Build\\Microsoft.Build.csproj""},
{""path"": ""src\\Framework\\Microsoft.Build.Framework.csproj""},
{""path"": ""src\\MSBuild\\MSBuild.csproj""},
{""path"": ""src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj""}
]
}
}
""", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(/*lang=json,strict*/ """
[{
"solution": {
"path": "C:\\notAPath\\MSBuild.Dev.sln",
"projects": [
"src\\Build\\Microsoft.Build.csproj",
"src\\Framework\\Microsoft.Build.Framework.csproj",
"src\\MSBuild\\MSBuild.csproj",
"src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj"
]
}
", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(@"
{
""solution"": {
""path"": ""C:\\notAPath2\\MSBuild.Dev.sln"",
""projects"": [
{""path"": ""src\\Build\\Microsoft.Build.csproj""},
{""path"": ""src\\Framework\\Microsoft.Build.Framework.csproj""},
{""path"": ""src\\MSBuild\\MSBuild.csproj""},
{""path"": ""src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj""}
]
}
}]
""", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(/*lang=json,strict*/ """
{
"solution": {
"path": "C:\\notAPath\\MSBuild.Dev.sln",
"projects": [
{"path": "src\\Build\\Microsoft.Build.csproj"},
{"path": "src\\Framework\\Microsoft.Build.Framework.csproj"},
{"path": "src\\MSBuild\\MSBuild.csproj"},
{"path": "src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj"}
]
}
}
""", "MSBuild.SolutionFilterJsonParsingError")]
[InlineData(/*lang=json,strict*/ """
{
"solution": {
"path": "C:\\notAPath2\\MSBuild.Dev.sln",
"projects": [
{"path": "src\\Build\\Microsoft.Build.csproj"},
{"path": "src\\Framework\\Microsoft.Build.Framework.csproj"},
{"path": "src\\MSBuild\\MSBuild.csproj"},
{"path": "src\\Tasks.UnitTests\\Microsoft.Build.Tasks.UnitTests.csproj"}
]
}
", "MSBuild.SolutionFilterMissingSolutionError")]
public void InvalidSolutionFilters(string slnfValue, string exceptionReason)
}
""", "MSBuild.SolutionFilterMissingSolutionError")]
public void InvalidSolutionFilters([StringSyntax(StringSyntaxAttribute.Json)] string slnfValue, string exceptionReason)
{
Assert.False(File.Exists("C:\\notAPath2\\MSBuild.Dev.sln"));
using (TestEnvironment testEnvironment = TestEnvironment.Create())
Expand Down
10 changes: 5 additions & 5 deletions src/Framework.UnitTests/ExtendedBuildEventArgs_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void ExtendedCustomBuildEventArgs_SerializationDeserialization(bool withO
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string?> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -62,7 +62,7 @@ public void ExtendedErrorEventArgs_SerializationDeserialization(bool withOptiona
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string?> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -101,7 +101,7 @@ public void ExtendedWarningEventArgs_SerializationDeserialization(bool withOptio
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string?> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -139,7 +139,7 @@ public void ExtendedMessageEventArgs_SerializationDeserialization(bool withOptio
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string?> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down Expand Up @@ -176,7 +176,7 @@ public void ExtendedCriticalMessageEventArgs_SerializationDeserialization(bool w
eventTimestamp: withOptionalData ? DateTime.Parse("3/1/2017 11:11:56 AM") : DateTime.Now,
messageArgs: withOptionalData ? new object[] { "arg0val", "arg1val" } : null)
{
ExtendedData = withOptionalData ? "{'long-json':'mostly-strings'}" : null,
ExtendedData = withOptionalData ? /*lang=json*/ "{'long-json':'mostly-strings'}" : null,
ExtendedMetadata = withOptionalData ? new Dictionary<string, string?> { { "m1", "v1" }, { "m2", "v2" } } : null,
BuildEventContext = withOptionalData ? new BuildEventContext(1, 2, 3, 4, 5, 6, 7) : null,
};
Expand Down
7 changes: 4 additions & 3 deletions src/Framework/BuildErrorEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.Build.Shared;

Expand Down Expand Up @@ -149,7 +150,7 @@ public BuildErrorEventArgs(
int columnNumber,
int endLineNumber,
int endColumnNumber,
string message,
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string message,
string helpKeyword,
string senderName,
DateTime eventTimestamp,
Expand Down Expand Up @@ -183,10 +184,10 @@ public BuildErrorEventArgs(
int columnNumber,
int endLineNumber,
int endColumnNumber,
string message,
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string message,
string helpKeyword,
string senderName,
string helpLink,
[StringSyntax(StringSyntaxAttribute.Uri)] string helpLink,
DateTime eventTimestamp,
params object[] messageArgs)
: base(message, helpKeyword, senderName, eventTimestamp, messageArgs)
Expand Down
5 changes: 3 additions & 2 deletions src/Framework/BuildMessageEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -98,7 +99,7 @@ public BuildMessageEventArgs(
/// <param name="eventTimestamp">Timestamp when event was created</param>
/// <param name="messageArgs">message arguments</param>
public BuildMessageEventArgs(
string message,
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string message,
string helpKeyword,
string senderName,
MessageImportance importance,
Expand Down Expand Up @@ -219,7 +220,7 @@ public BuildMessageEventArgs(
int columnNumber,
int endLineNumber,
int endColumnNumber,
string message,
[StringSyntax(StringSyntaxAttribute.CompositeFormat)] string message,
string helpKeyword,
string senderName,
MessageImportance importance,
Expand Down
Loading

0 comments on commit 5491064

Please sign in to comment.