-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite: fix formatFilepath to handle paths w/wo trailing slash
This adds a a testable helper function to ensure this path construction always remains robust.
- Loading branch information
1 parent
65a4dc4
commit 656fc41
Showing
2 changed files
with
62 additions
and
1 deletion.
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
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,57 @@ | ||
package sqlite | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func Test_formatFilePathNotWindows(t *testing.T) { | ||
// This test defines "want" paths in the *NIX path convention used by linux, | ||
// mac, bsd, etc., but not windows. Rather than using build flags to skip | ||
// this entire file, we'll define this test as such and we can make another | ||
// for Windows hosts if we want to. | ||
if runtime.GOOS == "windows" { | ||
t.Skip("test not applicable to windows paths") | ||
} | ||
type args struct { | ||
path string | ||
fileName string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "abs path no slash", | ||
args: args{ | ||
path: "/tmp", | ||
fileName: "dbname", | ||
}, | ||
want: "/tmp/dbname.sqlite", | ||
}, | ||
{ | ||
name: "abs path with trailing slash", | ||
args: args{ | ||
path: "/tmp/", | ||
fileName: "dbname", | ||
}, | ||
want: "/tmp/dbname.sqlite", | ||
}, | ||
{ | ||
name: "rel path no slash", | ||
args: args{ | ||
path: "./here", | ||
fileName: "dbname", | ||
}, | ||
want: "here/dbname.sqlite", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := formatFilePath(tt.args.path, tt.args.fileName); got != tt.want { | ||
t.Errorf("formatFilePath() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |