Skip to content

Commit

Permalink
chore: Add filter flag to TestScript
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI committed Nov 9, 2024
1 parent 3bbc077 commit 9fe2cad
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion internal/cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
_ "embed"
"errors"
"flag"
"fmt"
"io/fs"
"net"
Expand Down Expand Up @@ -37,6 +38,8 @@ var (
lookupRx = regexp.MustCompile(`\Alookup:(.*)\z`)
umaskConditionRx = regexp.MustCompile(`\Aumask:([0-7]{3})\z`)

filterRegex string

//go:embed mockcommand.tmpl
mockcommandTmplText string

Expand All @@ -45,6 +48,8 @@ var (
)

func TestMain(m *testing.M) {
flag.StringVar(&filterRegex, "filter", "", "regex to filter test scripts")
flag.Parse()
os.Exit(testscript.RunMain(m, map[string]func() int{
"chezmoi": func() int {
return cmd.Main(cmd.VersionInfo{
Expand All @@ -58,8 +63,32 @@ func TestMain(m *testing.M) {
}

func TestScript(t *testing.T) {
if testing.Short() {
t.Skip("skipping testscript tests in short mode")
}
files, err := filepath.Glob("testdata/scripts/*.txtar")
if err != nil {
t.Fatalf("failed to glob files: %v", err)
}
if filterRegex != "" {
re, err := regexp.Compile(filterRegex)
if err != nil {
t.Fatalf("invalid regex %q: %v", filterRegex, err)
}
var filteredFiles []string
for _, f := range files {
baseName := filepath.Base(f)
if re.MatchString(baseName) {
filteredFiles = append(filteredFiles, f)
}
}
files = filteredFiles
if len(files) == 0 {
t.Fatalf("no test scripts match regex %q", filterRegex)
}
}
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Files: files,
Cmds: map[string]func(*testscript.TestScript, bool, []string){
"appendline": cmdAppendLine,
"chhome": cmdChHome,
Expand Down

0 comments on commit 9fe2cad

Please sign in to comment.