Skip to content

Commit

Permalink
internal/cuetxtar: allow inline archives
Browse files Browse the repository at this point in the history
It is often handy allow testing of an isolated test
for debugging purposes. This change allows any
txtar test to be turned into such a test. This avoids
having to write a separate TestX-like test, like we have
now. TestX-style may deviate a little bit from a txtar-
based test, making it hard to track down bugs.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I707197788c4be941a317145e107aff9da6605794
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194650
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed May 13, 2024
1 parent 22b8c74 commit 645d586
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/cuetxtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type TxTarTest struct {
// If Matrix is non-nil, the tests are run for each configuration in the
// matrix.
Matrix cuetdtest.Matrix

// DebugArchive, if set, is loaded instead of the on-disk archive. This allows
// a test to be used for debugging.
DebugArchive string
}

// A Test represents a single test based on a .txtar file.
Expand Down Expand Up @@ -358,6 +362,31 @@ func (t *Test) Context() *cue.Context {
func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
t.Helper()

if x.DebugArchive != "" {
archive := txtar.Parse([]byte(x.DebugArchive))

t.Run("", func(t *testing.T) {
tc := &Test{
T: t,
Archive: archive,
Dir: "/tmp",

prefix: path.Join("out", x.Name),
LoadConfig: x.LoadConfig,
}
// Don't let Env be nil, as the tests shouldn't depend on os.Environ.
if tc.LoadConfig.Env == nil {
tc.LoadConfig.Env = []string{}
}

f(tc)

// Unconditionally output as an error.
t.Error(tc.buf.String())
})
return
}

dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 645d586

Please sign in to comment.