-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
108 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |