-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH-87] Added convention to enforce a project file treats a given com…
…piler warning as an error
- Loading branch information
eddie.stanley
committed
Jan 6, 2024
1 parent
be5b0b6
commit 62e51a9
Showing
6 changed files
with
113 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
...entional/Conventions/Assemblies/MustTreatWarningAsErrorAssemblyConventionSpecification.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,42 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public class MustTreatWarningAsErrorAssemblyConventionSpecification : AssemblyConventionSpecification | ||
{ | ||
private readonly string _warning; | ||
|
||
public MustTreatWarningAsErrorAssemblyConventionSpecification(string warning) | ||
{ | ||
_warning = warning; | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedByLegacyCsprojFormat(string assemblyName, XDocument projectDocument) | ||
{ | ||
return IsSatisfiedBy(assemblyName, projectDocument); | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedBy(string assemblyName, XDocument projectDocument) | ||
{ | ||
// The Project element (and descendants) are namespaced in legacy csproj files, so our XPath ignores the | ||
// namespace by considering the local element name only. Once we no-longer need to support legacy csproj | ||
// files, the XPath can be simplified to /Project/ItemGroup/ProjectReference | ||
var warningsAsErrors = projectDocument.XPathSelectElements("/*[local-name() = 'Project']/*[local-name() = 'PropertyGroup']/*[local-name() = 'WarningsAsErrors']") | ||
.SingleOrDefault() | ||
?.Value ?? ""; | ||
|
||
var warnings = warningsAsErrors.Split(new[] {' ', ','}, StringSplitOptions.RemoveEmptyEntries); | ||
if (!warnings.Contains(_warning)) | ||
{ | ||
return ConventionResult.NotSatisfied(assemblyName, string.Format(FailureMessage, assemblyName, _warning)); | ||
} | ||
|
||
return ConventionResult.Satisfied(assemblyName); | ||
} | ||
|
||
protected override string FailureMessage => "Assembly {0} should treat warning {1} as an error but does not"; | ||
} | ||
} |
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
Binary file modified
BIN
+0 Bytes
(100%)
src/Core/TestSolution/TestProjectTwo/bin/Debug/TestProjectTwo.dll
Binary file not shown.
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