Skip to content

Commit 108f0b1

Browse files
authored
Import tidy/refactoring (#487)
* refactor: change signature so that the function is the last argument * refactor: tidy * refactor: extract functions * refactor: extract function * lint
1 parent e488dee commit 108f0b1

File tree

6 files changed

+174
-188
lines changed

6 files changed

+174
-188
lines changed

helper/resource/plugin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func runProviderCommandApply(ctx context.Context, t testing.T, wd *plugintest.Wo
120120
fn := func() error {
121121
return wd.Apply(ctx)
122122
}
123-
return runProviderCommand(ctx, t, fn, wd, factories)
123+
return runProviderCommand(ctx, t, wd, factories, fn)
124124
}
125125

126126
func runProviderCommandCreatePlan(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, factories *providerFactories) error {
@@ -129,7 +129,7 @@ func runProviderCommandCreatePlan(ctx context.Context, t testing.T, wd *pluginte
129129
fn := func() error {
130130
return wd.CreatePlan(ctx)
131131
}
132-
return runProviderCommand(ctx, t, fn, wd, factories)
132+
return runProviderCommand(ctx, t, wd, factories, fn)
133133
}
134134

135135
func runProviderCommandGetStateJSON(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, factories *providerFactories) (*tfjson.State, error) {
@@ -141,7 +141,7 @@ func runProviderCommandGetStateJSON(ctx context.Context, t testing.T, wd *plugin
141141
stateJSON, err = wd.State(ctx)
142142
return err
143143
}
144-
err := runProviderCommand(ctx, t, fn, wd, factories)
144+
err := runProviderCommand(ctx, t, wd, factories, fn)
145145
if err != nil {
146146
return nil, err
147147
}
@@ -158,15 +158,15 @@ func runProviderCommandSavedPlan(ctx context.Context, t testing.T, wd *plugintes
158158
plan, err = wd.SavedPlan(ctx)
159159
return err
160160
}
161-
err := runProviderCommand(ctx, t, fn, wd, factories)
161+
err := runProviderCommand(ctx, t, wd, factories, fn)
162162
if err != nil {
163163
return nil, err
164164
}
165165

166166
return plan, nil
167167
}
168168

169-
func runProviderCommand(ctx context.Context, t testing.T, f func() error, wd *plugintest.WorkingDir, factories *providerFactories) error {
169+
func runProviderCommand(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, factories *providerFactories, f func() error) error {
170170
// don't point to this as a test failure location
171171
// point to whatever called it
172172
t.Helper()

helper/resource/plugin_test.go

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -250,44 +250,38 @@ func TestRunProviderCommand(t *testing.T) {
250250
funcCalled := false
251251
helper := plugintest.AutoInitProviderHelper(ctx, currentDir)
252252

253-
err = runProviderCommand(
254-
ctx,
255-
t,
256-
func() error {
257-
funcCalled = true
258-
return nil
259-
},
260-
helper.RequireNewWorkingDir(ctx, t, ""),
261-
&providerFactories{
262-
legacy: map[string]func() (*schema.Provider, error){
263-
"examplecloud": func() (*schema.Provider, error) { //nolint:unparam // required signature
264-
return &schema.Provider{
265-
ResourcesMap: map[string]*schema.Resource{
266-
"examplecloud_thing": {
267-
CreateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
268-
d.SetId("id")
269-
270-
return nil
271-
},
272-
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
273-
return nil
274-
},
275-
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
276-
return nil
277-
},
278-
Schema: map[string]*schema.Schema{
279-
"id": {
280-
Computed: true,
281-
Type: schema.TypeString,
282-
},
253+
err = runProviderCommand(ctx, t, helper.RequireNewWorkingDir(ctx, t, ""), &providerFactories{
254+
legacy: map[string]func() (*schema.Provider, error){
255+
"examplecloud": func() (*schema.Provider, error) { //nolint:unparam // required signature
256+
return &schema.Provider{
257+
ResourcesMap: map[string]*schema.Resource{
258+
"examplecloud_thing": {
259+
CreateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
260+
d.SetId("id")
261+
262+
return nil
263+
},
264+
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
265+
return nil
266+
},
267+
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
268+
return nil
269+
},
270+
Schema: map[string]*schema.Schema{
271+
"id": {
272+
Computed: true,
273+
Type: schema.TypeString,
283274
},
284275
},
285276
},
286-
}, nil
287-
},
277+
},
278+
}, nil
288279
},
289280
},
290-
)
281+
}, func() error {
282+
funcCalled = true
283+
return nil
284+
})
291285

292286
if err != nil {
293287
t.Fatal(err)

helper/resource/testing_new.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
func runPostTestDestroy(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, providers *providerFactories, statePreDestroy *terraform.State) error {
2828
t.Helper()
2929

30-
err := runProviderCommand(ctx, t, func() error {
30+
err := runProviderCommand(ctx, t, wd, providers, func() error {
3131
return wd.Destroy(ctx)
32-
}, wd, providers)
32+
})
3333
if err != nil {
3434
return err
3535
}
@@ -67,13 +67,13 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
6767

6868
var statePreDestroy *terraform.State
6969
var err error
70-
err = runProviderCommand(ctx, t, func() error {
70+
err = runProviderCommand(ctx, t, wd, providers, func() error {
7171
_, statePreDestroy, err = getState(ctx, t, wd)
7272
if err != nil {
7373
return err
7474
}
7575
return nil
76-
}, wd, providers)
76+
})
7777
if err != nil {
7878
logging.HelperResourceError(ctx,
7979
"Error retrieving state, there may be dangling resources",
@@ -116,9 +116,9 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
116116
t.Fatalf("TestCase error setting provider configuration: %s", err)
117117
}
118118

119-
err = runProviderCommand(ctx, t, func() error {
119+
err = runProviderCommand(ctx, t, wd, providers, func() error {
120120
return wd.Init(ctx)
121-
}, wd, providers)
121+
})
122122

123123
if err != nil {
124124
logging.HelperResourceError(ctx,
@@ -264,15 +264,9 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
264264
t.Fatalf("TestStep %d/%d error setting test provider configuration: %s", stepNumber, len(c.Steps), err)
265265
}
266266

267-
err = runProviderCommand(
268-
ctx,
269-
t,
270-
func() error {
271-
return wd.Init(ctx)
272-
},
273-
wd,
274-
providers,
275-
)
267+
err = runProviderCommand(ctx, t, wd, providers, func() error {
268+
return wd.Init(ctx)
269+
})
276270

277271
if err != nil {
278272
logging.HelperResourceError(ctx,
@@ -568,7 +562,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
568562
}()
569563

570564
// Refresh!
571-
err = runProviderCommand(ctx, t, func() error {
565+
err = runProviderCommand(ctx, t, wd, providers, func() error {
572566
err = wd.Refresh(ctx)
573567
if err != nil {
574568
t.Fatalf("Error running terraform refresh: %s", err)
@@ -578,7 +572,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
578572
return err
579573
}
580574
return nil
581-
}, wd, providers)
575+
})
582576
if err != nil {
583577
return err
584578
}

0 commit comments

Comments
 (0)