From 628f954ef5fc28e3792a3cdef157026fe55056d2 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 18 Sep 2024 11:27:05 -0500 Subject: [PATCH 1/2] fix parsing of ports that lack port type metadata --- .../ContainerHelpers.cs | 12 ++++++------ .../PublicAPI/net472/PublicAPI.Unshipped.txt | 8 ++++---- .../PublicAPI/net8.0/PublicAPI.Unshipped.txt | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Containers/Microsoft.NET.Build.Containers/ContainerHelpers.cs b/src/Containers/Microsoft.NET.Build.Containers/ContainerHelpers.cs index 3a1ff7b8d004..46d1b7ba3cae 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/ContainerHelpers.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/ContainerHelpers.cs @@ -41,10 +41,10 @@ public static class ContainerHelpers [Flags] public enum ParsePortError { - MissingPortNumber, - InvalidPortNumber, - InvalidPortType, - UnknownPortFormat + MissingPortNumber = 1, + InvalidPortNumber = 2, + InvalidPortType = 4, + UnknownPortFormat = 8 } /// @@ -68,9 +68,9 @@ public static bool TryParsePort(string? portNumber, string? portType, [NotNullWh error = ParsePortError.InvalidPortNumber; } - if (!Enum.TryParse(portType, out PortType t)) + if (!Enum.TryParse(portType, out PortType t)) { - if (portType is not null) + if (!string.IsNullOrEmpty(portType)) { error = (error ?? ParsePortError.InvalidPortType) | ParsePortError.InvalidPortType; } diff --git a/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt index adf04ca7a59b..6dd6551ddb37 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1,9 +1,9 @@ Microsoft.NET.Build.Containers.ContainerHelpers Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 0 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber | Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 4 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = 8 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError Microsoft.NET.Build.Containers.Port Microsoft.NET.Build.Containers.Port.Deconstruct(out int Number, out Microsoft.NET.Build.Containers.PortType Type) -> void Microsoft.NET.Build.Containers.Port.Equals(Microsoft.NET.Build.Containers.Port other) -> bool diff --git a/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt b/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt index 698c2894586b..c1a657fc58b9 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt +++ b/src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt @@ -21,10 +21,10 @@ Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.UsesInvariantG static readonly Microsoft.NET.Build.Containers.Constants.Version -> string! Microsoft.NET.Build.Containers.ContainerHelpers Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 0 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError -Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber | Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 4 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError +Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = 8 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError Microsoft.NET.Build.Containers.Descriptor Microsoft.NET.Build.Containers.Descriptor.Annotations.get -> System.Collections.Generic.Dictionary? Microsoft.NET.Build.Containers.Descriptor.Annotations.init -> void From 5004a6877ab3143b0aa4b4676f8f403e82290619 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 25 Sep 2024 09:35:07 -0500 Subject: [PATCH 2/2] update test cases now that logic is correct --- .../ContainerHelpersTests.cs | 2 +- src/Tests/containerize.UnitTests/ParserTests.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Tests/Microsoft.NET.Build.Containers.UnitTests/ContainerHelpersTests.cs b/src/Tests/Microsoft.NET.Build.Containers.UnitTests/ContainerHelpersTests.cs index 4d83f91b175c..ecf0cf61f691 100644 --- a/src/Tests/Microsoft.NET.Build.Containers.UnitTests/ContainerHelpersTests.cs +++ b/src/Tests/Microsoft.NET.Build.Containers.UnitTests/ContainerHelpersTests.cs @@ -109,7 +109,7 @@ public void IsValidImageTag_InvalidLength() [InlineData("80", true, 80, PortType.tcp, null)] [InlineData("125/dup", false, 125, PortType.tcp, ContainerHelpers.ParsePortError.InvalidPortType)] [InlineData("invalidNumber", false, null, null, ContainerHelpers.ParsePortError.InvalidPortNumber)] - [InlineData("welp/unknowntype", false, null, null, (ContainerHelpers.ParsePortError)3)] + [InlineData("welp/unknowntype", false, null, null, (ContainerHelpers.ParsePortError)6)] [InlineData("a/b/c", false, null, null, ContainerHelpers.ParsePortError.UnknownPortFormat)] [InlineData("/tcp", false, null, null, ContainerHelpers.ParsePortError.MissingPortNumber)] public void CanParsePort(string input, bool shouldParse, int? expectedPortNumber, PortType? expectedType, ContainerHelpers.ParsePortError? expectedError) { diff --git a/src/Tests/containerize.UnitTests/ParserTests.cs b/src/Tests/containerize.UnitTests/ParserTests.cs index 7a43de11b233..079d3068d1ae 100644 --- a/src/Tests/containerize.UnitTests/ParserTests.cs +++ b/src/Tests/containerize.UnitTests/ParserTests.cs @@ -204,9 +204,9 @@ public void CanParsePorts() } [Theory] - [InlineData("1501/smth", "(MissingPortNumber, InvalidPortType)")] - [InlineData("1501\\tcp", "(MissingPortNumber, InvalidPortNumber)")] - [InlineData("not-a-number", "(MissingPortNumber, InvalidPortNumber)")] + [InlineData("1501/smth", "(InvalidPortType)")] + [InlineData("1501\\tcp", "(InvalidPortNumber)")] + [InlineData("not-a-number", "(InvalidPortNumber)")] public void CanHandleInvalidPorts(string portStr, string reason) { string errorMessage = $"Incorrectly formatted ports:{Environment.NewLine}\t{portStr}:\t{reason}{Environment.NewLine}";