Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: test formatFilepath handling of paths w/wo trailing slash #228

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charithabandi yeah, you did have the fix, I must have seen a commit slightly earlier. I'm adding the tests in this PR that I was writing from earlier.

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)
}
})
}
}