Skip to content

Commit

Permalink
Fix issues related to error comparison (petoju#150)
Browse files Browse the repository at this point in the history
Mostly related to static code analysis
  • Loading branch information
petoju authored May 7, 2024
1 parent 7be1098 commit aa94599
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"golang.org/x/net/proxy"
"golang.org/x/oauth2"

cloudsqlconn "cloud.google.com/go/cloudsqlconn"
"cloud.google.com/go/cloudsqlconn"
cloudsql "cloud.google.com/go/cloudsqlconn/mysql/mysql"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion mysql/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
Expand Down Expand Up @@ -134,7 +135,7 @@ func ReadDatabase(ctx context.Context, d *schema.ResourceData, meta interface{})
}

if res != nil {
if res == sql.ErrNoRows {
if errors.Is(res, sql.ErrNoRows) {
return diag.Errorf("charset %s has no default collation", defaultCharset)
}

Expand Down
16 changes: 8 additions & 8 deletions mysql/resource_default_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func TestAccDefaultRoles_basic(t *testing.T) {
CheckDestroy: testAccDefaultRolesCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccDefaultRoles_basic,
Config: testAccDefaultRolesBasic,
Check: resource.ComposeTestCheckFunc(
testAccDefaultRoles("mysql_default_roles.test", "role1"),
resource.TestCheckResourceAttr("mysql_default_roles.test", "roles.#", "1"),
resource.TestCheckResourceAttr("mysql_default_roles.test", "roles.0", "role1"),
),
},
{
Config: testAccDefaultRoles_multiple,
Config: testAccDefaultRolesMultiple,
Check: resource.ComposeTestCheckFunc(
testAccDefaultRoles("mysql_default_roles.test", "role1", "role2"),
resource.TestCheckResourceAttr("mysql_default_roles.test", "roles.#", "2"),
Expand All @@ -38,21 +38,21 @@ func TestAccDefaultRoles_basic(t *testing.T) {
),
},
{
Config: testAccDefaultRoles_none,
Config: testAccDefaultRolesNone,
Check: resource.ComposeTestCheckFunc(
testAccDefaultRoles("mysql_default_roles.test"),
resource.TestCheckResourceAttr("mysql_default_roles.test", "roles.#", "0"),
),
},
{
Config: testAccDefaultRoles_basic,
Config: testAccDefaultRolesBasic,
ResourceName: "mysql_default_roles.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: fmt.Sprintf("%v@%v", "jdoe", "%"),
},
{
Config: testAccDefaultRoles_multiple,
Config: testAccDefaultRolesMultiple,
ResourceName: "mysql_default_roles.test",
ImportState: true,
ImportStateVerify: true,
Expand Down Expand Up @@ -142,7 +142,7 @@ func testAccDefaultRolesCheckDestroy(s *terraform.State) error {
return nil
}

const testAccDefaultRoles_basic = `
const testAccDefaultRolesBasic = `
resource "mysql_role" "role1" {
name = "role1"
}
Expand All @@ -166,7 +166,7 @@ resource "mysql_default_roles" "test" {
}
`

const testAccDefaultRoles_multiple = `
const testAccDefaultRolesMultiple = `
resource "mysql_role" "role1" {
name = "role1"
}
Expand Down Expand Up @@ -194,7 +194,7 @@ resource "mysql_default_roles" "test" {
}
`

const testAccDefaultRoles_none = `
const testAccDefaultRolesNone = `
resource "mysql_user" "test" {
user = "jdoe"
host = "%"
Expand Down
3 changes: 2 additions & 1 deletion mysql/resource_global_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
Expand Down Expand Up @@ -88,7 +89,7 @@ func ReadGlobalVariable(ctx context.Context, d *schema.ResourceData, meta interf
var name, value string
err = stmt.QueryRow(d.Id()).Scan(&name, &value)

if err != nil && err != sql.ErrNoRows {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
d.SetId("")
return diag.Errorf("error during show global variables: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion mysql/resource_global_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"context"
"database/sql"
"errors"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -153,7 +154,7 @@ func testAccGetGlobalVar(varName string, db *sql.DB) (string, error) {
var value string
err = stmt.QueryRow(varName).Scan(&name, &value)

if err != nil && err != sql.ErrNoRows {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}

Expand Down
6 changes: 3 additions & 3 deletions mysql/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func prepareProcedure(dbname string, procedureName string) resource.TestCheckFun
_, err = db.ExecContext(ctx, fmt.Sprintf("USE `%s`", dbname))
log.Printf("[DEBUG] SQL: %s", dbname)
if err != nil {
return fmt.Errorf("Error selecting database %s: %s", dbname, err)
return fmt.Errorf("error selecting database %s: %s", dbname, err)
}

// Check if the procedure exists
Expand All @@ -868,7 +868,7 @@ WHERE ROUTINE_SCHEMA = ? AND ROUTINE_NAME = ? AND ROUTINE_TYPE = 'PROCEDURE'
log.Printf("[DEBUG] SQL: %s", checkExistenceSQL)
err = db.QueryRowContext(ctx, checkExistenceSQL, dbname, procedureName).Scan(&exists)
if err != nil {
return fmt.Errorf("Error checking existence of procedure %s: %s", procedureName, err)
return fmt.Errorf("error checking existence of procedure %s: %s", procedureName, err)
}

if exists > 0 {
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func testAccCheckProcedureGrant(resourceName, userName, hostName, procedureName

// Compare the result with the expected outcome
if found != expected {
return fmt.Errorf("Grant for procedure %s does not match expected state: %v", procedureName, expected)
return fmt.Errorf("grant for procedure %s does not match expected state: %v", procedureName, expected)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion mysql/resource_ti_config_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func testAccGetConfigVar(varName string, varType string, db *sql.DB) (string, st

err = stmt.QueryRow(varName, varType).Scan(&resType, &resInstance, &resName, &resValue)

if err != nil && err != sql.ErrNoRows {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "nil", "nil", err
}

Expand Down
10 changes: 6 additions & 4 deletions mysql/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -214,7 +215,7 @@ func testAccUserExists(rn string) resource.TestCheckFunc {
var count int
err = db.QueryRow(stmtSQL).Scan(&count)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("expected 1 row reading user but got no rows")
}
return fmt.Errorf("error reading user: %s", err)
Expand Down Expand Up @@ -246,7 +247,7 @@ func testAccUserAuthExists(rn string) resource.TestCheckFunc {
var count int
err = db.QueryRow(stmtSQL).Scan(&count)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("expected 1 row reading user but got no rows")
}
return fmt.Errorf("error reading user: %s", err)
Expand Down Expand Up @@ -292,8 +293,9 @@ func testAccUserCheckDestroy(s *terraform.State) error {
if err != nil {
return fmt.Errorf("error issuing query: %s", err)
}
defer rows.Close()
if rows.Next() {
haveNext := rows.Next()
rows.Close()
if haveNext {
return fmt.Errorf("user still exists after destroy")
}
}
Expand Down

0 comments on commit aa94599

Please sign in to comment.