Skip to content

Commit

Permalink
Tidy up test initialisation
Browse files Browse the repository at this point in the history
- Extract pulumiTestInit method so we're not copying the same code into 3 places.
- Extract testContext method to create context with deadline if required.
  • Loading branch information
danielrbradley committed Jan 18, 2024
1 parent 5c316d7 commit cdb1182
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
7 changes: 1 addition & 6 deletions pulumitest/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func (a *PulumiTest) Convert(language string, opts ...opttest.Option) ConvertRes
source: targetDir,
options: options,
}
if !options.SkipInstall {
convertedTest.Install()
}
if !options.SkipStackCreate {
convertedTest.NewStack(options.StackName, options.NewStackOpts...)
}
pulumiTestInit(convertedTest, options)
return ConvertResult{
PulumiTest: convertedTest,
Output: string(out),
Expand Down
7 changes: 1 addition & 6 deletions pulumitest/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func (a *PulumiTest) CopyTo(dir string, opts ...opttest.Option) *PulumiTest {
source: dir,
options: options,
}
if !options.SkipInstall {
newTest.Install()
}
if !options.SkipStackCreate {
newTest.NewStack(options.StackName, options.NewStackOpts...)
}
pulumiTestInit(newTest, options)
return newTest
}

Expand Down
31 changes: 22 additions & 9 deletions pulumitest/pulumiTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ type PulumiTest struct {
// 3. Create a new stack called "test" with state stored to a local temporary directory and a fixed passphrase for encryption.
func NewPulumiTest(t *testing.T, source string, opts ...opttest.Option) *PulumiTest {
t.Helper()
var ctx context.Context
var cancel context.CancelFunc
if deadline, ok := t.Deadline(); ok {
ctx, cancel = context.WithDeadline(context.Background(), deadline)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
t.Cleanup(cancel)
ctx := testContext(t)
options := opttest.DefaultOptions()
for _, opt := range opts {
opt.Apply(options)
Expand All @@ -43,14 +36,34 @@ func NewPulumiTest(t *testing.T, source string, opts ...opttest.Option) *PulumiT
}
if !options.TestInPlace {
pt = pt.CopyToTempDir()
} else {
pulumiTestInit(pt, options)
}
return pt
}

func testContext(t *testing.T) context.Context {
t.Helper()
var ctx context.Context
var cancel context.CancelFunc
if deadline, ok := t.Deadline(); ok {
ctx, cancel = context.WithDeadline(context.Background(), deadline)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
t.Cleanup(cancel)
return ctx
}

// Perform the common initialization steps for a PulumiTest instance.
func pulumiTestInit(pt *PulumiTest, options *opttest.Options) {
pt.t.Helper()
if !options.SkipInstall {
pt.Install()
}
if !options.SkipStackCreate {
pt.NewStack(options.StackName, options.NewStackOpts...)
}
return pt
}

// Source returns the current source directory.
Expand Down

0 comments on commit cdb1182

Please sign in to comment.