udpate code which was failing staticckeck

Some updates to staticcheck were catching more errors. Most of these
were format functions without a format string, but rather than adding
more exceptions I just fixed them all. This did also catch some
incorrectly wrapped errors.
pull/35697/head
James Bardin 1 year ago
parent 809fe73b84
commit 2cbb6bf92f

@ -115,7 +115,7 @@ func TestParseAbsOutputValueStr(t *testing.T) {
t.Run(input, func(t *testing.T) {
got, diags := ParseAbsOutputValueStr(input)
for _, problem := range deep.Equal(got, tc.want) {
t.Errorf(problem)
t.Error(problem)
}
if len(diags) > 0 {
gotErr := diags.Err().Error()

@ -159,7 +159,7 @@ func TestParseRefInTestingScope(t *testing.T) {
}
for _, problem := range deep.Equal(got, test.Want) {
t.Errorf(problem)
t.Error(problem)
}
})
}
@ -950,7 +950,7 @@ func TestParseRef(t *testing.T) {
}
for _, problem := range deep.Equal(got, test.Want) {
t.Errorf(problem)
t.Error(problem)
}
})
}

@ -403,7 +403,7 @@ func TestParseTarget(t *testing.T) {
}
for _, problem := range deep.Equal(got, test.Want) {
t.Errorf(problem)
t.Error(problem)
}
})
}

