Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73cda76

Browse files
Add locale config option (fergusstrange#12)
1 parent b512d5a commit 73cda76

5 files changed

+71
-11
lines changed

config.go

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Config struct {
1010
username string
1111
password string
1212
runtimePath string
13+
locale string
1314
startTimeout time.Duration
1415
}
1516

@@ -68,6 +69,12 @@ func (c Config) RuntimePath(path string) Config {
6869
return c
6970
}
7071

72+
// Locale sets the default locale for initdb
73+
func (c Config) Locale(locale string) Config {
74+
c.locale = locale
75+
return c
76+
}
77+
7178
// StartTimeout sets the max timeout that will be used when starting the Postgres process and creating the initial database.
7279
func (c Config) StartTimeout(timeout time.Duration) Config {
7380
c.startTimeout = timeout

embedded_postgres.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (ep *EmbeddedPostgres) Start() error {
7575
return fmt.Errorf("unable to extract postgres archive %s to %s", cacheLocation, binaryExtractLocation)
7676
}
7777

78-
if err := ep.initDatabase(binaryExtractLocation, ep.config.username, ep.config.password); err != nil {
78+
if err := ep.initDatabase(binaryExtractLocation, ep.config.username, ep.config.password, ep.config.locale); err != nil {
7979
return err
8080
}
8181

embedded_postgres_test.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func Test_ErrorWhenUnableToInitDatabase(t *testing.T) {
117117
return jarFile, true
118118
}
119119

120-
database.initDatabase = func(binaryExtractLocation, username, password string) error {
120+
database.initDatabase = func(binaryExtractLocation, username, password, locale string) error {
121121
return errors.New("ah it did not work")
122122
}
123123

@@ -220,7 +220,7 @@ func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {
220220
return jarFile, true
221221
}
222222

223-
database.initDatabase = func(binaryExtractLocation, username, password string) error {
223+
database.initDatabase = func(binaryExtractLocation, username, password, locale string) error {
224224
return nil
225225
}
226226

@@ -248,7 +248,8 @@ func Test_CustomConfig(t *testing.T) {
248248
Version(V12).
249249
RuntimePath(tempDir).
250250
Port(9876).
251-
StartTimeout(10 * time.Second))
251+
StartTimeout(10 * time.Second).
252+
Locale("C"))
252253
if err := database.Start(); err != nil {
253254
shutdownDBAndFail(t, err, database)
254255
}
@@ -271,6 +272,31 @@ func Test_CustomConfig(t *testing.T) {
271272
}
272273
}
273274

275+
func Test_CustomLocaleConfig(t *testing.T) {
276+
// C is the only locale we can guarantee to always work
277+
database := NewDatabase(DefaultConfig().Locale("C"))
278+
if err := database.Start(); err != nil {
279+
shutdownDBAndFail(t, err, database)
280+
}
281+
282+
db, err := sql.Open("postgres", fmt.Sprintf("host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable"))
283+
if err != nil {
284+
shutdownDBAndFail(t, err, database)
285+
}
286+
287+
if err = db.Ping(); err != nil {
288+
shutdownDBAndFail(t, err, database)
289+
}
290+
291+
if err := db.Close(); err != nil {
292+
shutdownDBAndFail(t, err, database)
293+
}
294+
295+
if err := database.Stop(); err != nil {
296+
shutdownDBAndFail(t, err, database)
297+
}
298+
}
299+
274300
func Test_CanStartAndStopTwice(t *testing.T) {
275301
database := NewDatabase()
276302

prepare_database.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@ import (
1313
"github.com/lib/pq"
1414
)
1515

16-
type initDatabase func(binaryExtractLocation, username, password string) error
16+
type initDatabase func(binaryExtractLocation, username, password, locale string) error
1717
type createDatabase func(port uint32, username, password, database string) error
1818

19-
func defaultInitDatabase(binaryExtractLocation, username, password string) error {
19+
func defaultInitDatabase(binaryExtractLocation, username, password, locale string) error {
2020
passwordFile, err := createPasswordFile(binaryExtractLocation, password)
2121
if err != nil {
2222
return err
2323
}
2424

25-
postgresInitDbBinary := filepath.Join(binaryExtractLocation, "bin/initdb")
26-
postgresInitDbProcess := exec.Command(postgresInitDbBinary,
25+
args := []string{
2726
"-A", "password",
2827
"-U", username,
2928
"-D", filepath.Join(binaryExtractLocation, "data"),
30-
fmt.Sprintf("--pwfile=%s", passwordFile))
29+
fmt.Sprintf("--pwfile=%s", passwordFile),
30+
}
31+
32+
if locale != "" {
33+
args = append(args, fmt.Sprintf("--locale=%s", locale))
34+
}
35+
36+
postgresInitDbBinary := filepath.Join(binaryExtractLocation, "bin/initdb")
37+
postgresInitDbProcess := exec.Command(postgresInitDbBinary, args...)
3138
postgresInitDbProcess.Stderr = os.Stderr
3239
postgresInitDbProcess.Stdout = os.Stdout
3340

prepare_database_test.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func Test_defaultInitDatabase_ErrorWhenCannotCreatePasswordFile(t *testing.T) {
14-
err := defaultInitDatabase("path_not_exists", "Tom", "Beer")
14+
err := defaultInitDatabase("path_not_exists", "Tom", "Beer", "")
1515

1616
assert.EqualError(t, err, "unable to write password file to path_not_exists/pwfile")
1717
}
@@ -28,7 +28,7 @@ func Test_defaultInitDatabase_ErrorWhenCannotStartInitDBProcess(t *testing.T) {
2828
}
2929
}()
3030

31-
err = defaultInitDatabase(tempDir, "Tom", "Beer")
31+
err = defaultInitDatabase(tempDir, "Tom", "Beer", "")
3232

3333
assert.EqualError(t, err, fmt.Sprintf("unable to init database using: %s/bin/initdb -A password -U Tom -D %s/data --pwfile=%s/pwfile",
3434
tempDir,
@@ -37,6 +37,26 @@ func Test_defaultInitDatabase_ErrorWhenCannotStartInitDBProcess(t *testing.T) {
3737
assert.FileExists(t, filepath.Join(tempDir, "pwfile"))
3838
}
3939

40+
func Test_defaultInitDatabase_ErrorInvalidLocaleSetting(t *testing.T) {
41+
tempDir, err := ioutil.TempDir("", "prepare_database_test")
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
defer func() {
47+
if err := os.RemoveAll(tempDir); err != nil {
48+
panic(err)
49+
}
50+
}()
51+
52+
err = defaultInitDatabase(tempDir, "postgres", "postgres", "en_XY")
53+
54+
assert.EqualError(t, err, fmt.Sprintf("unable to init database using: %s/bin/initdb -A password -U postgres -D %s/data --pwfile=%s/pwfile --locale=en_XY",
55+
tempDir,
56+
tempDir,
57+
tempDir))
58+
}
59+
4060
func Test_defaultCreateDatabase_ErrorWhenSQLOpenError(t *testing.T) {
4161
err := defaultCreateDatabase(1234, "user client_encoding=lol", "password", "database")
4262

0 commit comments

Comments
 (0)