Skip to content

Commit

Permalink
sqlite: fix formatFilepath to handle paths w/wo trailing slash
Browse files Browse the repository at this point in the history
This adds a a testable helper function to ensure this path construction always remains robust.
  • Loading branch information
jchappelow authored Aug 28, 2023
1 parent 65a4dc4 commit 656fc41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/sql/sqlite/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ func (c *Connection) getFilePath() string {
return c.formatFilePath(c.name)
}

func formatFilePath(path, fileName string) string {
return filepath.Join(path, fileName+".sqlite")
}

func (c *Connection) formatFilePath(fileName string) string {
return fmt.Sprintf("%s.sqlite", filepath.Join(c.path, fileName))
return formatFilePath(c.path, fileName)
}

func (c *Connection) openConn() error {
Expand Down
57 changes: 57 additions & 0 deletions pkg/sql/sqlite/helpers_test.go
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)
}
})
}
}

0 comments on commit 656fc41

Please sign in to comment.