Skip to content

Commit

Permalink
Simplify config tests by using T.Setenv, T.TempDir
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored and jackc committed May 15, 2024
1 parent c81bba8 commit 0080acf
Showing 1 changed file with 11 additions and 33 deletions.
44 changes: 11 additions & 33 deletions pgconn/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -933,19 +934,6 @@ func TestParseConfigEnvLibpq(t *testing.T) {

pgEnvvars := []string{"PGHOST", "PGPORT", "PGDATABASE", "PGUSER", "PGPASSWORD", "PGAPPNAME", "PGSSLMODE", "PGCONNECT_TIMEOUT", "PGSSLSNI"}

savedEnv := make(map[string]string)
for _, n := range pgEnvvars {
savedEnv[n] = os.Getenv(n)
}
defer func() {
for k, v := range savedEnv {
err := os.Setenv(k, v)
if err != nil {
t.Fatalf("Unable to restore environment: %v", err)
}
}
}()

tests := []struct {
name string
envvars map[string]string
Expand Down Expand Up @@ -1021,8 +1009,7 @@ func TestParseConfigEnvLibpq(t *testing.T) {
}

for k, v := range tt.envvars {
err := os.Setenv(k, v)
require.NoError(t, err)
t.Setenv(k, v)
}

config, err := pgconn.ParseConfig("")
Expand All @@ -1038,16 +1025,11 @@ func TestParseConfigReadsPgPassfile(t *testing.T) {
skipOnWindows(t)
t.Parallel()

tf, err := os.CreateTemp("", "")
tfName := filepath.Join(t.TempDir(), "config")
err := os.WriteFile(tfName, []byte("test1:5432:curlydb:curly:nyuknyuknyuk"), 0600)
require.NoError(t, err)

defer tf.Close()
defer os.Remove(tf.Name())

_, err = tf.Write([]byte("test1:5432:curlydb:curly:nyuknyuknyuk"))
require.NoError(t, err)

connString := fmt.Sprintf("postgres://curly@test1:5432/curlydb?sslmode=disable&passfile=%s", tf.Name())
connString := fmt.Sprintf("postgres://curly@test1:5432/curlydb?sslmode=disable&passfile=%s", tfName)
expected := &pgconn.Config{
User: "curly",
Password: "nyuknyuknyuk",
Expand All @@ -1068,13 +1050,9 @@ func TestParseConfigReadsPgServiceFile(t *testing.T) {
skipOnWindows(t)
t.Parallel()

tf, err := os.CreateTemp("", "")
require.NoError(t, err)

defer tf.Close()
defer os.Remove(tf.Name())
tfName := filepath.Join(t.TempDir(), "config")

_, err = tf.Write([]byte(`
err := os.WriteFile(tfName, []byte(`
[abc]
host=abc.example.com
port=9999
Expand All @@ -1086,7 +1064,7 @@ host = def.example.com
dbname = defdb
user = defuser
application_name = spaced string
`))
`), 0600)
require.NoError(t, err)

defaultPort := getDefaultPort(t)
Expand All @@ -1098,7 +1076,7 @@ application_name = spaced string
}{
{
name: "abc",
connString: fmt.Sprintf("postgres:///?servicefile=%s&service=%s", tf.Name(), "abc"),
connString: fmt.Sprintf("postgres:///?servicefile=%s&service=%s", tfName, "abc"),
config: &pgconn.Config{
Host: "abc.example.com",
Database: "abcdb",
Expand All @@ -1120,7 +1098,7 @@ application_name = spaced string
},
{
name: "def",
connString: fmt.Sprintf("postgres:///?servicefile=%s&service=%s", tf.Name(), "def"),
connString: fmt.Sprintf("postgres:///?servicefile=%s&service=%s", tfName, "def"),
config: &pgconn.Config{
Host: "def.example.com",
Port: defaultPort,
Expand All @@ -1142,7 +1120,7 @@ application_name = spaced string
},
{
name: "conn string has precedence",
connString: fmt.Sprintf("postgres://other.example.com:7777/?servicefile=%s&service=%s&sslmode=disable", tf.Name(), "abc"),
connString: fmt.Sprintf("postgres://other.example.com:7777/?servicefile=%s&service=%s&sslmode=disable", tfName, "abc"),
config: &pgconn.Config{
Host: "other.example.com",
Database: "abcdb",
Expand Down

0 comments on commit 0080acf

Please sign in to comment.