Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ log/
**/*.sqlite
**/*.bck
tabbly*
!**/testData/**/*

# IDE files
**/.vscode/
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ require (
github.com/mattn/go-sqlite3 v1.14.32
github.com/shirou/gopsutil/v4 v4.25.8
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.11.1
gitlab.com/electrenator/mozilla-lz4-decoder v1.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ebitengine/purego v0.8.4 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/lufia/plan9stats v0.0.0-20250827001030-24949be3fa54 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.35.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 4 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0omw=
github.com/ebitengine/purego v0.8.4/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc=
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
github.com/lufia/plan9stats v0.0.0-20250827001030-24949be3fa54 h1:mFWunSatvkQQDhpdyuFAYwyAan3hzCuma+Pz8sqvOfg=
github.com/lufia/plan9stats v0.0.0-20250827001030-24949be3fa54/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
github.com/mattn/go-sqlite3 v1.14.27 h1:drZCnuvf37yPfs95E5jd9s3XhdVWLal+6BOK6qrv6IU=
github.com/mattn/go-sqlite3 v1.14.27/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
Expand All @@ -25,16 +19,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v4 v4.25.3 h1:SeA68lsu8gLggyMbmCn8cmp97V1TI9ld9sVzAUcKcKE=
github.com/shirou/gopsutil/v4 v4.25.3/go.mod h1:xbuxyoZj+UsgnZrENu3lQivsngRR5BdjbJwf2fv4szA=
github.com/shirou/gopsutil/v4 v4.25.8 h1:NnAsw9lN7587WHxjJA9ryDnqhJpFH6A+wagYWTOH970=
github.com/shirou/gopsutil/v4 v4.25.8/go.mod h1:q9QdMmfAOVIw7a+eF86P7ISEU6ka+NLgkUxlopV4RwI=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=
Expand All @@ -46,9 +36,9 @@ gitlab.com/electrenator/mozilla-lz4-decoder v1.0.0/go.mod h1:3KcErumQETJyGeUEXoc
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion internal/storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

const dbStructureDirectory = "database"
const developmentFileAddition = "-dev"

var Databasefiles embed.FS

Expand Down Expand Up @@ -255,7 +256,7 @@ func getVersionFromFile(filename string) int {
nameParts := strings.Split(filename, "-")

if len(nameParts) <= 0 {
return -1
return -1 // Likely will never arrive here
}
version, err := strconv.Atoi(nameParts[0])
if err != nil {
Expand Down
49 changes: 49 additions & 0 deletions internal/storage/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package internal_storage_test

import (
"database/sql"
"testing"

internal_storage "github.com/electrenator/tabbly/internal/storage"
"github.com/stretchr/testify/assert"
)

func TestGetCurrentDbSchemaVersion(test *testing.T) {
db, err := sql.Open("sqlite3", "file:testData/v0.sqlite")
if err != nil {
test.Error(err)
}
version := internal_storage.GetCurrentDbSchemaVersion(db)

assert.Equal(test, 0, version)
}

func TestGetCurrentDbSchemaVersionUninitialized(test *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
test.Error(err)
}
version := internal_storage.GetCurrentDbSchemaVersion(db)

assert.Equal(test, -1, version)
}

func TestGetVersionFromFile(test *testing.T) {
cases := map[string]int{
"000-something.sql": 0,
"010-newStuff!": 10,
".901-invalid": -1,
"": -1,
"901-aaa-4444": 901,
"9-2-391-call": 9,
"123456": 123456,
"123456.sql": -1,
"nan-123456": -1,
"123456-solution": 123456,
}

for caseInput, caseExpectedOutput := range cases {
output := internal_storage.GetVersionFromFile(caseInput)
assert.Equal(test, caseExpectedOutput, output, "With input \"%s\"", caseInput)
}
}
2 changes: 0 additions & 2 deletions internal/storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
internal_setting "github.com/electrenator/tabbly/internal/setting"
)

const developmentFileAddition = "-dev"

// Copies file from targetFileName path to copyFileName path.
func copyFile(targetFileName string, copyFileName string) error {
targetFile, err := os.Open(targetFileName)
Expand Down
94 changes: 94 additions & 0 deletions internal/storage/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package internal_storage_test

import (
"os"
"testing"
"time"

internal_setting "github.com/electrenator/tabbly/internal/setting"
internal_storage "github.com/electrenator/tabbly/internal/storage"
"github.com/stretchr/testify/assert"
)

func TestAsciiHostnameToCamelCase(test *testing.T) {
cases := map[string]string{
"hostname": "hostname",
"HOSTNAME": "hostname",
"Hostname": "hostname",
"hostname-with-separator": "hostnameWithSeparator",
"HOSNAME_MORE_SCREEEEEMINGGG-SEPRATOR": "hosnameMoreScreeeeemingggSeprator",
"Some-Intr_hostWITH,inValIdSEpERaToRS": "someIntrHostwith,invalidseperators",
}

for caseInput, caseExpectedOutput := range cases {
output := internal_storage.AsciiHostnameToCamelCase(caseInput)
assert.Equal(test, caseExpectedOutput, output)
}
}

func TestCloseFile(test *testing.T) {
const FILE_PATH = "/tmp/tabblyTestFile"
test.Cleanup(cleanFile(test, FILE_PATH))

file, err := os.OpenFile(FILE_PATH, os.O_CREATE|os.O_RDWR, internal_setting.DefaultFilePerms)
if err != nil {
test.Error(err)
test.FailNow()
}
internal_storage.CloseFile(file)

_, err = file.Write([]byte(":3"))
assert.ErrorIs(test, err, os.ErrClosed)
}

func TestCopyFile(test *testing.T) {
const FROM_FILE = "/tmp/tabblyFromFile"
const TO_FILE = "/tmp/tabblyToFile"
var content = "Cheese\n\t" + time.Now().String()

test.Cleanup(cleanFile(test, FROM_FILE))
test.Cleanup(cleanFile(test, TO_FILE))

err := prepareFile(FROM_FILE, content)
if err != nil {
test.Error(err)
test.FailNow()
}

err = internal_storage.CopyFile(FROM_FILE, TO_FILE)
if err != nil {
test.Error(err)
}

fileData, err := os.ReadFile(TO_FILE)
if err != nil {
test.Error(err)
}

assert.EqualValues(test, content, fileData)
}

func prepareFile(fileName string, contents string) error {
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, internal_setting.DefaultFilePerms)
if err != nil {
return err
}
_, err = file.Write([]byte(contents))
if err == nil {
err = file.Sync()
if err == nil {
err = file.Close()
}
}
return err
}

func cleanFile(test *testing.T, filepath string) func() {
cleanupFunction := func() {
err := os.Remove(filepath)
if err != nil {
test.Error(err)
}
}
return cleanupFunction
}
10 changes: 10 additions & 0 deletions internal/storage/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package internal_storage

// File
var AsciiHostnameToCamelCase = asciiHostnameToCamelCase
var CloseFile = closeFile
var CopyFile = copyFile

// Database
var GetCurrentDbSchemaVersion = getCurrentDbSchemaVersion
var GetVersionFromFile = getVersionFromFile
Binary file added internal/storage/testData/v0.sqlite
Binary file not shown.
37 changes: 14 additions & 23 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,26 @@ func StringContains(haystack string, needle string) bool {
}

// Expands user specific properties of a directory path. Paths which will be
// expanded might start with "~" or "%APPDATA%".
// expanded might start with "~" or "%APPDATA%". Returns an unaltered path
// when unsuccessful with error set.
func ExpandUserDirectory(path string) (string, error) {
usr, err := user.Current()
if err == nil {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(usr.HomeDir, path[2:])
} else if strings.HasPrefix(path, "%APPDATA%") {
appDataPath, err := os.UserConfigDir()
if err != nil {
return path, err
}
path = strings.Replace(path, "%APPDATA%", appDataPath, 1)
}
if err != nil {
return path, err
}
return path, err
}

// Converts a list of any into a list of the given type.
//
// From https://stackoverflow.com/a/24454401/13042236
func ConvertSlice[E any](in []any) (out []E) {
out = make([]E, 0, len(in))
for _, v := range in {
out = append(out, v.(E))
if strings.HasPrefix(path, "~/") {
path = filepath.Join(usr.HomeDir, path[2:])
} else if strings.HasPrefix(path, "%APPDATA%") {
appDataPath, err := os.UserConfigDir()
if err != nil {
return path, err
}
path = strings.Replace(path, "%APPDATA%", appDataPath, 1)
}
return
return path, nil
}

// Sums all entries of a list together.
// Sums all entries of a list together. Will return -1 if no slice is given.
func SumSlice[T ~int](slice []T) T {
if slice == nil {
return -1
Expand Down
Loading