Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"regexp"

"go.followtheprocess.codes/hue"
)

// Option is a functional option for configuring a snapshot test [Runner].
Expand Down Expand Up @@ -54,9 +56,7 @@ func Description(description string) Option {
// Passing this option will override default detection and set the provided value.
func Color(enabled bool) Option {
return func(r *Runner) error {
// noColor rather than color so that the default value is false
// which falls back to hue's autodetection
r.noColor = !enabled
hue.Enabled(enabled)
return nil
}
}
Expand Down
9 changes: 2 additions & 7 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type Runner struct {
filters []filter
update bool
clean bool
noColor bool
}

// New initialises a new snapshot test [Runner].
Expand Down Expand Up @@ -146,7 +145,7 @@ func (r Runner) Snap(value any) {
old = bytes.ReplaceAll(old, []byte("\r\n"), []byte("\n"))

if diff := diff.Diff("old", old, "new", content); diff != nil {
r.tb.Fatalf("\nMismatch\n--------\n%s\n", prettyDiff(string(diff), r.noColor))
r.tb.Fatalf("\nMismatch\n--------\n%s\n", prettyDiff(string(diff)))
}
}

Expand Down Expand Up @@ -186,11 +185,7 @@ func fileExists(path string) (bool, error) {
// prettyDiff takes a string diff in unified diff format and colourises it for easier viewing.
//
// if noColor is true, the original diff is returned unchanged.
func prettyDiff(diff string, noColor bool) string {
if noColor {
return diff
}

func prettyDiff(diff string) string {
lines := strings.Split(diff, "\n")
for i := range lines {
trimmed := strings.TrimSpace(lines[i])
Expand Down
Loading