Skip to content

Commit

Permalink
Refactored magefiles and improved RunPreCommit function.
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
l50 committed Oct 28, 2023
1 parent 99dc00b commit 6246710
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 27 deletions.
2 changes: 1 addition & 1 deletion magefiles/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
35 changes: 9 additions & 26 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -121,46 +120,30 @@ 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
}

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
//
Expand Down
98 changes: 98 additions & 0 deletions magefiles/testing.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6246710

Please sign in to comment.