Skip to content

Commit

Permalink
fix: StartParameters on Windows (#146)
Browse files Browse the repository at this point in the history
* change string delimiting char based on os

* test StartParameters in platform_test

* explanatory comment

* use double quotes instead of single quotes for parameter values on all platforms
  • Loading branch information
hugodutka authored Dec 8, 2024
1 parent 7c0ced3 commit f5c275b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,10 @@ func (ep *EmbeddedPostgres) Stop() error {
func encodeOptions(port uint32, parameters map[string]string) string {
options := []string{fmt.Sprintf("-p %d", port)}
for k, v := range parameters {
// Single-quote parameter values - they may have spaces.
options = append(options, fmt.Sprintf("-c %s='%s'", k, v))
// Double-quote parameter values - they may have spaces.
// Careful: CMD on Windows uses only double quotes to delimit strings.
// It treats single quotes as regular characters.
options = append(options, fmt.Sprintf("-c %s=\"%s\"", k, v))
}
return strings.Join(options, " ")
}
Expand Down
24 changes: 23 additions & 1 deletion platform-test/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ func Test_AllMajorVersions(t *testing.T) {
port := uint32(5555 + testNumber)
runtimePath := filepath.Join(tempExtractLocation, string(version))

maxConnections := 150
database := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().
Version(version).
Port(port).
RuntimePath(runtimePath))
RuntimePath(runtimePath).
StartParameters(map[string]string{
"max_connections": fmt.Sprintf("%d", maxConnections),
}))

if err := database.Start(); err != nil {
shutdownDBAndFail(t, err, database, version)
Expand All @@ -64,6 +68,24 @@ func Test_AllMajorVersions(t *testing.T) {
shutdownDBAndFail(t, err, database, version)
}

rows, err = db.Query(`SELECT setting::int max_conn FROM pg_settings WHERE name = 'max_connections';`)
if err != nil {
shutdownDBAndFail(t, err, database, version)
}
if !rows.Next() {
shutdownDBAndFail(t, fmt.Errorf("no rows returned for max_connections"), database, version)
}
var maxConnReturned int
if err := rows.Scan(&maxConnReturned); err != nil {
shutdownDBAndFail(t, err, database, version)
}
if maxConnReturned != maxConnections {
shutdownDBAndFail(t, fmt.Errorf("max_connections is %d, not %d as expected", maxConnReturned, maxConnections), database, version)
}
if err := rows.Close(); err != nil {
shutdownDBAndFail(t, err, database, version)
}

if err := db.Close(); err != nil {
shutdownDBAndFail(t, err, database, version)
}
Expand Down

0 comments on commit f5c275b

Please sign in to comment.