forked from decred/dcrdata
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dcrsqlite: create DB-file parent directory when necessary (decred#516)
* dcrsqlite: add test to reproduce the issue decred#515 decred#515 * dcrsqlite: create DB-file parent directory when necessary Fixes decred#515 * dcrsqlite: move test helpers to a testutil package * testutil functions take *testing.T (#1)
- Loading branch information
Showing
6 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ ffldb_stake*/ | |
*.pprof | ||
testnet/ | ||
.DS_Store | ||
testdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dcrsqlite | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/decred/dcrdata/testutil" | ||
) | ||
|
||
// TestMissingParentFolder ensures InitDB() is able to create a new DB-file parent directory if necessary | ||
// See https://github.com/decred/dcrdata/issues/515 | ||
func TestMissingParentFolder(t *testing.T) { | ||
// Specify DB file in non-existent path | ||
testutil.ResetTempFolder(t) | ||
targetDBFile := testutil.FilePathInsideTempDir(t, "x/y/z/"+testutil.DefaultDBFileName) | ||
|
||
dbInfo := &DBInfo{FileName: targetDBFile} | ||
db, err := InitDB(dbInfo) | ||
if err != nil { | ||
t.Fatalf("InitDB() failed: %v", err) | ||
} | ||
|
||
if db == nil { | ||
t.Fatalf("InitDB() failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// package testutil provides some helper functions to be used in unit tests. | ||
package testutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
const ( | ||
DefaultDataDirname = "test.data" | ||
DefaultDBFileName = "test.dcrdata.sqlt.db" | ||
) | ||
|
||
// TempFolderPath returns path of a temporary directory used by these tests to | ||
// store data. | ||
func TempFolderPath(t *testing.T) string { | ||
testDir, err := filepath.Abs(DefaultDataDirname) | ||
if err != nil { | ||
t.Fatalf("Failed to produce DB-test folder path") | ||
} | ||
return testDir | ||
} | ||
|
||
// Ensures we run our test in a clean room. Removes all files created by any of | ||
// these tests in the temp directory. | ||
func ResetTempFolder(t *testing.T) { | ||
testFolderPath := TempFolderPath(t) | ||
err := os.RemoveAll(testFolderPath) | ||
// Failed to clear test-files | ||
if err != nil { | ||
t.Fatalf("Failed to clear temp folder") | ||
} | ||
} | ||
|
||
// FilePathInsideTempDir creates a path to a file inside the temp directory. | ||
func FilePathInsideTempDir(t *testing.T, pathInsideTempFolder string) string { | ||
tempDir := TempFolderPath(t) | ||
targetPath := filepath.Join(tempDir, pathInsideTempFolder) | ||
targetPath, err := filepath.Abs(targetPath) | ||
if err != nil { | ||
t.Fatalf("Failed to build a path: " + pathInsideTempFolder) | ||
} | ||
return targetPath | ||
} |