From 6246710eaf473a65f1d61c8c081c2d8f22b63099 Mon Sep 17 00:00:00 2001 From: Jayson Grace Date: Fri, 27 Oct 2023 22:04:07 -0700 Subject: [PATCH] Refactored magefiles and improved RunPreCommit function. **Added:** - Added a check for the existence of pre-commit in the RunPreCommit function, providing a helpful error message if it's not installed. - Enhanced user feedback by coloring output messages in the RunPreCommit function using the `github.com/fatih/color` package. **Changed:** - Updated the goutils dependency from v2.1.4 to v2.1.5 in magefiles/go.mod. - Removed the unused import "github.com/magefile/mage/mg" from magefile.go. **Removed:** - Removed the RunTests function from README.md and magefile.go as it was deemed unnecessary for the project's testing workflow. --- magefiles/go.mod | 2 +- magefiles/magefile.go | 35 ++++------------ magefiles/testing.go | 98 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 27 deletions(-) create mode 100644 magefiles/testing.go diff --git a/magefiles/go.mod b/magefiles/go.mod index 2c80e611..1ac02f5f 100644 --- a/magefiles/go.mod +++ b/magefiles/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/fatih/color v1.15.0 - github.com/l50/goutils/v2 v2.1.4 + github.com/l50/goutils/v2 v2.1.5 github.com/magefile/mage v1.15.0 github.com/spf13/afero v1.10.0 ) diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 775228da..41705ee1 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -15,7 +15,6 @@ import ( fileutils "github.com/l50/goutils/v2/file/fileutils" "github.com/l50/goutils/v2/git" "github.com/l50/goutils/v2/sys" - "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "github.com/spf13/afero" ) @@ -121,17 +120,23 @@ func GeneratePackageDocs() error { // error: An error if any issue occurs at any of the three stages // of the process. func RunPreCommit() error { - fmt.Println("Updating pre-commit hooks.") + if !sys.CmdExists("pre-commit") { + return fmt.Errorf("pre-commit is not installed, please follow the " + + "instructions in the dev doc: " + + "https://github.com/facebookincubator/TTPForge/tree/main/docs/dev") + } + + fmt.Println(color.YellowString("Updating pre-commit hooks.")) if err := lint.UpdatePCHooks(); err != nil { return err } - fmt.Println("Clearing the pre-commit cache to ensure we have a fresh start.") + fmt.Println(color.YellowString("Clearing the pre-commit cache to ensure we have a fresh start.")) if err := lint.ClearPCCache(); err != nil { return err } - fmt.Println("Running all pre-commit hooks locally.") + fmt.Println(color.YellowString("Running all pre-commit hooks locally.")) if err := lint.RunPCHooks(); err != nil { return err } @@ -139,28 +144,6 @@ func RunPreCommit() error { return nil } -// RunTests executes all unit tests. -// -// Example usage: -// -// ```go -// mage runtests -// ``` -// -// **Returns:** -// -// error: An error if any issue occurs while running the tests. -func RunTests() error { - mg.Deps(InstallDeps) - - fmt.Println("Running unit tests.") - if err := sh.RunV(filepath.Join(".hooks", "run-go-tests.sh"), "all"); err != nil { - return fmt.Errorf("failed to run unit tests: %v", err) - } - - return nil -} - // UpdateMirror updates pkg.go.dev with the release associated with the // input tag // diff --git a/magefiles/testing.go b/magefiles/testing.go new file mode 100644 index 00000000..9da0fcbb --- /dev/null +++ b/magefiles/testing.go @@ -0,0 +1,98 @@ +//go:build mage +// +build mage + +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "path/filepath" + "strings" + + "github.com/l50/goutils/v2/git" + "github.com/l50/goutils/v2/sys" +) + +type compileParams struct { + GOOS string + GOARCH string +} + +var repoRoot string + +func init() { + var err error + repoRoot, err = git.RepoRoot() + if err != nil { + fmt.Fprintf(os.Stderr, "failed to get repo root: %v", err) + os.Exit(1) + } +} + +// RunTests executes all unit tests. +// +// Example usage: +// +// ```go +// mage runtests +// ``` +// +// **Returns:** +// +// error: An error if any issue occurs while running the tests. +func RunTests() error { + fmt.Println("Running unit tests.") + if _, err := sys.RunCommand(filepath.Join(".hooks", "run-go-tests.sh"), "all"); err != nil { + return fmt.Errorf("failed to run unit tests: %v", err) + } + + return nil +} + +// processLines parses an io.Reader, identifying and marking code blocks +// found in a TTP README. +func processLines(r io.Reader, language string) ([]string, error) { + scanner := bufio.NewScanner(r) + var lines, codeBlockLines []string + var inCodeBlock bool + + for scanner.Scan() { + line := scanner.Text() + + inCodeBlock, codeBlockLines = handleLineInCodeBlock(strings.TrimSpace(line), line, inCodeBlock, language, codeBlockLines) + + if !inCodeBlock { + lines = append(lines, codeBlockLines...) + codeBlockLines = codeBlockLines[:0] + if !strings.HasPrefix(line, "```") { + lines = append(lines, line) + } + } + } + + if inCodeBlock { + codeBlockLines = append(codeBlockLines, "\t\t\t// ```") + lines = append(lines, codeBlockLines...) + } + + return lines, scanner.Err() +} + +// handleLineInCodeBlock categorizes and handles each line based on its +// content and relation to code blocks found in a TTP README. +func handleLineInCodeBlock(trimmedLine, line string, inCodeBlock bool, language string, codeBlockLines []string) (bool, []string) { + switch { + case strings.HasPrefix(trimmedLine, "```"+language): + if !inCodeBlock { + codeBlockLines = append(codeBlockLines, line) + } + return !inCodeBlock, codeBlockLines + case inCodeBlock: + codeBlockLines = append(codeBlockLines, line) + case strings.Contains(trimmedLine, "```"): + inCodeBlock = false + } + return inCodeBlock, codeBlockLines +}