Skip to content

Commit 3f7bbfb

Browse files
authored
Cherry pick to v0.1 - More correct image name normalization (#119) (#129)
More correct image name normalization (#119) more correct image name normalization
1 parent 0818510 commit 3f7bbfb

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

Microsoft.NET.Build.Containers/ContainerHelpers.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace Microsoft.NET.Build.Containers;
55

66
public static class ContainerHelpers
77
{
8-
private static Regex imageTagRegex = new Regex("^[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}$");
8+
private static Regex imageTagRegex = new Regex(@"^[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}$");
99

10-
private static Regex imageNameRegex = new Regex("^[a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*$");
10+
private static Regex imageNameRegex = new Regex(@"^[a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*$");
1111

1212
/// <summary>
1313
/// Matches if the string is not lowercase or numeric, or ., _, or -.
1414
/// </summary>
15-
private static Regex imageNameCharacters = new Regex("[^a-zA-Z0-9._-]");
15+
private static Regex imageNameCharacters = new Regex(@"[^a-z0-9._\-/]");
1616

1717
/// <summary>
1818
/// Given some "fully qualified" image name (e.g. mcr.microsoft.com/dotnet/runtime), return
@@ -133,13 +133,11 @@ public static bool NormalizeImageName(string containerImageName, [NotNullWhen(fa
133133
}
134134
else
135135
{
136-
if (Char.IsUpper(containerImageName, 0))
137-
{
138-
containerImageName = Char.ToLowerInvariant(containerImageName[0]) + containerImageName[1..];
139-
} else if (!Char.IsLetterOrDigit(containerImageName, 0)) {
136+
if (!Char.IsLetterOrDigit(containerImageName, 0)) {
140137
throw new ArgumentException("The first character of the image name must be a lowercase letter or a digit.");
141138
}
142-
normalizedImageName = imageNameCharacters.Replace(containerImageName, "-");
139+
var loweredImageName = containerImageName.ToLowerInvariant();
140+
normalizedImageName = imageNameCharacters.Replace(loweredImageName, "-");
143141
return false;
144142
}
145143
}

Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ public void CanSetEntrypointArgsToUseAppHost(bool useAppHost, params string[] en
8383
}
8484
}
8585

86-
[DataRow("WebApplication44", "webApplication44", true)]
86+
[DataRow("WebApplication44", "webapplication44", true)]
8787
[DataRow("friendly-suspicious-alligator", "friendly-suspicious-alligator", true)]
8888
[DataRow("*friendly-suspicious-alligator", "", false)]
89-
[DataRow("web/app2+7", "web-app2-7", true)]
89+
[DataRow("web/app2+7", "web/app2-7", true)]
90+
[DataRow("Microsoft.Apps.Demo.ContosoWeb", "microsoft.apps.demo.contosoweb", true)]
9091
[TestMethod]
9192
public void CanNormalizeInputContainerNames(string projectName, string expectedContainerImageName, bool shouldPass)
9293
{

0 commit comments

Comments
 (0)