Skip to content

Commit bd78418

Browse files
[release/8.0.4xx] [Containers] Fix parsing and error reporting of ports that lack port type metadata (#43934)
Co-authored-by: Chet Husk <[email protected]>
1 parent 29991fa commit bd78418

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/Containers/Microsoft.NET.Build.Containers/ContainerHelpers.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public static class ContainerHelpers
4141
[Flags]
4242
public enum ParsePortError
4343
{
44-
MissingPortNumber,
45-
InvalidPortNumber,
46-
InvalidPortType,
47-
UnknownPortFormat
44+
MissingPortNumber = 1,
45+
InvalidPortNumber = 2,
46+
InvalidPortType = 4,
47+
UnknownPortFormat = 8
4848
}
4949

5050
/// <summary>
@@ -68,9 +68,9 @@ public static bool TryParsePort(string? portNumber, string? portType, [NotNullWh
6868
error = ParsePortError.InvalidPortNumber;
6969
}
7070

71-
if (!Enum.TryParse<PortType>(portType, out PortType t))
71+
if (!Enum.TryParse(portType, out PortType t))
7272
{
73-
if (portType is not null)
73+
if (!string.IsNullOrEmpty(portType))
7474
{
7575
error = (error ?? ParsePortError.InvalidPortType) | ParsePortError.InvalidPortType;
7676
}

src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Microsoft.NET.Build.Containers.ContainerHelpers
22
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
3-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
4-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
5-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 0 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
6-
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
3+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
4+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 4 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
5+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
6+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = 8 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
77
Microsoft.NET.Build.Containers.Port
88
Microsoft.NET.Build.Containers.Port.Deconstruct(out int Number, out Microsoft.NET.Build.Containers.PortType Type) -> void
99
Microsoft.NET.Build.Containers.Port.Equals(Microsoft.NET.Build.Containers.Port other) -> bool

src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.UsesInvariantG
2121
static readonly Microsoft.NET.Build.Containers.Constants.Version -> string!
2222
Microsoft.NET.Build.Containers.ContainerHelpers
2323
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
24-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
25-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
26-
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 0 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
27-
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
24+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortNumber = 2 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
25+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.InvalidPortType = 4 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
26+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.MissingPortNumber = 1 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
27+
Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError.UnknownPortFormat = 8 -> Microsoft.NET.Build.Containers.ContainerHelpers.ParsePortError
2828
Microsoft.NET.Build.Containers.Descriptor
2929
Microsoft.NET.Build.Containers.Descriptor.Annotations.get -> System.Collections.Generic.Dictionary<string!, string?>?
3030
Microsoft.NET.Build.Containers.Descriptor.Annotations.init -> void

src/Tests/Microsoft.NET.Build.Containers.UnitTests/ContainerHelpersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void IsValidImageTag_InvalidLength()
109109
[InlineData("80", true, 80, PortType.tcp, null)]
110110
[InlineData("125/dup", false, 125, PortType.tcp, ContainerHelpers.ParsePortError.InvalidPortType)]
111111
[InlineData("invalidNumber", false, null, null, ContainerHelpers.ParsePortError.InvalidPortNumber)]
112-
[InlineData("welp/unknowntype", false, null, null, (ContainerHelpers.ParsePortError)3)]
112+
[InlineData("welp/unknowntype", false, null, null, (ContainerHelpers.ParsePortError)6)]
113113
[InlineData("a/b/c", false, null, null, ContainerHelpers.ParsePortError.UnknownPortFormat)]
114114
[InlineData("/tcp", false, null, null, ContainerHelpers.ParsePortError.MissingPortNumber)]
115115
public void CanParsePort(string input, bool shouldParse, int? expectedPortNumber, PortType? expectedType, ContainerHelpers.ParsePortError? expectedError) {

src/Tests/containerize.UnitTests/ParserTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ public void CanParsePorts()
204204
}
205205

206206
[Theory]
207-
[InlineData("1501/smth", "(MissingPortNumber, InvalidPortType)")]
208-
[InlineData("1501\\tcp", "(MissingPortNumber, InvalidPortNumber)")]
209-
[InlineData("not-a-number", "(MissingPortNumber, InvalidPortNumber)")]
207+
[InlineData("1501/smth", "(InvalidPortType)")]
208+
[InlineData("1501\\tcp", "(InvalidPortNumber)")]
209+
[InlineData("not-a-number", "(InvalidPortNumber)")]
210210
public void CanHandleInvalidPorts(string portStr, string reason)
211211
{
212212
string errorMessage = $"Incorrectly formatted ports:{Environment.NewLine}\t{portStr}:\t{reason}{Environment.NewLine}";

0 commit comments

Comments
 (0)