From ccb3848ee6ad7cc105cc131eae7250df3a8357d0 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Fri, 24 May 2024 23:07:22 -0400 Subject: [PATCH 1/4] Add trimmable tests --- .github/workflows/wf-build-test.yml | 6 ++++- QRCoder.sln | 22 +++++++++++++++++-- QRCoder/QRCoder.csproj | 1 + QRCoderTrimAnalysis/Program.cs | 2 ++ .../QRCoderTrimAnalysis.csproj | 21 ++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 QRCoderTrimAnalysis/Program.cs create mode 100644 QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj diff --git a/.github/workflows/wf-build-test.yml b/.github/workflows/wf-build-test.yml index 18ca287f..eca43ce8 100644 --- a/.github/workflows/wf-build-test.yml +++ b/.github/workflows/wf-build-test.yml @@ -86,7 +86,11 @@ jobs: - name: Run test .NET 6.0 Windows working-directory: QRCoderTests run: dotnet test -c Release -f net6.0-windows --nologo --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover - + + - name: Run trim analysis + working-directory: QRCoderTrimAnalysis + run: dotnet publish -c Release -o bin/publish + - name: Run API approval tests working-directory: QRCoderApiTests run: dotnet test -c Release --nologo --no-build diff --git a/QRCoder.sln b/QRCoder.sln index 7723fafb..3d38636f 100644 --- a/QRCoder.sln +++ b/QRCoder.sln @@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderTests", "QRCoderTest EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoder.Xaml", "QRCoder.Xaml\QRCoder.Xaml.csproj", "{A7A7E073-2504-4BA2-A63B-87AC34174789}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderTrimAnalysis", "QRCoderTrimAnalysis\QRCoderTrimAnalysis.csproj", "{F046136A-7BEA-49F3-9415-70CE50AD533B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -155,6 +157,22 @@ Global {C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x64.Build.0 = Release|Any CPU {C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.ActiveCfg = Release|Any CPU {C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.Build.0 = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.ActiveCfg = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.Build.0 = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.ActiveCfg = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.Build.0 = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.ActiveCfg = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.Build.0 = Debug|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.Build.0 = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.ActiveCfg = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.Build.0 = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.ActiveCfg = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.Build.0 = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.ActiveCfg = Release|Any CPU + {F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/QRCoder/QRCoder.csproj b/QRCoder/QRCoder.csproj index c9a30473..cb1bb0a9 100644 --- a/QRCoder/QRCoder.csproj +++ b/QRCoder/QRCoder.csproj @@ -8,6 +8,7 @@ $(DefineConstants);NET6_0_WINDOWS false true + true diff --git a/QRCoderTrimAnalysis/Program.cs b/QRCoderTrimAnalysis/Program.cs new file mode 100644 index 00000000..3751555c --- /dev/null +++ b/QRCoderTrimAnalysis/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj b/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj new file mode 100644 index 00000000..3f4eedf8 --- /dev/null +++ b/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj @@ -0,0 +1,21 @@ + + + + + + + Exe + net8.0 + enable + enable + true + true + false + + + + + + + + From 630a3522802f97ac1ac297765bb6f717af9d2c3e Mon Sep 17 00:00:00 2001 From: Shane32 Date: Fri, 24 May 2024 23:16:37 -0400 Subject: [PATCH 2/4] Fix trim warnings --- QRCoder/Extensions/StringValueAttribute.cs | 6 ++++++ QRCoder/PayloadGenerator.cs | 15 +++++++-------- QRCoder/SvgQRCode.cs | 22 ++++++++++++++++++++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/QRCoder/Extensions/StringValueAttribute.cs b/QRCoder/Extensions/StringValueAttribute.cs index cc6e1bf4..f8ff37ef 100644 --- a/QRCoder/Extensions/StringValueAttribute.cs +++ b/QRCoder/Extensions/StringValueAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Text; @@ -9,6 +10,7 @@ namespace QRCoder.Extensions /// /// Used to represent a string value for a value in an enum /// + [Obsolete("This attribute will be removed in a future version of QRCoder.")] public class StringValueAttribute : Attribute { @@ -31,6 +33,7 @@ public StringValueAttribute(string value) } } + [Obsolete("This class will be removed in a future version of QRCoder.")] public static class CustomExtensions { /// @@ -38,6 +41,9 @@ public static class CustomExtensions /// /// /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")] +#endif public static string GetStringValue(this Enum value) { #if NETSTANDARD1_3 diff --git a/QRCoder/PayloadGenerator.cs b/QRCoder/PayloadGenerator.cs index bddd084b..9fcbeec1 100644 --- a/QRCoder/PayloadGenerator.cs +++ b/QRCoder/PayloadGenerator.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Text; using System.Text.RegularExpressions; +using System.Diagnostics.CodeAnalysis; #if NETSTANDARD1_3 using System.Reflection; #endif @@ -2477,14 +2478,12 @@ public class RussiaPaymentOrder : Payload //base private CharacterSets characterSet; - private MandatoryFields mFields; - private OptionalFields oFields; + private readonly MandatoryFields mFields = new MandatoryFields(); + private readonly OptionalFields oFields = new OptionalFields(); private string separator = "|"; private RussiaPaymentOrder() { - mFields = new MandatoryFields(); - oFields = new OptionalFields(); } /// @@ -2596,7 +2595,7 @@ private string DetermineSeparator() private List GetOptionalFieldsAsList() { #if NETSTANDARD1_3 - return oFields.GetType().GetRuntimeProperties() + return typeof(OptionalFields).GetRuntimeProperties() .Where(field => field.GetValue(oFields) != null) .Select(field => { var objValue = field.GetValue(oFields, null); @@ -2605,7 +2604,7 @@ private List GetOptionalFieldsAsList() }) .ToList(); #else - return oFields.GetType().GetProperties() + return typeof(OptionalFields).GetProperties() .Where(field => field.GetValue(oFields, null) != null) .Select(field => { var objValue = field.GetValue(oFields, null); @@ -2624,7 +2623,7 @@ private List GetOptionalFieldsAsList() private List GetMandatoryFieldsAsList() { #if NETSTANDARD1_3 - return mFields.GetType().GetRuntimeFields() + return typeof(MandatoryFields).GetRuntimeFields() .Where(field => field.GetValue(mFields) != null) .Select(field => { var objValue = field.GetValue(mFields); @@ -2633,7 +2632,7 @@ private List GetMandatoryFieldsAsList() }) .ToList(); #else - return mFields.GetType().GetFields() + return typeof(MandatoryFields).GetFields() .Where(field => field.GetValue(mFields) != null) .Select(field => { var objValue = field.GetValue(mFields); diff --git a/QRCoder/SvgQRCode.cs b/QRCoder/SvgQRCode.cs index 642b8289..2ac3b4d7 100644 --- a/QRCoder/SvgQRCode.cs +++ b/QRCoder/SvgQRCode.cs @@ -362,7 +362,7 @@ public MediaType GetMediaType() /// public string GetDataUri() { - return $"data:{_mediaType.GetStringValue()};base64,{_logoData}"; + return $"data:{GetMimeType(_mediaType)};base64,{_logoData}"; } /// @@ -388,11 +388,29 @@ public bool FillLogoBackground() /// public enum MediaType : int { +#pragma warning disable CS0618 // Type or member is obsolete [StringValue("image/png")] - PNG = 0, +#pragma warning restore CS0618 // Type or member is obsolete + PNG = 0, +#pragma warning disable CS0618 // Type or member is obsolete [StringValue("image/svg+xml")] +#pragma warning restore CS0618 // Type or member is obsolete SVG = 1 } + + private string GetMimeType(MediaType type) + { + switch (type) + { + case MediaType.PNG: + return "image/png"; + case MediaType.SVG: + return "image/svg+xml"; + default: + throw new ArgumentOutOfRangeException(nameof(type)); + } + } + } } From 9eb686af1ddc8d6bae18d78b6bdc0010d991d08d Mon Sep 17 00:00:00 2001 From: Shane32 Date: Fri, 24 May 2024 23:21:09 -0400 Subject: [PATCH 3/4] Update link --- QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj b/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj index 3f4eedf8..0f65205f 100644 --- a/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj +++ b/QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj @@ -1,7 +1,7 @@  - + Exe From 535f927ff212632aade031468bfe8e53df8485ac Mon Sep 17 00:00:00 2001 From: Shane32 Date: Fri, 24 May 2024 23:49:06 -0400 Subject: [PATCH 4/4] Update API approvals --- .../QRCoder.approved.txt | 2 ++ QRCoderApiTests/net60-windows/QRCoder.approved.txt | 3 +++ QRCoderApiTests/net60/QRCoder.approved.txt | 3 +++ QRCoderApiTests/netstandard13/QRCoder.approved.txt | 2 ++ 4 files changed, 10 insertions(+) diff --git a/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt b/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt index e43622bf..4c5794a4 100644 --- a/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt +++ b/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt @@ -967,10 +967,12 @@ namespace QRCoder.Exceptions } namespace QRCoder.Extensions { + [System.Obsolete("This class will be removed in a future version of QRCoder.")] public static class CustomExtensions { public static string GetStringValue(this System.Enum value) { } } + [System.Obsolete("This attribute will be removed in a future version of QRCoder.")] public class StringValueAttribute : System.Attribute { public StringValueAttribute(string value) { } diff --git a/QRCoderApiTests/net60-windows/QRCoder.approved.txt b/QRCoderApiTests/net60-windows/QRCoder.approved.txt index 1aeace01..52c12031 100644 --- a/QRCoderApiTests/net60-windows/QRCoder.approved.txt +++ b/QRCoderApiTests/net60-windows/QRCoder.approved.txt @@ -977,10 +977,13 @@ namespace QRCoder.Exceptions } namespace QRCoder.Extensions { + [System.Obsolete("This class will be removed in a future version of QRCoder.")] public static class CustomExtensions { + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")] public static string GetStringValue(this System.Enum value) { } } + [System.Obsolete("This attribute will be removed in a future version of QRCoder.")] public class StringValueAttribute : System.Attribute { public StringValueAttribute(string value) { } diff --git a/QRCoderApiTests/net60/QRCoder.approved.txt b/QRCoderApiTests/net60/QRCoder.approved.txt index fa3205a9..fd21ee83 100644 --- a/QRCoderApiTests/net60/QRCoder.approved.txt +++ b/QRCoderApiTests/net60/QRCoder.approved.txt @@ -904,10 +904,13 @@ namespace QRCoder.Exceptions } namespace QRCoder.Extensions { + [System.Obsolete("This class will be removed in a future version of QRCoder.")] public static class CustomExtensions { + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")] public static string GetStringValue(this System.Enum value) { } } + [System.Obsolete("This attribute will be removed in a future version of QRCoder.")] public class StringValueAttribute : System.Attribute { public StringValueAttribute(string value) { } diff --git a/QRCoderApiTests/netstandard13/QRCoder.approved.txt b/QRCoderApiTests/netstandard13/QRCoder.approved.txt index 2e7dacea..bada43f1 100644 --- a/QRCoderApiTests/netstandard13/QRCoder.approved.txt +++ b/QRCoderApiTests/netstandard13/QRCoder.approved.txt @@ -831,10 +831,12 @@ namespace QRCoder.Exceptions } namespace QRCoder.Extensions { + [System.Obsolete("This class will be removed in a future version of QRCoder.")] public static class CustomExtensions { public static string GetStringValue(this System.Enum value) { } } + [System.Obsolete("This attribute will be removed in a future version of QRCoder.")] public class StringValueAttribute : System.Attribute { public StringValueAttribute(string value) { }