cmd

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AggregateResultsCmd = &cobra.Command{
	Use:   "aggregate-results",
	Short: "Aggregate test results and optionally filter failed tests based on a threshold",
	Run: func(cmd *cobra.Command, args []string) {

		allResults, err := reports.AggregateTestResults(resultsFolderPath)
		if err != nil {
			log.Fatalf("Error aggregating results: %v", err)
		}

		var resultsToSave []reports.TestResult

		if filterFailed {

			for _, result := range allResults {
				if result.PassRatio < maxPassRatio && !result.Skipped {
					resultsToSave = append(resultsToSave, result)
				}
			}
		} else {
			resultsToSave = allResults
		}

		if len(resultsToSave) > 0 {
			reports.SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath, resultsToSave)
		}
	},
}
View Source
var FindTestsCmd = &cobra.Command{
	Use:   "find",
	Long:  "Analyzes Golang project repository for changed files against a specified base reference and determines the test packages that are potentially impacted",
	Short: "Find test packages that may be affected by changes",
	Run: func(cmd *cobra.Command, args []string) {
		projectPath, _ := cmd.Flags().GetString("project-path")
		verbose, _ := cmd.Flags().GetBool("verbose")
		jsonOutput, _ := cmd.Flags().GetBool("json")
		filterEmptyTests, _ := cmd.Flags().GetBool("filter-empty-tests")
		baseRef, _ := cmd.Flags().GetString("base-ref")
		excludes, _ := cmd.Flags().GetStringSlice("excludes")
		levels, _ := cmd.Flags().GetInt("levels")
		findByTestFilesDiff, _ := cmd.Flags().GetBool("find-by-test-files-diff")
		findByAffected, _ := cmd.Flags().GetBool("find-by-affected-packages")
		onlyShowChangedTestFiles, _ := cmd.Flags().GetBool("only-show-changed-test-files")

		// Find all changes in test files and get their package names
		var changedTestPkgs []string
		if findByTestFilesDiff {
			changedTestFiles, err := git.FindChangedFiles(projectPath, baseRef, "grep '_test\\.go$'")
			if err != nil {
				log.Fatalf("Error finding changed test files: %v", err)
			}
			if onlyShowChangedTestFiles {
				outputResults(changedTestFiles, jsonOutput)
				return
			}
			if verbose {
				fmt.Println("Changed test files:", changedTestFiles)
			}
			changedTestPkgs, err = golang.GetFilePackages(changedTestFiles)
			if err != nil {
				log.Fatalf("Error getting package names for test files: %v", err)
			}
		}

		// Find all affected test packages
		var affectedTestPkgs []string
		if findByAffected {
			if verbose {
				fmt.Println("Finding affected packages...")
			}
			affectedTestPkgs = findAffectedPackages(baseRef, projectPath, excludes, levels)
		}

		testPkgs := append(changedTestPkgs, affectedTestPkgs...)
		testPkgs = utils.Deduplicate(testPkgs)

		if filterEmptyTests {
			if verbose {
				fmt.Println("Filtering packages without tests...")
			}
			testPkgs = golang.FilterPackagesWithTests(testPkgs)
		}

		outputResults(testPkgs, jsonOutput)
	},
}
View Source
var RunTestsCmd = &cobra.Command{
	Use:   "run",
	Short: "Run tests to check if they are flaky",
	Run: func(cmd *cobra.Command, args []string) {
		projectPath, _ := cmd.Flags().GetString("project-path")
		testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
		testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
		runCount, _ := cmd.Flags().GetInt("run-count")
		useRace, _ := cmd.Flags().GetBool("race")
		outputPath, _ := cmd.Flags().GetString("output-json")
		maxPassRatio, _ := cmd.Flags().GetFloat64("max-pass-ratio")
		skipTests, _ := cmd.Flags().GetStringSlice("skip-tests")
		printFailedTests, _ := cmd.Flags().GetBool("print-failed-tests")

		if err := checkDependencies(projectPath); err != nil {
			log.Fatalf("Error: %v", err)
		}

		var testPackages []string
		if testPackagesJson != "" {
			if err := json.Unmarshal([]byte(testPackagesJson), &testPackages); err != nil {
				log.Fatalf("Error decoding test packages JSON: %v", err)
			}
		} else if len(testPackagesArg) > 0 {
			testPackages = testPackagesArg
		} else {
			log.Fatalf("Error: must specify either --test-packages-json or --test-packages")
		}

		runner := runner.Runner{
			ProjectPath:          projectPath,
			Verbose:              true,
			RunCount:             runCount,
			UseRace:              useRace,
			SkipTests:            skipTests,
			SelectedTestPackages: testPackages,
		}

		testResults, err := runner.RunTests()
		if err != nil {
			fmt.Printf("Error running tests: %v\n", err)
			os.Exit(1)
		}

		passedTests := reports.FilterPassedTests(testResults, maxPassRatio)
		failedTests := reports.FilterFailedTests(testResults, maxPassRatio)
		skippedTests := reports.FilterSkippedTests(testResults)
		flakyTests := reports.FilterFlakyTests(testResults, maxPassRatio)

		if len(failedTests) > 0 && printFailedTests {
			fmt.Printf("Maximum threshold for flaky tests: %.2f\n", maxPassRatio)
			fmt.Printf("PassRatio threshold for flaky tests: %.2f\n", maxPassRatio)
			fmt.Printf("%d failed tests:\n", len(failedTests))
			reports.PrintTests(failedTests, os.Stdout)
		}

		fmt.Printf("Summary: %d passed, %d skipped, %d failed, %d flaky\n", len(passedTests), len(skippedTests), len(failedTests), len(flakyTests))

		if outputPath != "" && len(testResults) > 0 {
			jsonData, err := json.MarshalIndent(testResults, "", "  ")
			if err != nil {
				log.Fatalf("Error marshaling test results to JSON: %v", err)
			}
			if err := os.WriteFile(outputPath, jsonData, 0644); err != nil {
				log.Fatalf("Error writing test results to file: %v", err)
			}
			fmt.Printf("All test results saved to %s\n", outputPath)
		}

		if len(flakyTests) > 0 {

			os.Exit(1)
		} else if len(testResults) == 0 {
			fmt.Printf("No tests were run for the specified packages.\n")
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL