Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
TripleDogDare committed Nov 16, 2022
1 parent 8470be5 commit 809162f
Showing 1 changed file with 8 additions and 39 deletions.
47 changes: 8 additions & 39 deletions testexec/testexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ package testexec

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -24,8 +21,6 @@ import (
"github.com/warpfork/go-testmark"
)

var DisableStrict = flag.Bool("testexec.no-strict", false, "Setting this flag will allow unconventional directory structures.")

// ExecFn is the outline for a callback that can run individual commands in a sequence of commands.
// An ExecFn can be placed in the Tester config struct to control the behavior of TestSequence.
//
Expand Down Expand Up @@ -55,18 +50,6 @@ type FilterFn func(line string) (replacement string)
// The default behavior, if a Tester object doesn't get an AssertFn, is to fall back to a very basic call to `t.Errorf`.
type AssertFn func(t *testing.T, actual, expect string)

var (
// SkipRecursion can be returned by a RecursionFn for paths that should show up in the test as skipped
SkipRecursion = errors.New("skip this directory")
// IgnoreRecursion can be returned by a RecursionFn for paths that should not run a new test at all
IgnoreRecursion = errors.New("ignore this directory")
)

// RecursionFn can be used in Tester to specify a recursion pattern
// The default recursion pattern will recurse whenever it finds "then-"
// Returning a non-nil error will cause the test for the directory to fail unless the error is a SkipRecursion or IgnoreRecursion error
type RecursionFn func(*testing.T, testmark.DirEnt) error

// Tester is a configuration-gathering structure.
// Each of the `Test*` methods upon it will use these callbacks to define their behavior.
//
Expand Down Expand Up @@ -99,9 +82,6 @@ func (tcfg *Tester) init() {
if tcfg.AssertFn == nil {
tcfg.AssertFn = defaultAssertFn
}
if *DisableStrict {
tcfg.DisableStrictMode = true
}
}

// TestSequence runs a test based on a "sequence" instruction -- a hunk called "sequence" should have a series of lines,
Expand Down Expand Up @@ -347,18 +327,19 @@ func (tcfg Tester) test(t *testing.T, data testmark.DirEnt, allowExec, allowScri
}

func (tcfg Tester) recurse(t *testing.T, data testmark.DirEnt, allowExec bool, allowScript bool, parentTmpDir string) {
alreadyFailed := t.Failed()
for _, child := range data.ChildrenList {
recurseErr := canRecurse(t, child)
if errors.Is(recurseErr, IgnoreRecursion) {
if _, exists := leafNodeTable[child.Name]; exists {
t.Logf("%s will not recurse into special leaf node %q", t.Name(), child.Name)
continue
}
alreadyFailed := t.Failed()
t.Run(child.Name, func(t *testing.T) {
if recurseErr != nil {
if len(child.Name) <= 5 || !strings.HasPrefix(child.Name, "then-") {
t.Logf("%q does not begin with %q", child.Name, "then-")
if tcfg.DisableStrictMode {
t.Skip(recurseErr)
t.SkipNow()
}
t.Fatal(recurseErr)
t.FailNow()
}
if alreadyFailed {
// This comes after the recursion test because a file structure error should still fail
Expand All @@ -370,7 +351,7 @@ func (tcfg Tester) recurse(t *testing.T, data testmark.DirEnt, allowExec bool, a
}

// Hash Table of all the "special" nodes used by testexec.
var defaultLeafNodes = map[string]struct{}{
var leafNodeTable = map[string]struct{}{
"exitcode": {},
"stderr": {},
"stdout": {},
Expand All @@ -381,18 +362,6 @@ var defaultLeafNodes = map[string]struct{}{
"fs": {},
}

func canRecurse(t *testing.T, dir testmark.DirEnt) error {
t.Helper()
if _, exists := defaultLeafNodes[dir.Name]; exists {
t.Logf("%s will not recurse into special leaf node %q", t.Name(), dir.Name)
return IgnoreRecursion
}
if len(dir.Name) > 5 && strings.HasPrefix(dir.Name, "then-") {
return nil
}
return fmt.Errorf("%q does not begin with %q", dir.Name, "then-")
}

func (tcfg Tester) doSequence(t *testing.T, hunk *testmark.Hunk, stdin io.Reader, stdout, stderr io.Writer) (exitcode int) {
t.Helper()
// Loop over the lines in the sequence.
Expand Down

0 comments on commit 809162f

Please sign in to comment.