@ -432,7 +432,7 @@ func TestParseProviderSourceStr(t *testing.T) {
for name, test := range tests {
got, diags := ParseProviderSourceString(name)
for _, problem := range deep.Equal(got, test.Want) {
t.Errorf(problem)
t.Error(problem)
}
if len(diags) > 0 {
if test.Err == false {

@ -439,9 +439,7 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backendrun.
}
err = b.confirm(stopCtx, op, opts, r, "override")
if err != nil && err != errRunOverridden {
return fmt.Errorf(
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
)
return fmt.Errorf("Failed to override: %w\n%s\n", err, runURL)
}
if err != errRunOverridden {

@ -412,9 +412,7 @@ func (b *Cloud) checkPolicy(stopCtx, cancelCtx context.Context, op *backendrun.O
}
err = b.confirm(stopCtx, op, opts, r, "override")
if err != nil && err != errRunOverridden {
return fmt.Errorf(
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
)
return fmt.Errorf("Failed to override: %w\n%s\n", err, runURL)
}
if err != errRunOverridden {

@ -191,9 +191,7 @@ func (b *Cloud) processStageOverrides(context *IntegrationContext, output Integr
runURL := fmt.Sprintf(taskStageHeader, b.Hostname, b.Organization, context.Op.Workspace, context.Run.ID)
err := b.confirm(context.StopContext, context.Op, opts, context.Run, "override")
if err != nil && err != errRunOverridden {
return false, fmt.Errorf(
fmt.Sprintf("Failed to override: %s\n%s\n", err.Error(), runURL),
)
return false, fmt.Errorf("Failed to override: %w\n%s\n", err, runURL)
}
if err != errRunOverridden {

@ -259,7 +259,7 @@ func skipWithoutRemoteTerraformVersion(t *testing.T) {
version := tfversion.Version
baseVersion, err := goversion.NewVersion(version)
if err != nil {
t.Fatalf(fmt.Sprintf("Error instantiating go-version for %s", version))
t.Fatalf("Error instantiating go-version for %s", version)
}
opts := &tfe.AdminTerraformVersionsListOptions{
ListOptions: tfe.ListOptions{

@ -167,7 +167,7 @@ func (c *InitCommand) Run(args []string) int {
// be the first error displayed if that is an issue, but other operations are required
// before being able to check core version requirements.
if rootModEarly == nil {
diags = diags.Append(fmt.Errorf(view.PrepareMessage(views.InitConfigError)), earlyConfDiags)
diags = diags.Append(errors.New(view.PrepareMessage(views.InitConfigError)), earlyConfDiags)
view.Diagnostics(diags)
return 1
@ -259,7 +259,7 @@ func (c *InitCommand) Run(args []string) int {
diags = diags.Append(earlyConfDiags)
diags = diags.Append(backDiags)
if earlyConfDiags.HasErrors() {
diags = diags.Append(fmt.Errorf(view.PrepareMessage(views.InitConfigError)))
diags = diags.Append(errors.New(view.PrepareMessage(views.InitConfigError)))
view.Diagnostics(diags)
return 1
}
@ -276,7 +276,7 @@ func (c *InitCommand) Run(args []string) int {
// show other errors from loading the full configuration tree.
diags = diags.Append(confDiags)
if confDiags.HasErrors() {
diags = diags.Append(fmt.Errorf(view.PrepareMessage(views.InitConfigError)))
diags = diags.Append(errors.New(view.PrepareMessage(views.InitConfigError)))
view.Diagnostics(diags)
return 1
}

@ -242,7 +242,7 @@ func (m *Meta) selectWorkspace(b backend.Backend) error {
log.Printf("[TRACE] Meta.selectWorkspace: selecting the new HCP Terraform workspace requested by the user (%s)", name)
return m.SetWorkspace(name)
} else {
return fmt.Errorf(strings.TrimSpace(errBackendNoExistingWorkspaces))
return errors.New(strings.TrimSpace(errBackendNoExistingWorkspaces))
}
}

@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -492,7 +491,7 @@ func (m *Meta) backendMigrateNonEmptyConfirm(
destination := destinationState.State()
// Save both to a temporary
td, err := ioutil.TempDir("", "terraform")
td, err := os.MkdirTemp("", "terraform")
if err != nil {
return false, fmt.Errorf("Error creating temporary directory: %s", err)
}
@ -577,7 +576,7 @@ func (m *Meta) backendMigrateTFC(opts *backendMigrateOpts) error {
if sourceTFC && !destinationTFC {
// From HCP Terraform to another backend. This is not yet implemented, and
// we recommend people to use the HCP Terraform API.
return fmt.Errorf(strings.TrimSpace(errTFCMigrateNotYetImplemented))
return errors.New(strings.TrimSpace(errTFCMigrateNotYetImplemented))
}
// Everything below, by the above two conditionals, now assumes that the

@ -286,7 +286,7 @@ func (c *ShowCommand) getDataFromCloudPlan(plan *cloudplan.SavedPlanBookmark, re
cl, ok := b.(*cloud.Cloud)
if !ok {
errMessage := fmt.Sprintf("can't show a saved cloud plan unless the current root module is connected to %s", cl.AppName())
return nil, errUnusable(fmt.Errorf(errMessage), "cloud plan")
return nil, errUnusable(errors.New(errMessage), "cloud plan")
}
result, err := cl.ShowPlanForRun(context.Background(), plan.RunID, plan.Hostname, redacted)

@ -45,7 +45,7 @@ func TestLoadConfigWithSnapshot(t *testing.T) {
problems := deep.Equal(wantModuleDirs, gotModuleDirs)
for _, problem := range problems {
t.Errorf(problem)
t.Error(problem)
}
if len(problems) > 0 {
return

@ -234,7 +234,7 @@ func TestSaveLocksToFile(t *testing.T) {
fileInfo, err := os.Stat(filename)
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}
if mode := fileInfo.Mode(); mode&0111 != 0 {
t.Fatalf("Expected lock file to be non-executable: %o", mode)
@ -242,7 +242,7 @@ func TestSaveLocksToFile(t *testing.T) {
gotContentBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}
gotContent := string(gotContentBytes)
wantContent := `# This file is maintained automatically by "terraform init".

@ -99,7 +99,7 @@ func TestPackagesServer_ProviderPackageVersions(t *testing.T) {
if diag.Severity == terraform1.Diagnostic_WARNING && diag.Summary == "Additional provider information from registry" {
expected := fmt.Sprintf("The remote registry returned warnings for %s:\n%s", tc.source, strings.Join(tc.expectedWarnings, "\n"))
if diff := cmp.Diff(expected, diag.Detail); len(diff) > 0 {
t.Errorf(diff)
t.Error(diff)
}
}
}
@ -122,7 +122,7 @@ func TestPackagesServer_ProviderPackageVersions(t *testing.T) {
}
if diff := cmp.Diff(tc.expectedVersions, response.Versions); len(diff) > 0 {
t.Errorf(diff)
t.Error(diff)
}
})
}
@ -277,7 +277,7 @@ func TestPackagesServer_FetchProviderPackage(t *testing.T) {
}
if diff := cmp.Diff(providerHashes[tc.platformLocations[platform]], result.Provider.Hashes); len(diff) > 0 {
t.Errorf(diff)
t.Error(diff)
}
}
})

@ -71,7 +71,7 @@ func TestContext2Apply_createBeforeDestroy_deposedKeyPreApply(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
_, diags = ctx.Apply(plan, m, nil)

@ -814,7 +814,7 @@ func TestContext2Apply_providerAliasConfigure(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
// Configure to record calls AFTER Plan above
@ -950,7 +950,7 @@ func TestContext2Apply_createBeforeDestroy(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags = ctx.Apply(plan, m, nil)
@ -1030,7 +1030,7 @@ func TestContext2Apply_createBeforeDestroyUpdate(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags = ctx.Apply(plan, m, nil)
@ -1087,7 +1087,7 @@ func TestContext2Apply_createBeforeDestroy_dependsNonCBD(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags = ctx.Apply(plan, m, nil)
@ -1151,7 +1151,7 @@ func TestContext2Apply_createBeforeDestroy_hook(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
if _, diags := ctx.Apply(plan, m, nil); diags.HasErrors() {
@ -1227,7 +1227,7 @@ func TestContext2Apply_createBeforeDestroy_deposedCount(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags = ctx.Apply(plan, m, nil)
@ -1287,7 +1287,7 @@ func TestContext2Apply_createBeforeDestroy_deposedOnly(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags = ctx.Apply(plan, m, nil)
@ -1619,7 +1619,7 @@ func TestContext2Apply_dataBasic(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags := ctx.Apply(plan, m, nil)
@ -1674,7 +1674,7 @@ func TestContext2Apply_destroyData(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
newState, diags := ctx.Apply(plan, m, nil)
@ -1739,7 +1739,7 @@ func TestContext2Apply_destroySkipsCBD(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
if _, diags := ctx.Apply(plan, m, nil); diags.HasErrors() {
@ -3742,7 +3742,7 @@ func TestContext2Apply_multiVarComprehensive(t *testing.T) {
t.Run("config for "+key, func(t *testing.T) {
for _, problem := range deep.Equal(got, want) {
t.Errorf(problem)
t.Error(problem)
}
})
}
@ -8333,7 +8333,7 @@ func TestContext2Apply_ignoreChangesCreate(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags := ctx.Apply(plan, m, nil)
@ -8475,7 +8475,7 @@ func TestContext2Apply_ignoreChangesAll(t *testing.T) {
logDiagnostics(t, diags)
t.Fatal("plan failed")
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags := ctx.Apply(plan, m, nil)
@ -12646,7 +12646,7 @@ func TestContext2Apply_dataSensitive(t *testing.T) {
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
state, diags := ctx.Apply(plan, m, nil)

@ -1393,7 +1393,7 @@ func TestContext2Plan_preventDestroy_bad(t *testing.T) {
expectedErr := "aws_instance.foo has lifecycle.prevent_destroy"
if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) {
if plan != nil {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
t.Fatalf("expected err would contain %q\nerr: %s", expectedErr, err)
}
@ -1494,7 +1494,7 @@ func TestContext2Plan_preventDestroy_countBad(t *testing.T) {
expectedErr := "aws_instance.foo[1] has lifecycle.prevent_destroy"
if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) {
if plan != nil {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
t.Fatalf("expected err would contain %q\nerr: %s", expectedErr, err)
}
@ -1620,7 +1620,7 @@ func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) {
expectedErr := "aws_instance.foo has lifecycle.prevent_destroy"
if !strings.Contains(fmt.Sprintf("%s", diags.Err()), expectedErr) {
if plan != nil {
t.Logf(legacyDiffComparisonString(plan.Changes))
t.Log(legacyDiffComparisonString(plan.Changes))
}
t.Fatalf("expected diagnostics would contain %q\nactual diags: %s", expectedErr, diags.Err())
}

@ -21,7 +21,7 @@ type DeferredTransformer struct {
}
func (t *DeferredTransformer) Transform(g *Graph) error {
if t.DeferredChanges == nil || len(t.DeferredChanges) == 0 {
if len(t.DeferredChanges) == 0 {
return nil
}

Loading…
Cancel
Save