@@ -18,6 +18,8 @@ package support
1818
1919import (
2020 "context"
21+ "os"
22+ "path"
2123 "sync"
2224 "testing"
2325
@@ -30,6 +32,7 @@ type Test interface {
3032 T () * testing.T
3133 Ctx () context.Context
3234 Client () Client
35+ OutputDir () string
3336
3437 gomega.Gomega
3538
@@ -70,9 +73,13 @@ type T struct {
7073 * gomega.WithT
7174 t * testing.T
7275 // nolint: containedctx
73- ctx context.Context
74- client Client
75- once sync.Once
76+ ctx context.Context
77+ client Client
78+ outputDir string
79+ once struct {
80+ client sync.Once
81+ outputDir sync.Once
82+ }
7683}
7784
7885func (t * T ) T () * testing.T {
@@ -84,7 +91,8 @@ func (t *T) Ctx() context.Context {
8491}
8592
8693func (t * T ) Client () Client {
87- t .once .Do (func () {
94+ t .T ().Helper ()
95+ t .once .client .Do (func () {
8896 c , err := newTestClient ()
8997 if err != nil {
9098 t .T ().Fatalf ("Error creating client: %v" , err )
@@ -94,6 +102,31 @@ func (t *T) Client() Client {
94102 return t .client
95103}
96104
105+ func (t * T ) OutputDir () string {
106+ t .T ().Helper ()
107+ t .once .outputDir .Do (func () {
108+ if parent , ok := os .LookupEnv (CodeFlareTestOutputDir ); ok {
109+ if ! path .IsAbs (parent ) {
110+ if cwd , err := os .Getwd (); err == nil {
111+ // best effort to output the parent absolute path
112+ parent = path .Join (cwd , parent )
113+ }
114+ }
115+ t .T ().Logf ("Creating output directory in parent directory: %s" , parent )
116+ dir , err := os .MkdirTemp (parent , t .T ().Name ())
117+ if err != nil {
118+ t .T ().Fatalf ("Error creating output directory: %v" , err )
119+ }
120+ t .outputDir = dir
121+ } else {
122+ t .T ().Logf ("Creating ephemeral output directory as %s env variable is unset" , CodeFlareTestOutputDir )
123+ t .outputDir = t .T ().TempDir ()
124+ }
125+ t .T ().Logf ("Output directory has been created at: %s" , t .outputDir )
126+ })
127+ return t .outputDir
128+ }
129+
97130func (t * T ) NewTestNamespace (options ... Option [* corev1.Namespace ]) * corev1.Namespace {
98131 t .T ().Helper ()
99132 namespace := createTestNamespace (t , options ... )
0 commit comments