Skip to content

Commit bab016e

Browse files
fergusstrangeferguss
andauthored
Ensure target directories exist before extraction and add test for runtime path creation (#158)
Co-authored-by: ferguss <[email protected]>
1 parent 4fb7ddc commit bab016e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

decompression.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ func defaultTarReader(xzReader *xz.Reader) (func() (*tar.Header, error), func()
2121
}
2222

2323
func decompressTarXz(tarReader func(*xz.Reader) (func() (*tar.Header, error), func() io.Reader), path, extractPath string) error {
24-
tempExtractPath, err := os.MkdirTemp(filepath.Dir(extractPath), "temp_")
24+
extractDirectory := filepath.Dir(extractPath)
25+
26+
if err := os.MkdirAll(extractDirectory, os.ModePerm); err != nil {
27+
return errorUnableToExtract(path, extractPath, err)
28+
}
29+
30+
tempExtractPath, err := os.MkdirTemp(extractDirectory, "temp_")
2531
if err != nil {
2632
return errorUnableToExtract(path, extractPath, err)
2733
}

embedded_postgres_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,37 @@ func Test_CachePath(t *testing.T) {
672672
}
673673
}
674674

675+
func Test_CustomRuntimePathCreatedWhenNotPresent(t *testing.T) {
676+
runtimeTempDir, err := os.MkdirTemp("", "non_existent_runtime_path")
677+
if err != nil {
678+
panic(err)
679+
}
680+
681+
defer func() {
682+
if err := os.RemoveAll(runtimeTempDir); err != nil {
683+
panic(err)
684+
}
685+
}()
686+
687+
postgresDataPath := filepath.Join(runtimeTempDir,
688+
fmt.Sprintf(".embedded-postgres-go-%d", 4444),
689+
"extracted")
690+
691+
database := NewDatabase(DefaultConfig().
692+
RuntimePath(postgresDataPath).
693+
BinariesPath(postgresDataPath).
694+
DataPath(filepath.Join(postgresDataPath, "data")).
695+
Database("hoh"))
696+
697+
if err := database.Start(); err != nil {
698+
shutdownDBAndFail(t, err, database)
699+
}
700+
701+
if err := database.Stop(); err != nil {
702+
shutdownDBAndFail(t, err, database)
703+
}
704+
}
705+
675706
func Test_CustomBinariesLocation(t *testing.T) {
676707
tempDir, err := os.MkdirTemp("", "prepare_database_test")
677708
if err != nil {

0 commit comments

Comments
 (0)