From cd7b42d1d9fdf1180c4a684d29a42c8286d719e3 Mon Sep 17 00:00:00 2001 From: Francesco Banconi Date: Wed, 22 Apr 2020 23:11:58 +0200 Subject: [PATCH] testscript: expose current subtest in Env --- testscript/testscript.go | 12 ++++++++++++ testscript/testscript_test.go | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/testscript/testscript.go b/testscript/testscript.go index b440d748..ea0dee0e 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -69,6 +69,18 @@ func (e *Env) Defer(f func()) { e.ts.Defer(f) } +// T returns the t argument passed to the current test by the T.Run method. +// Note that if the tests were started by calling Run, +// the returned value will implement testing.TB. +// Note that, despite that, the underlying value will not be of type +// *testing.T because *testing.T does not implement T. +// +// If Cleanup is called on the returned value, the function will run +// after any functions passed to Env.Defer. +func (e *Env) T() T { + return e.ts.t +} + // Params holds parameters for a call to Run. type Params struct { // Dir holds the name of the directory holding the scripts. diff --git a/testscript/testscript_test.go b/testscript/testscript_test.go index ac97400d..a5db1699 100644 --- a/testscript/testscript_test.go +++ b/testscript/testscript_test.go @@ -113,6 +113,12 @@ func TestScripts(t *testing.T) { if ts.Value("somekey") != 1234 { ts.Fatalf("test-values did not see expected value") } + if ts.Value("t").(T) != ts.t { + ts.Fatalf("test-values did not see expected t") + } + if _, ok := ts.Value("t").(testing.TB); !ok { + ts.Fatalf("test-values t does not implement testing.TB") + } }, "testreadfile": func(ts *TestScript, neg bool, args []string) { if len(args) != 1 { @@ -165,6 +171,7 @@ func TestScripts(t *testing.T) { } env.Values["setupFilenames"] = setupFilenames env.Values["somekey"] = 1234 + env.Values["t"] = env.T() env.Vars = append(env.Vars, "GONOSUMDB=*", )