private static void Setup(out string startDirectory, out DirectoryInfo testDir, out FileInfo[] testAssemblies) { string reportHost = Arguments["testReportHost"]; if (string.IsNullOrEmpty(reportHost)) { reportHost = DefaultConfiguration.GetAppSetting("TestReportHost", string.Empty); } if (!string.IsNullOrEmpty(reportHost)) { if (!int.TryParse(Arguments["testReportPort"].Or(DefaultConfiguration.GetAppSetting("TestReportPort", string.Empty)).Or("80"), out int port)) { port = 80; } UnitTestRunListeners.Add(new UnitTestRunReportingListener(reportHost, port)); } GetUnitTestRunListeners = () => UnitTestRunListeners; startDirectory = Environment.CurrentDirectory; testDir = GetTestDirectory(); Environment.CurrentDirectory = testDir.FullName; testAssemblies = GetTestAssemblies(testDir); }
public static void RunTestsForRecipe() { string recipePath = GetArgument("Recipe", "Please enter the path to the recipe file to test"); if (string.IsNullOrEmpty(recipePath)) { recipePath = "./recipe.json"; } if (!File.Exists(recipePath)) { Message.PrintLine("Recipe not found: {0}\r\nSpecify /Recipe:[path_to_bake_recipe_dot_json]", ConsoleColor.Magenta, recipePath); Exit(1); } else { Message.PrintLine("Running tests for recipe: {0}", ConsoleColor.Cyan, recipePath); } Recipe recipe = recipePath.FromJsonFile <Recipe>(); string testGroupName = Arguments["Group"]; string searchPattern = GetArgumentOrDefault("search", "*tests.*"); HashSet <string> projectsSpecified = new HashSet <string>(); HashSet <string> projectsRun = new HashSet <string>(); if (Arguments.Contains("projects")) { projectsSpecified = new HashSet <string>(Arguments["projects"].DelimitSplit(new[] { "," }, true).ToArray()); Message.PrintLine("Limiting test runs to the specified projects: {0}\r\n\t", string.Join("\r\n\t", projectsSpecified)); } string returnToDirectory = Environment.CurrentDirectory; BamUnitTestRunListener testListener = new BamUnitTestRunListener(Arguments["results"].Or("./BamTests")); UnitTestRunListeners.Clear(); UnitTestRunListeners.Add(testListener); HashSet <string> assemblyHashes = new HashSet <string>(); foreach (string projectFilePath in recipe.ProjectFilePaths) { FileInfo projectFile = new FileInfo(projectFilePath); string projectName = Path.GetFileNameWithoutExtension(projectFile.Name); projectsRun.Add(projectName); if (projectsSpecified.Count > 0 && !projectsSpecified.Contains(projectName)) { Message.PrintLine("Skipping project in recipe [{0}]({1})...", ConsoleColor.Blue, projectName, projectFilePath); continue; } else { Message.PrintLine("Running tests for project [{0}]({1})", ConsoleColor.DarkBlue, projectName, projectFilePath); } string testDirectoryPath = Path.Combine(recipe.OutputDirectory, projectName); DirectoryInfo testDirectory = new DirectoryInfo(testDirectoryPath); if (!testDirectory.Exists) { Message.PrintLine("Test directory not found: {0}", ConsoleColor.Yellow, testDirectory.FullName); continue; } Environment.CurrentDirectory = testDirectoryPath; if (!string.IsNullOrEmpty(testGroupName)) { Message.PrintLine("Running test group [{0}]", ConsoleColor.DarkGreen, testGroupName); RunUnitTestGroupsInFolder(testDirectoryPath, searchPattern, testGroupName); } else { FileInfo[] testAssemblies = GetDllsAndExes(testDirectory.GetFiles(searchPattern)).ToArray(); if (testAssemblies.Length == 0) { // Didn't find tests using the specified search pattern // checking to see if there is a test dll for the current project string testDll = Path.Combine(testDirectoryPath, $"{projectName}.dll"); Message.PrintLine("No test assemblies found (/search:{0}), checking for project assembly {1}", ConsoleColor.Yellow, searchPattern, testDll); if (File.Exists(testDll)) { Message.PrintLine("Project assembly found {0}", ConsoleColor.Cyan, testDll); string testCommand = $"dotnet {testDll} /t"; Message.PrintLine("Running {0}", ConsoleColor.DarkCyan, testCommand); testCommand.Run(msg => OutLine(msg, ConsoleColor.DarkCyan)); } else { Message.PrintLine("Project assembly NOT found {0}", ConsoleColor.Yellow, testDll); } } else { foreach (FileInfo testAssembly in testAssemblies) { string assemblyHash = testAssembly.ContentHash(HashAlgorithms.SHA1); if (!assemblyHashes.Contains(assemblyHash)) { assemblyHashes.Add(assemblyHash); Message.PrintLine("Running tests in {0}", ConsoleColor.DarkBlue, testAssembly.FullName); RunUnitTestsInFile(testAssembly.FullName, testDirectory.FullName); } else { Message.PrintLine("Tests in {0} already run", ConsoleColor.DarkGreen, testAssembly.FullName); } } } } } foreach (string project in projectsSpecified) { if (!projectsRun.Contains(project)) { Message.PrintLine("Project specified on command line was not in recipe: {0}", ConsoleColor.Yellow, project); } } Environment.CurrentDirectory = returnToDirectory; if (testListener.FailuresOccurred) { testListener.LogSummary(); Exit(1); } }