Skip to content

Commit

Permalink
fix(BRIDGE-10): fix linter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroNafta committed Apr 22, 2024
1 parent 3734c76 commit 261993e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/db_impl/sqlite3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -287,7 +288,9 @@ func pathExists(path string) (bool, error) {
}

func getDatabaseConn(dir, userID, path string) string {
return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", path)
// We need to escape special characters in the db path, such as #
escapedPath := url.PathEscape(path)
return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", escapedPath)
}

func TestUpdateDBVersion(ctx context.Context, dbPath, userID string, version int) error {
Expand Down
57 changes: 57 additions & 0 deletions internal/db_impl/sqlite3/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sqlite3

import (
"database/sql"
"fmt"
"os"
"os/user"
"path/filepath"
"testing"
)

const (
TestingDirectoryRoot = "/tmp/test_db_sqlite"
)

func TestClient_db_connection_special_characters_path(t *testing.T) {
dbDirs := []string{
"#test",
"test_test",
"test#test#test",
"test$test$test",
}

curUser, err := user.Current()
if err != nil {
fmt.Println("Could not get current current user, error: ", err)
t.FailNow()
}

testingDir := filepath.Join(curUser.HomeDir, TestingDirectoryRoot)

for _, dirName := range dbDirs {
path := filepath.Join(testingDir, dirName)
if err := os.MkdirAll(path, 0777); err != nil {
fmt.Println("Could not create testing directory, error: ", err)
t.FailNow()
}

filePath := filepath.Join(path, "test.db")

client, err := sql.Open("sqlite3", getDatabaseConn("test", "test", filePath))
if err != nil {
fmt.Println("Could not connect to test database, error: ", err)
t.FailNow()
}

if err := client.Ping(); err != nil {
fmt.Println("Could not ping test database, error: ", err)
t.FailNow()
}
}

if err := os.RemoveAll(TestingDirectoryRoot); err != nil {
fmt.Println("Could not remove testing directory, error: ", err)
t.FailNow()
}
}

0 comments on commit 261993e

Please sign in to comment.