Skip to content

Commit 8f44823

Browse files
committed
Set PGAPPNAME for tests
This makes it easier to identify the connections belonging to the test runs and something else, such as the psql CLI.
1 parent 4af2196 commit 8f44823

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

conn_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ func TestPgpass(t *testing.T) {
132132
assertPassword("pass_C", map[string]string{"host": "/tmp", "passfile": file, "user": "some_user"})
133133

134134
// Connection parameter takes precedence
135-
os.Setenv("PGPASSFILE", "/tmp")
136-
defer os.Unsetenv("PGPASSFILE")
135+
t.Setenv("PGPASSFILE", "/tmp")
137136
assertPassword("pass_A", map[string]string{"host": "server", "passfile": file, "dbname": "some_db", "user": "some_user"})
138137
if warnbuf.String() != "" {
139138
t.Errorf("warnbuf not empty: %s", warnbuf)

connector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,9 @@ func (cfg Config) string() string {
10431043
continue
10441044
}
10451045
}
1046+
if k == "application_name" && m[k] == "pqgo" {
1047+
continue
1048+
}
10461049
if k == "host" && len(cfg.multiHost) > 0 {
10471050
m[k] += "," + strings.Join(cfg.multiHost, ",")
10481051
}

connector_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"net"
10-
"os"
1110
"path/filepath"
1211
"reflect"
1312
"runtime"
@@ -78,8 +77,7 @@ func TestNewConnector(t *testing.T) {
7877
useConn(t, db)
7978
})
8079
t.Run("Environ", func(t *testing.T) {
81-
os.Setenv("PGPASSFILE", "/tmp/.pgpass")
82-
defer os.Unsetenv("PGPASSFILE")
80+
t.Setenv("PGPASSFILE", "/tmp/.pgpass")
8381
c, err := NewConnector("")
8482
if err != nil {
8583
t.Fatal(err)
@@ -102,7 +100,7 @@ func TestNewConnector(t *testing.T) {
102100
t.Fatal(err)
103101
}
104102
want := fmt.Sprintf(
105-
`map[client_encoding:UTF8 connect_timeout:20 datestyle:ISO, MDY dbname:pqgo host:localhost max_protocol_version:3.0 min_protocol_version:3.0 port:%d search_path:foo sslmode:disable sslsni:yes user:pqgo]`,
103+
`map[application_name:pqgo client_encoding:UTF8 connect_timeout:20 datestyle:ISO, MDY dbname:pqgo host:localhost max_protocol_version:3.0 min_protocol_version:3.0 port:%d search_path:foo sslmode:disable sslsni:yes user:pqgo]`,
106104
cfg.Port)
107105
if have := fmt.Sprintf("%v", c.cfg.tomap()); have != want {
108106
t.Errorf("\nhave: %s\nwant: %s", have, want)
@@ -229,6 +227,7 @@ func TestRuntimeParameters(t *testing.T) {
229227
{"fallback_application_name=bar", "application_name", "bar", "", false},
230228
}
231229

230+
pqtest.Unsetenv(t, "PGAPPNAME")
232231
for _, tt := range tests {
233232
t.Run("", func(t *testing.T) {
234233
if tt.skipPgbouncer {
@@ -905,11 +904,7 @@ func TestService(t *testing.T) {
905904
[svc5]
906905
`), h, ".pg_service.conf")
907906

908-
// pgbouncer sets PGPORT; not really used to just unset.
909-
// TODO: might want to add pqtest.Clearenv() or the like.
910-
t.Setenv("PGPORT", "")
911-
os.Unsetenv("PGPORT")
912-
907+
pqtest.Unsetenv(t, "PGPORT")
913908
for _, tt := range tests {
914909
t.Run("", func(t *testing.T) {
915910
for k, v := range tt.env {

internal/pqtest/pqtest.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func InvalidCertificate(err error) bool {
5252
return false
5353
}
5454

55+
// Unsetenv unsets the environment variable and uses Cleanup to restore the
56+
// value after the test.
57+
//
58+
// Because Setenv affects the whole process, it cannot be used in parallel tests
59+
// or tests with parallel ancestors.
60+
func Unsetenv(t *testing.T, k string) { t.Setenv(k, ""); os.Unsetenv(k) }
61+
5562
// Ptr gets a pointer to any value.
5663
//
5764
// TODO(go1.26): replace with new(..) once pq requires Go 1.26.
@@ -73,6 +80,7 @@ func DSN(conninfo string) string {
7380
defaultTo("PGUSER", "pqgo")
7481
defaultTo("PGSSLMODE", "disable")
7582
defaultTo("PGCONNECT_TIMEOUT", "20")
83+
os.Setenv("PGAPPNAME", "pqgo")
7684
})
7785

7886
if ForceBinaryParameters() &&

0 commit comments

Comments
 (0)