Skip to content

Commit

Permalink
Remove argument validation from CompileToAssembly
Browse files Browse the repository at this point in the history
Per PR feedback
  • Loading branch information
stephentoub committed Sep 30, 2021
1 parent 715e945 commit 4759212
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal static class Obsoletions
internal const string SignerInfoCounterSigMessage = "ComputeCounterSignature without specifying a CmsSigner is obsolete and is not supported. Use the overload that accepts a CmsSigner.";
internal const string SignerInfoCounterSigDiagId = "SYSLIB0035";

internal const string RegexCompileToAssemblyMessage = "Regex.CompileToAssembly is obsolete and not supported. Use the RegexGeneratorAttribute with the regular expression source generator instead.";
internal const string RegexCompileToAssemblyMessage = "Regex.CompileToAssembly is obsolete and not supported. Use RegexGeneratorAttribute with the regular expression source generator instead.";
internal const string RegexCompileToAssemblyDiagId = "SYSLIB0036";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<value>This operation is only allowed once per object.</value>
</data>
<data name="PlatformNotSupported_CompileToAssembly" xml:space="preserve">
<value>This platform does not support writing compiled regular expressions to an assembly.</value>
<value>This platform does not support writing compiled regular expressions to an assembly. Use RegexGeneratorAttribute with the regular expression source generator instead.</value>
</data>
<data name="QuantifierAfterNothing" xml:space="preserve">
<value>Quantifier {x,y} following nothing.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,8 @@ public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, Assembly
CompileToAssembly(regexinfos, assemblyname, attributes, null);

[Obsolete(Obsoletions.RegexCompileToAssemblyMessage, DiagnosticId = Obsoletions.RegexCompileToAssemblyDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[]? attributes, string? resourceFile)
{
if (assemblyname is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.assemblyname);
}

if (regexinfos is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.regexinfos);
}

foreach (RegexCompilationInfo info in regexinfos)
{
if (info is null)
{
throw new ArgumentNullException(nameof(regexinfos), SR.ArgumentNull_ArrayWithNullElements);
}
}

public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[]? attributes, string? resourceFile) =>
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CompileToAssembly);
}

/// <summary>
/// Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,30 @@

namespace System.Text.RegularExpressions.Tests
{
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
public class RegexCompileToAssemblyTests : FileCleanupTestBase
{
[Fact]
public void CompileToAssembly_InvalidArgs_Throws()
{
AssertExtensions.Throws<ArgumentNullException>("assemblyname", () => Regex.CompileToAssembly(null, null));
AssertExtensions.Throws<ArgumentNullException>("assemblyname", () => Regex.CompileToAssembly(null, null, null));
AssertExtensions.Throws<ArgumentNullException>("assemblyname", () => Regex.CompileToAssembly(null, null, null, null));

AssertExtensions.Throws<ArgumentNullException>("regexinfos", () => Regex.CompileToAssembly(null, new AssemblyName("abcd")));
AssertExtensions.Throws<ArgumentNullException>("regexinfos", () => Regex.CompileToAssembly(null, new AssemblyName("abcd"), null));
AssertExtensions.Throws<ArgumentNullException>("regexinfos", () => Regex.CompileToAssembly(null, new AssemblyName("abcd"), null, null));

AssertExtensions.Throws<ArgumentNullException>("regexinfos", "regexes", () => Regex.CompileToAssembly(new RegexCompilationInfo[] { null }, new AssemblyName("abcd")));
AssertExtensions.Throws<ArgumentNullException>("regexinfos", "regexes", () => Regex.CompileToAssembly(new RegexCompilationInfo[] { new RegexCompilationInfo("abc", RegexOptions.None, "abc", "", true), null }, new AssemblyName("abcd")));
AssertExtensions.Throws<ArgumentNullException>("regexinfos", "regexes", () => Regex.CompileToAssembly(new RegexCompilationInfo[] { null }, new AssemblyName("abcd"), new CustomAttributeBuilder[0]));
}

[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
public void CompileToAssembly_PNSE()
{
Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "SomeNamespace", true) },
new AssemblyName("abcd")));

Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.CultureInvariant, "abcd", "", true, TimeSpan.FromMinutes(1)) },
new AssemblyName("abcdWithTimeout")));

Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("(?<FirstTwoLetters>ab)cd", RegexOptions.None, "abcd", "", true, TimeSpan.FromMinutes(1)) },
new AssemblyName("abcdWithNamedCapture")));

Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo(".*\\B(\\d+)(?<output>SUCCESS)\\B.*", RegexOptions.None, "withCaptures", "", true) },
new AssemblyName("withCaptures")));

Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "", true) },
new AssemblyName("abcdWithCustomAttribute"),
new[] { new CustomAttributeBuilder(typeof(AssemblyCompanyAttribute).GetConstructor(new[] { typeof(string) }), new[] { "TestCompany" }) }));
}

[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
public void CompileToAssembly_ResourceFile_PNSE()
{
Assert.Throws<PlatformNotSupportedException>(() =>
Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "", true) },
new AssemblyName("abcdWithUnsupportedResourceFile"),
attributes: null,
"unsupportedResourceFile"));
Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(null, null));
Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(null, null, null));
Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(null, null, null, null));

Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "SomeNamespace", true) },
new AssemblyName("abcd")));

Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "SomeNamespace", true) },
new AssemblyName("abcd"),
new[] { new CustomAttributeBuilder(typeof(AssemblyCompanyAttribute).GetConstructor(new[] { typeof(string) }), new[] { "TestCompany" }) }));

Assert.Throws<PlatformNotSupportedException>(() => Regex.CompileToAssembly(
new[] { new RegexCompilationInfo("abcd", RegexOptions.None, "abcd", "SomeNamespace", true) },
new AssemblyName("abcd"),
new[] { new CustomAttributeBuilder(typeof(AssemblyCompanyAttribute).GetConstructor(new[] { typeof(string) }), new[] { "TestCompany" }) },
"resourceFile"));
}
}
}

0 comments on commit 4759212

Please sign in to comment.