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
KnownReference: Add support for testing for referenced libraries #6726
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
695358b
Add KnowReference and tests
martin-strecker-sonarsource 7547b03
Fix code smell (parameter names)
martin-strecker-sonarsource 6b5d8f4
Add overloads for version passed as string.
martin-strecker-sonarsource 5f39f96
Add support for public key checks
martin-strecker-sonarsource c739408
Move predicates to own file
martin-strecker-sonarsource d3842bd
Rename to IsReferencedBy
martin-strecker-sonarsource 92deb06
Clean-up
martin-strecker-sonarsource 8422676
Add argument null checks
martin-strecker-sonarsource ec96228
Clean-up
martin-strecker-sonarsource ab95c9b
Remove Or and replace with params
martin-strecker-sonarsource 4caa689
argument naming
martin-strecker-sonarsource af2cdc8
test Naming
martin-strecker-sonarsource c4ae3a4
Move extension methods to the right file ine the right folder
martin-strecker-sonarsource 657c32e
Add suing after move
martin-strecker-sonarsource 9bef92a
Use IndexOf for "Contains"
martin-strecker-sonarsource 9450d62
Adopt tests for string comparison
martin-strecker-sonarsource 886b451
Use nested class
martin-strecker-sonarsource 98364c7
Move to extensions folder
martin-strecker-sonarsource abed89f
Remove "Any" extension method
martin-strecker-sonarsource d619346
Rename parameter
martin-strecker-sonarsource 6d341b1
Exception message
martin-strecker-sonarsource 078a782
Rename KnownReference to KnownAssembly
martin-strecker-sonarsource f9bbb8a
Rename SpecialTypeMethod
martin-strecker-sonarsource 0cd2ee6
Formatting
martin-strecker-sonarsource 88e2f90
Move file
martin-strecker-sonarsource d2794d2
File scoped namespace
martin-strecker-sonarsource 98ddc1b
Extract MockCompilation
martin-strecker-sonarsource 0c21bd5
Simplify project setup for tests
martin-strecker-sonarsource 54ed0dd
Rename method
martin-strecker-sonarsource 5117f76
Remove EditorBrowsable
martin-strecker-sonarsource cb7633f
Add public key token to XUnit
martin-strecker-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
74 changes: 74 additions & 0 deletions
74
analyzers/src/SonarAnalyzer.Common/Helpers/KnownAssembly.Predicates.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,74 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2023 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.Helpers; | ||
|
||
public sealed partial class KnownAssembly | ||
{ | ||
private const StringComparison AssemblyNameComparission = StringComparison.OrdinalIgnoreCase; | ||
|
||
internal static class Predicates | ||
{ | ||
internal static Func<AssemblyIdentity, bool> NameIs(string name) => | ||
x => x.Name.Equals(name, AssemblyNameComparission); | ||
|
||
internal static Func<AssemblyIdentity, bool> StartsWith(string name) => | ||
x => x.Name.StartsWith(name, AssemblyNameComparission); | ||
|
||
internal static Func<AssemblyIdentity, bool> EndsWith(string name) => | ||
x => x.Name.EndsWith(name, AssemblyNameComparission); | ||
|
||
internal static Func<AssemblyIdentity, bool> Contains(string name) => | ||
x => x.Name.IndexOf(name, 0, AssemblyNameComparission) >= 0; | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionLowerThen(string version) => | ||
VersionLowerThen(Version.Parse(version)); | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionLowerThen(Version version) => | ||
x => x.Version < version; | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionGreaterOrEqual(string version) => | ||
VersionGreaterOrEqual(Version.Parse(version)); | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionGreaterOrEqual(Version version) => | ||
x => x.Version >= version; | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionBetween(string from, string to) => | ||
VersionBetween(Version.Parse(from), Version.Parse(to)); | ||
|
||
internal static Func<AssemblyIdentity, bool> VersionBetween(Version from, Version to) => | ||
x => x.Version >= from && x.Version <= to; | ||
|
||
internal static Func<AssemblyIdentity, bool> OptionalPublicKeyTokenIs(string key) => | ||
x => !x.HasPublicKey || PublicKeyEqualHex(x, key); | ||
|
||
internal static Func<AssemblyIdentity, bool> PublicKeyTokenIs(string key) => | ||
x => x.HasPublicKey && PublicKeyEqualHex(x, key); | ||
|
||
private static bool PublicKeyEqualHex(AssemblyIdentity identity, string hexString) | ||
{ | ||
var normalizedHexString = hexString.Replace("-", string.Empty); | ||
return ArraysEqual(identity.PublicKeyToken.ToArray(), normalizedHexString) || ArraysEqual(identity.PublicKey.ToArray(), normalizedHexString); | ||
|
||
static bool ArraysEqual(byte[] key, string hexString) => | ||
BitConverter.ToString(key).Replace("-", string.Empty).Equals(hexString, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
analyzers/src/SonarAnalyzer.Common/Helpers/KnownAssembly.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,48 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2023 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using static SonarAnalyzer.Helpers.KnownAssembly.Predicates; | ||
|
||
namespace SonarAnalyzer.Helpers; | ||
|
||
public sealed partial class KnownAssembly | ||
{ | ||
private readonly Func<IEnumerable<AssemblyIdentity>, bool> predicate; | ||
|
||
public static KnownAssembly XUnit_Assert { get; } = new( | ||
And(NameIs("xunit.assert").Or(NameIs("xunit").And(VersionLowerThen("2.0"))), | ||
PublicKeyTokenIs("8d05b1bb7a6fdb6c"))); | ||
|
||
internal KnownAssembly(Func<AssemblyIdentity, bool> predicate, params Func<AssemblyIdentity, bool>[] or) | ||
: this(predicate is null || or.Any(x => x is null) | ||
? throw new ArgumentNullException(nameof(predicate), "All predicates must be non-null.") | ||
: identities => identities.Any(identitiy => predicate(identitiy) || or.Any(orPredicate => orPredicate(identitiy)))) | ||
{ | ||
} | ||
|
||
internal KnownAssembly(Func<IEnumerable<AssemblyIdentity>, bool> predicate) => | ||
this.predicate = predicate ?? throw new ArgumentNullException(nameof(predicate)); | ||
|
||
public bool IsReferencedBy(Compilation compilation) => | ||
predicate(compilation.ReferencedAssemblyNames); | ||
|
||
internal static Func<AssemblyIdentity, bool> And(Func<AssemblyIdentity, bool> left, Func<AssemblyIdentity, bool> right) => | ||
KnownAssemblyExtensions.And(left, right); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is somewhat overlapping with
NetFrameworkVersionProvider
classhttps://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/src/SonarAnalyzer.Common/Helpers/NetFrameworkVersionProvider.cs
While this extension can live here, the implementation should be in that class to have this kind of ugly detection should live on one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementations can not easily be merged. I created #6751