From 62ab29ac930387af26220cefc09d131fa8532075 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Fri, 18 Oct 2024 15:00:38 +0200 Subject: [PATCH] chore: remove ununsed Playwright.Tests utils (#3026) --- src/Playwright.Tests/Playwright.Tests.csproj | 8 - .../Utils/IdentifyMissingTests.cs | 139 -------------- .../Utils/IdentifyMissingTestsOptions.cs | 46 ----- src/Playwright.Tests/Utils/Program.cs | 37 ---- src/Playwright.Tests/Utils/ScaffoldTest.cs | 173 ------------------ .../Utils/ScaffoldTestOptions.cs | 43 ----- 6 files changed, 446 deletions(-) delete mode 100644 src/Playwright.Tests/Utils/IdentifyMissingTests.cs delete mode 100644 src/Playwright.Tests/Utils/IdentifyMissingTestsOptions.cs delete mode 100644 src/Playwright.Tests/Utils/Program.cs delete mode 100644 src/Playwright.Tests/Utils/ScaffoldTest.cs delete mode 100644 src/Playwright.Tests/Utils/ScaffoldTestOptions.cs diff --git a/src/Playwright.Tests/Playwright.Tests.csproj b/src/Playwright.Tests/Playwright.Tests.csproj index b744244c25..9637a94e42 100644 --- a/src/Playwright.Tests/Playwright.Tests.csproj +++ b/src/Playwright.Tests/Playwright.Tests.csproj @@ -8,13 +8,11 @@ 1701;1702 Microsoft.Playwright.Tests Microsoft.Playwright.Tests - false enable - runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -26,13 +24,7 @@ - - - - - - diff --git a/src/Playwright.Tests/Utils/IdentifyMissingTests.cs b/src/Playwright.Tests/Utils/IdentifyMissingTests.cs deleted file mode 100644 index da632c1cf7..0000000000 --- a/src/Playwright.Tests/Utils/IdentifyMissingTests.cs +++ /dev/null @@ -1,139 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System.Reflection; - -namespace Microsoft.Playwright.Tests; - -/// -/// This will identify missing tests from upstream. -/// -internal static class IdentifyMissingTests -{ - private static readonly List<(string FileName, string TestName)> _testPairs = new(); - - /// - /// Runs the scenario. - /// - /// The options argument. - public static void Run(IdentifyMissingTestsOptions options) - { - // get all files that match a pattern - var directoryInfo = new DirectoryInfo(options.SpecFileLocations); - if (!directoryInfo.Exists) - { - throw new ArgumentException($"The location ({directoryInfo.FullName}) specified does not exist."); - } - - // let's map the test cases from the spec files - MapTestsCases(directoryInfo, options, string.Empty); - - // now, let's load the DLL and use some reflection-fu - var assembly = Assembly.LoadFrom(options.TestsAssemblyPath); - - var attributes = assembly.DefinedTypes.SelectMany( - type => type.GetMethods().SelectMany(method => method.GetCustomAttributes())); - - int potentialMatches = 0; - int fullMatches = 0; - int noMatches = 0; - int totalTests = 0; - - List<(string FileName, string TestName)> missingTests = new(); - List>> invalidMaps = new(); - foreach (var atx in attributes) - { - totalTests++; - - // a test can either be a full match, a partial (i.e. just the test name) or no match - var potentialMatch = _testPairs.Where(x => string.Equals(x.TestName, atx.TestName, StringComparison.InvariantCultureIgnoreCase)); - if (!potentialMatch.Any()) - { - noMatches++; - missingTests.Add((atx.FileName, atx.TestName)); - } - else if (potentialMatch.Any(x => string.Equals(x.FileName, atx.TrimmedName, StringComparison.InvariantCultureIgnoreCase))) - { - fullMatches++; - continue; - } - else - { - invalidMaps.Add(new KeyValuePair<(string, string), List<(string, string)>>((atx.TrimmedName, atx.TestName), potentialMatch.ToList())); - potentialMatches++; - } - } - - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine($"Total matching tests: {fullMatches}/{totalTests}."); - Console.ResetColor(); - - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"Total tests found by name, but not by file: {potentialMatches}/{totalTests}."); - Console.ResetColor(); - - foreach (var invalidTest in invalidMaps) - { - Console.WriteLine($"{invalidTest.Key.FileName}: {invalidTest.Key.TestName}"); - foreach (var (fileName, testName) in invalidTest.Value) - { - Console.WriteLine($"\t{fileName}: {testName}"); - } - } - - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine($"Total missing tests: {noMatches}/{totalTests}."); - Console.ResetColor(); - - foreach (var invalidTest in missingTests) - { - Console.WriteLine($"{invalidTest.FileName}: {invalidTest.TestName}"); - } - - Console.WriteLine($"Found/Mismatched/Missing: {fullMatches}/{potentialMatches}/{noMatches} out of {totalTests}"); - } - - private static void MapTestsCases(DirectoryInfo directoryInfo, IdentifyMissingTestsOptions options, string basePath) - { - // get the sub-directories - if (options.Recursive) - { - foreach (var subdirectory in directoryInfo.GetDirectories()) - { - MapTestsCases(subdirectory, options, $"{basePath}{subdirectory.Name}/"); - } - } - - foreach (var fileInfo in directoryInfo.GetFiles(options.Pattern)) - { - ScaffoldTest.FindTestsInFile( - fileInfo.FullName, - (testName) => - { - _testPairs.Add(new(basePath + fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')), testName)); - }); - } - } -} - diff --git a/src/Playwright.Tests/Utils/IdentifyMissingTestsOptions.cs b/src/Playwright.Tests/Utils/IdentifyMissingTestsOptions.cs deleted file mode 100644 index d0acba05f0..0000000000 --- a/src/Playwright.Tests/Utils/IdentifyMissingTestsOptions.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using CommandLine; - -namespace Microsoft.Playwright.Tests; - -/// -/// Describes the options for scaffolding the tests. -/// -[Verb("missing-tests", HelpText = "Checks if there are missing tests in the C# variant, compared to the specs.")] -internal class IdentifyMissingTestsOptions -{ - [Option(Required = true, HelpText = "Location of the PlaywrightSharp.Tests assembly.")] - public string TestsAssemblyPath { get; set; } - - [Option(Required = true, HelpText = "Location of spec files.")] - public string SpecFileLocations { get; set; } - - [Option(Required = false, HelpText = "The search pattern to use for spec files.", Default = "*.spec.ts")] - public string Pattern { get; set; } - - [Option(Required = false, Default = true, HelpText = "When True, looks inside subdirectories of specified location as well.")] - public bool Recursive { get; set; } -} diff --git a/src/Playwright.Tests/Utils/Program.cs b/src/Playwright.Tests/Utils/Program.cs deleted file mode 100644 index ce4315223d..0000000000 --- a/src/Playwright.Tests/Utils/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using CommandLine; - -namespace Microsoft.Playwright.Tests; - -public static class Program -{ - public static void Main(string[] args) - { - ParserResult result = Parser.Default.ParseArguments(args); - result.WithParsed(ScaffoldTest.Run); - result.WithParsed(IdentifyMissingTests.Run); - } -} diff --git a/src/Playwright.Tests/Utils/ScaffoldTest.cs b/src/Playwright.Tests/Utils/ScaffoldTest.cs deleted file mode 100644 index 7db1d3dbda..0000000000 --- a/src/Playwright.Tests/Utils/ScaffoldTest.cs +++ /dev/null @@ -1,173 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System.CodeDom; -using System.CodeDom.Compiler; -using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Text.RegularExpressions; - -namespace Microsoft.Playwright.Tests; - -[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1118:ParameterMustNotSpanMultipleLines", Justification = "CodeDom is complicated.")] -[SuppressMessage("Microsoft.CodeQuality.Analyzers", "CA1416", Justification = "Validate platform compatibility (we only run tests on win/mac/linux).")] -internal static partial class ScaffoldTest -{ - private static readonly TextInfo _textInfo = CultureInfo.InvariantCulture.TextInfo; - - public static void FindTestsInFile(string path, Action callback) - { - var rx = new Regex(@"it\('(.*)',"); - foreach (string line in File.ReadAllLines(path)) - { - var m = rx.Match(line); - if (m?.Success == false) - { - continue; - } - - // keep in mind, group 0 is the entire match, but - // first (and only group), should give us the describe value - callback(m.Groups[1].Value); - } - } - - /// - /// Generates a clean name from the test name. - /// - /// The original test name. - /// Returns a "clean" string, suitable for C# method names. - public static string CleanName(string testDescribe) - => new(Array.FindAll(_textInfo.ToTitleCase(testDescribe).ToCharArray(), c => char.IsLetterOrDigit(c))); - - public static void Run(ScaffoldTestOptions options) - { - if (!File.Exists(options.SpecFile)) - { - throw new FileNotFoundException(); - } - - var fileInfo = new FileInfo(options.SpecFile); - - int dotSeparator = fileInfo.Name.IndexOf('.'); - string name = _textInfo.ToTitleCase(fileInfo.Name.Substring(0, dotSeparator)) + "Tests"; - var targetClass = GenerateClass(options.Namespace, name, fileInfo.Name); - - FindTestsInFile(options.SpecFile, (name) => AddTest(targetClass, name, fileInfo.Name)); - - using CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); - CodeGeneratorOptions codegenOptions = new CodeGeneratorOptions() - { - BracingStyle = "C", - }; - - using StreamWriter sourceWriter = new StreamWriter(options.OutputFile); - provider.GenerateCodeFromCompileUnit( - targetClass, sourceWriter, codegenOptions); - } - - private static CodeCompileUnit GenerateClass(string @namespace, string @class, string fileOrigin) - { - var targetUnit = new CodeCompileUnit(); - var globalNamespace = new CodeNamespace(); - - // add imports - globalNamespace.Imports.Add(new("System.Threading.Tasks")); - globalNamespace.Imports.Add(new("Microsoft.Playwright.Tests.BaseTests")); - globalNamespace.Imports.Add(new("Xunit")); - globalNamespace.Imports.Add(new("Xunit.Abstractions")); - - targetUnit.Namespaces.Add(globalNamespace); - - var codeNamespace = new CodeNamespace(@namespace); - var targetClass = new CodeTypeDeclaration(@class) - { - IsClass = true, - TypeAttributes = System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed, - }; - - targetClass.BaseTypes.Add(new CodeTypeReference("MicrosoftPlaywrightPageBaseTest")); - - _ = targetClass.CustomAttributes.Add(new( - "Collection", - new CodeAttributeArgument[] - { - new( - new CodeFieldReferenceExpression( - new CodeTypeReferenceExpression("TestConstants"), - "TestFixtureBrowserCollectionName")), - })); - - targetClass.Comments.Add(new($"{fileOrigin}", true)); - codeNamespace.Types.Add(targetClass); - - targetUnit.Namespaces.Add(codeNamespace); - - // add constructor - var constructor = new CodeConstructor() - { - Attributes = MemberAttributes.Public, - }; - - constructor.Parameters.Add(new("ITestOutputHelper", "output")); - constructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("output")); - constructor.Comments.Add(new("", true)); - targetClass.Members.Add(constructor); - - return targetUnit; - } - - private static void AddTest(CodeCompileUnit @class, string testDescribe, string testOrigin) - { - // make name out of the describe, and we should ignore any whitespaces, hyphens, etc. - string name = CleanName(testDescribe); - - Console.WriteLine($"Adding {name}"); - - CodeMemberMethod method = new CodeMemberMethod() - { - Attributes = MemberAttributes.Public | MemberAttributes.Final, - ReturnType = new("async Task"), - Name = name, - }; - - @class.Namespaces[1].Types[0].Members.Add(method); - - method.Comments.Add(new($"{testOrigin}", true)); - method.Comments.Add(new($"{testDescribe}", true)); - method.CustomAttributes.Add(new( - "Fact", - new CodeAttributeArgument[] - { - new( - "Timeout", - new CodeFieldReferenceExpression( - new CodeTypeReferenceExpression("Microsoft.Playwright"), - "DefaultTimeout")), - new( - "Skip", - new CodePrimitiveExpression("This test is not yet implemented.")), - })); - } -} diff --git a/src/Playwright.Tests/Utils/ScaffoldTestOptions.cs b/src/Playwright.Tests/Utils/ScaffoldTestOptions.cs deleted file mode 100644 index 9524e4f097..0000000000 --- a/src/Playwright.Tests/Utils/ScaffoldTestOptions.cs +++ /dev/null @@ -1,43 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using CommandLine; - -namespace Microsoft.Playwright.Tests; - -/// -/// Describes the options for scaffolding the tests. -/// -[Verb("scaffold-test", HelpText = "Takes a spec.ts file and scaffolds the C# test.")] -internal class ScaffoldTestOptions -{ - [Option(Required = true, HelpText = "Name of the spec file to use.")] - public string SpecFile { get; set; } - - [Option(Required = false, HelpText = "The location of the scaffold code. If not present, will output to console.")] - public string OutputFile { get; set; } - - [Option(Required = false, HelpText = "The namespace of the generated class.", Default = "Microsoft.Playwright.Tests")] - public string Namespace { get; set; } -}