Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.24'
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.24'
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.24'
- name: Install Gomobile
run: go install golang.org/x/mobile/cmd/gomobile@latest
- name: Checkout code
Expand Down
7 changes: 2 additions & 5 deletions cmd/compare_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build !windows
// +build !windows

package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
Expand All @@ -17,9 +16,7 @@ import (
func Test_compare(t *testing.T) {
t.Parallel()

tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
leftFilename := filepath.Join(tmp, "left.jwlibrary")
Expand Down
6 changes: 1 addition & 5 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package cmd
import (
"bytes"
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -22,9 +20,7 @@ import (
func Test_merge(t *testing.T) {
t.Parallel()

tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
leftFilename := filepath.Join(tmp, "left.jwlibrary")
Expand Down
6 changes: 1 addition & 5 deletions cmd/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -18,9 +16,7 @@ import (
func Test_purge(t *testing.T) {
t.Parallel()

tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

inputFilename := filepath.Join(tmp, "left.jwlibrary")
assert.NoError(t, leftDB.ExportJWLBackup(inputFilename))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/AndreasSko/go-jwlm

go 1.20
go 1.24

require (
github.com/AlecAivazis/survey/v2 v2.3.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -336,6 +337,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down
28 changes: 11 additions & 17 deletions gomobile/CatalogDB_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package gomobile
Expand All @@ -6,7 +7,6 @@ import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -20,15 +20,13 @@ import (
)

func TestDownloadCatalog(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if strings.Contains(req.URL.String(), "manifest.json") {
rw.Write([]byte(`{"version": 1, "current": "164a1c4b-4dbd-4909-8f88-8e7a18c562f2"}`))
} else {
data, err := ioutil.ReadFile(filepath.Join("../publication/testdata", "catalog.db.gz"))
data, err := os.ReadFile(filepath.Join("../publication/testdata", "catalog.db.gz"))
assert.NoError(t, err)
rw.Write(data)
}
Expand All @@ -54,17 +52,15 @@ func TestDownloadCatalog(t *testing.T) {
// Test error
publication.CatalogURL = "https://notvaliddomain.com/%s"
dm := DownloadCatalog(filepath.Join(tmp, "catalog.db"))
time.Sleep(250 * time.Millisecond)
time.Sleep(5 * time.Second)
assert.Error(t, dm.err)
assert.NotEqual(t, "", dm.Error())
assert.True(t, dm.Progress.Done)
assert.False(t, dm.DownloadSuccessful())
}

func TestDownloadManager_CancelDownload(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

dm := DownloadCatalog(filepath.Join(tmp, "catalog.db"))
dm.CancelDownload()
Expand All @@ -77,14 +73,13 @@ func TestDownloadManager_CancelDownload(t *testing.T) {
}

func TestCatalogNeedsUpdate(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

assert.True(t, CatalogNeedsUpdate("not-valid-path"))

filePath := filepath.Join(tmp, "catalog.db")
_, err = os.Create(filePath)
_, err := os.Create(filePath)
assert.NoError(t, err)

assert.False(t, CatalogNeedsUpdate(filePath))

Expand All @@ -93,12 +88,11 @@ func TestCatalogNeedsUpdate(t *testing.T) {
}

func TestCatalogExists(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

filePath := filepath.Join(tmp, "catalog.db")
_, err = os.Create(filePath)
_, err := os.Create(filePath)
assert.NoError(t, err)

assert.False(t, CatalogExists("not-valid-path"))
assert.True(t, CatalogExists(filePath))
Expand Down
7 changes: 6 additions & 1 deletion gomobile/Database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
// DatabaseWrapper wraps the left, right, and merged
// Database structs so they can be used with Gomobile.
type DatabaseWrapper struct {
TempDir string

left *model.Database
right *model.Database
merged *model.Database
Expand All @@ -28,6 +30,7 @@ type DatabaseWrapper struct {
// on the given side.
func (dbw *DatabaseWrapper) ImportJWLBackup(filename string, side string) error {
db := &model.Database{
TempDir: dbw.TempDir,
SkipPlaylists: dbw.skipPlaylists,
}

Expand Down Expand Up @@ -58,7 +61,9 @@ func (dbw *DatabaseWrapper) SkipPlaylists(skipPlaylists bool) {
func (dbw *DatabaseWrapper) Init() {
dbw.leftTmp = model.MakeDatabaseCopy(dbw.left)
dbw.rightTmp = model.MakeDatabaseCopy(dbw.right)
dbw.merged = &model.Database{}
dbw.merged = &model.Database{
TempDir: dbw.TempDir,
}
merger.PrepareDatabasesPreMerge(dbw.leftTmp, dbw.rightTmp)
}

Expand Down
23 changes: 15 additions & 8 deletions gomobile/Database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package gomobile

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -39,11 +37,13 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
assert.Len(t, dbWrapper.right.Tag, 3)
assert.Len(t, dbWrapper.right.TagMap, 3)
assert.Len(t, dbWrapper.right.UserMark, 5)
assert.Equal(t, dbWrapper.TempDir, dbWrapper.right.TempDir)
},
},
{
name: "contains playlists, skip playlists set, import left",
dbw: &DatabaseWrapper{
TempDir: t.TempDir(),
skipPlaylists: true,
},
filename: filepath.Join(testdataDir, "backup_withPlaylist.jwlibrary"),
Expand All @@ -57,6 +57,7 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
assert.Len(t, dbWrapper.left.Tag, 5)
assert.Len(t, dbWrapper.left.TagMap, 5)
assert.Len(t, dbWrapper.left.UserMark, 5)
assert.Equal(t, dbWrapper.TempDir, dbWrapper.left.TempDir)
},
},
{
Expand Down Expand Up @@ -89,6 +90,7 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dbw.TempDir = t.TempDir()
err := tt.dbw.ImportJWLBackup(tt.filename, tt.side)
tt.wantErr(t, err)

Expand Down Expand Up @@ -125,14 +127,18 @@ func TestDatabaseWrapper_SkipPlaylists(t *testing.T) {
}

func TestDatabaseWrapper_Init(t *testing.T) {
dbw := &DatabaseWrapper{}
dbw := &DatabaseWrapper{
TempDir: t.TempDir(),
}

assert.NoError(t, dbw.ImportJWLBackup(backupFile, "leftSide"))
assert.NoError(t, dbw.ImportJWLBackup(backupFile, "rightSide"))

dbw.Init()

assert.True(t, dbw.merged.Equals(&model.Database{}))
assert.True(t, dbw.merged.Equals(&model.Database{
TempDir: dbw.TempDir,
}))
assert.True(t, dbw.leftTmp.Equals(dbw.left))
assert.True(t, dbw.rightTmp.Equals(dbw.right))

Expand All @@ -141,9 +147,12 @@ func TestDatabaseWrapper_Init(t *testing.T) {

dbw.merged = model.MakeDatabaseCopy(dbw.left)
dbw.Init()
assert.True(t, dbw.merged.Equals(&model.Database{}))
assert.True(t, dbw.merged.Equals(&model.Database{
TempDir: dbw.TempDir,
}))
assert.True(t, dbw.leftTmp.Equals(dbw.left))
assert.True(t, dbw.rightTmp.Equals(dbw.right))
assert.Equal(t, dbw.TempDir, dbw.merged.TempDir)
}

func TestDatabaseWrapper_DBIsLoaded(t *testing.T) {
Expand All @@ -170,9 +179,7 @@ func TestDatabaseWrapper_DBIsLoaded(t *testing.T) {
}

func TestDatabaseWrapper_ExportMerged(t *testing.T) {
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

dbw := &DatabaseWrapper{}
dbw.merged = &model.Database{}
Expand Down
6 changes: 1 addition & 5 deletions gomobile/Merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package gomobile

import (
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -164,9 +162,7 @@ func Test_MergeNwt(t *testing.T) {
func Test_MergeNwtDifferentDocID(t *testing.T) {
// As the cleanup happens in the PostMerge hook, which is called on export,
// we also need to export the merged DB
tmp, err := ioutil.TempDir("", "go-jwlm")
assert.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

dbw := DatabaseWrapper{
left: model.MakeDatabaseCopy(leftDBNwtWithDifferentDocID),
Expand Down
15 changes: 11 additions & 4 deletions model/Database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -50,6 +49,8 @@ type Database struct {
// SkipPlaylists allows to skip prevention of merging if playlists exist in the database.
// It is meant as a temporary workaround until merging of playlists is implemented.
SkipPlaylists bool
// TempDir is used for temporary files. If not set, os.TempDir() will be used.
TempDir string
}

// FetchFromTable tries to fetch a entry with the given ID. If it can't find it
Expand Down Expand Up @@ -132,6 +133,8 @@ func MakeDatabaseCopy(db *Database) *Database {
cpField.Set(cpSlice)
case reflect.Bool:
cpField.SetBool(field.Bool())
case reflect.String:
cpField.SetString(field.String())
default:
panic(fmt.Sprintf("Field type %T is not supported for copying", tp))
}
Expand Down Expand Up @@ -221,6 +224,10 @@ func (db *Database) Equals(other *Database) bool {
if dbFields.Field(i).Bool() != otherFields.Field(i).Bool() {
return false
}
case reflect.String:
if dbFields.Field(i).String() != otherFields.Field(i).String() {
return false
}
default:
panic(fmt.Sprintf("field type %T is not supported for checking equality", tp))
}
Expand All @@ -233,7 +240,7 @@ func (db *Database) Equals(other *Database) bool {
// included SQLite DB to the Database struct
func (db *Database) ImportJWLBackup(filename string) error {
// Create tmp folder and place all files there
tmp, err := ioutil.TempDir("", "go-jwlm")
tmp, err := os.MkdirTemp(db.TempDir, "go-jwlm")
if err != nil {
return errors.Wrap(err, "Error while creating temporary directory")
}
Expand Down Expand Up @@ -593,7 +600,7 @@ func getSliceCapacity(sqlite *sql.DB, modelType Model) (int, error) {
// ExportJWLBackup creates a .jwlibrary backup file out of a Database{} struct
func (db *Database) ExportJWLBackup(filename string) error {
// Create tmp folder and place all files there
tmp, err := ioutil.TempDir("", "go-jwlm")
tmp, err := os.MkdirTemp(db.TempDir, "go-jwlm")
if err != nil {
return errors.Wrap(err, "Error while creating temporary directory")
}
Expand Down Expand Up @@ -759,7 +766,7 @@ func insertEntries(sqlite *sql.DB, m []Model) error {

// createEmptySQLiteDB creates a new SQLite database at filename with the base userData.db from JWLibrary
func createEmptySQLiteDB(filename string) error {
if err := ioutil.WriteFile(filename, userDataDatabaseFile, 0644); err != nil {
if err := os.WriteFile(filename, userDataDatabaseFile, 0644); err != nil {
return errors.Wrap(err, fmt.Sprintf("Error while saving new SQLite database at %s", filename))
}

Expand Down
Loading
Loading