Skip to content

Commit fbf1417

Browse files
authored
Merge pull request #166 from AndreasSko/fix_tmpdir
Support custom temporary directory
2 parents e228e49 + 0aaf953 commit fbf1417

17 files changed

Lines changed: 85 additions & 102 deletions

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- name: Install Go
1212
uses: actions/setup-go@v3
1313
with:
14-
go-version: '1.20'
14+
go-version: '1.24'
1515
- name: Checkout code
1616
uses: actions/checkout@v3
1717
- name: Run tests

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Install Go
2121
uses: actions/setup-go@v3
2222
with:
23-
go-version: '1.20'
23+
go-version: '1.24'
2424
- name: Checkout code
2525
uses: actions/checkout@v3
2626
- name: Run tests
@@ -32,7 +32,7 @@ jobs:
3232
- name: Install Go
3333
uses: actions/setup-go@v3
3434
with:
35-
go-version: '1.20'
35+
go-version: '1.24'
3636
- name: Install Gomobile
3737
run: go install golang.org/x/mobile/cmd/gomobile@latest
3838
- name: Checkout code

cmd/compare_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
//go:build !windows
12
// +build !windows
23

34
package cmd
45

56
import (
6-
"io/ioutil"
7-
"os"
87
"path/filepath"
98
"testing"
109
"time"
@@ -17,9 +16,7 @@ import (
1716
func Test_compare(t *testing.T) {
1817
t.Parallel()
1918

20-
tmp, err := ioutil.TempDir("", "go-jwlm")
21-
assert.NoError(t, err)
22-
defer os.RemoveAll(tmp)
19+
tmp := t.TempDir()
2320

2421
emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
2522
leftFilename := filepath.Join(tmp, "left.jwlibrary")

cmd/merge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package cmd
66
import (
77
"bytes"
88
"database/sql"
9-
"io/ioutil"
10-
"os"
119
"path/filepath"
1210
"testing"
1311

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

25-
tmp, err := ioutil.TempDir("", "go-jwlm")
26-
assert.NoError(t, err)
27-
defer os.RemoveAll(tmp)
23+
tmp := t.TempDir()
2824

2925
emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
3026
leftFilename := filepath.Join(tmp, "left.jwlibrary")

cmd/purge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package cmd
55

66
import (
7-
"io/ioutil"
8-
"os"
97
"path/filepath"
108
"testing"
119

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

21-
tmp, err := ioutil.TempDir("", "go-jwlm")
22-
assert.NoError(t, err)
23-
defer os.RemoveAll(tmp)
19+
tmp := t.TempDir()
2420

2521
inputFilename := filepath.Join(tmp, "left.jwlibrary")
2622
assert.NoError(t, leftDB.ExportJWLBackup(inputFilename))

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/AndreasSko/go-jwlm
22

3-
go 1.20
3+
go 1.24
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
153153
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
154154
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
155155
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
156+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
156157
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
157158
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
158159
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -336,6 +337,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
336337
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
337338
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
338339
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
340+
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
339341
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
340342
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
341343
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

gomobile/CatalogDB_test.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
package gomobile
@@ -6,7 +7,6 @@ import (
67
"crypto/sha256"
78
"fmt"
89
"io"
9-
"io/ioutil"
1010
"net/http"
1111
"net/http/httptest"
1212
"os"
@@ -20,15 +20,13 @@ import (
2020
)
2121

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

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

6462
func TestDownloadManager_CancelDownload(t *testing.T) {
65-
tmp, err := ioutil.TempDir("", "go-jwlm")
66-
assert.NoError(t, err)
67-
defer os.RemoveAll(tmp)
63+
tmp := t.TempDir()
6864

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

7975
func TestCatalogNeedsUpdate(t *testing.T) {
80-
tmp, err := ioutil.TempDir("", "go-jwlm")
81-
assert.NoError(t, err)
82-
defer os.RemoveAll(tmp)
76+
tmp := t.TempDir()
8377

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

8680
filePath := filepath.Join(tmp, "catalog.db")
87-
_, err = os.Create(filePath)
81+
_, err := os.Create(filePath)
82+
assert.NoError(t, err)
8883

8984
assert.False(t, CatalogNeedsUpdate(filePath))
9085

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

9590
func TestCatalogExists(t *testing.T) {
96-
tmp, err := ioutil.TempDir("", "go-jwlm")
97-
assert.NoError(t, err)
98-
defer os.RemoveAll(tmp)
91+
tmp := t.TempDir()
9992

10093
filePath := filepath.Join(tmp, "catalog.db")
101-
_, err = os.Create(filePath)
94+
_, err := os.Create(filePath)
95+
assert.NoError(t, err)
10296

10397
assert.False(t, CatalogExists("not-valid-path"))
10498
assert.True(t, CatalogExists(filePath))

gomobile/Database.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
// DatabaseWrapper wraps the left, right, and merged
1111
// Database structs so they can be used with Gomobile.
1212
type DatabaseWrapper struct {
13+
TempDir string
14+
1315
left *model.Database
1416
right *model.Database
1517
merged *model.Database
@@ -28,6 +30,7 @@ type DatabaseWrapper struct {
2830
// on the given side.
2931
func (dbw *DatabaseWrapper) ImportJWLBackup(filename string, side string) error {
3032
db := &model.Database{
33+
TempDir: dbw.TempDir,
3134
SkipPlaylists: dbw.skipPlaylists,
3235
}
3336

@@ -58,7 +61,9 @@ func (dbw *DatabaseWrapper) SkipPlaylists(skipPlaylists bool) {
5861
func (dbw *DatabaseWrapper) Init() {
5962
dbw.leftTmp = model.MakeDatabaseCopy(dbw.left)
6063
dbw.rightTmp = model.MakeDatabaseCopy(dbw.right)
61-
dbw.merged = &model.Database{}
64+
dbw.merged = &model.Database{
65+
TempDir: dbw.TempDir,
66+
}
6267
merger.PrepareDatabasesPreMerge(dbw.leftTmp, dbw.rightTmp)
6368
}
6469

gomobile/Database_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package gomobile
55

66
import (
7-
"io/ioutil"
8-
"os"
97
"path/filepath"
108
"testing"
119

@@ -39,11 +37,13 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
3937
assert.Len(t, dbWrapper.right.Tag, 3)
4038
assert.Len(t, dbWrapper.right.TagMap, 3)
4139
assert.Len(t, dbWrapper.right.UserMark, 5)
40+
assert.Equal(t, dbWrapper.TempDir, dbWrapper.right.TempDir)
4241
},
4342
},
4443
{
4544
name: "contains playlists, skip playlists set, import left",
4645
dbw: &DatabaseWrapper{
46+
TempDir: t.TempDir(),
4747
skipPlaylists: true,
4848
},
4949
filename: filepath.Join(testdataDir, "backup_withPlaylist.jwlibrary"),
@@ -57,6 +57,7 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
5757
assert.Len(t, dbWrapper.left.Tag, 5)
5858
assert.Len(t, dbWrapper.left.TagMap, 5)
5959
assert.Len(t, dbWrapper.left.UserMark, 5)
60+
assert.Equal(t, dbWrapper.TempDir, dbWrapper.left.TempDir)
6061
},
6162
},
6263
{
@@ -89,6 +90,7 @@ func TestDatabaseWrapper_ImportJWLBackup(t *testing.T) {
8990
}
9091
for _, tt := range tests {
9192
t.Run(tt.name, func(t *testing.T) {
93+
tt.dbw.TempDir = t.TempDir()
9294
err := tt.dbw.ImportJWLBackup(tt.filename, tt.side)
9395
tt.wantErr(t, err)
9496

@@ -125,14 +127,18 @@ func TestDatabaseWrapper_SkipPlaylists(t *testing.T) {
125127
}
126128

127129
func TestDatabaseWrapper_Init(t *testing.T) {
128-
dbw := &DatabaseWrapper{}
130+
dbw := &DatabaseWrapper{
131+
TempDir: t.TempDir(),
132+
}
129133

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

133137
dbw.Init()
134138

135-
assert.True(t, dbw.merged.Equals(&model.Database{}))
139+
assert.True(t, dbw.merged.Equals(&model.Database{
140+
TempDir: dbw.TempDir,
141+
}))
136142
assert.True(t, dbw.leftTmp.Equals(dbw.left))
137143
assert.True(t, dbw.rightTmp.Equals(dbw.right))
138144

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

142148
dbw.merged = model.MakeDatabaseCopy(dbw.left)
143149
dbw.Init()
144-
assert.True(t, dbw.merged.Equals(&model.Database{}))
150+
assert.True(t, dbw.merged.Equals(&model.Database{
151+
TempDir: dbw.TempDir,
152+
}))
145153
assert.True(t, dbw.leftTmp.Equals(dbw.left))
146154
assert.True(t, dbw.rightTmp.Equals(dbw.right))
155+
assert.Equal(t, dbw.TempDir, dbw.merged.TempDir)
147156
}
148157

149158
func TestDatabaseWrapper_DBIsLoaded(t *testing.T) {
@@ -170,9 +179,7 @@ func TestDatabaseWrapper_DBIsLoaded(t *testing.T) {
170179
}
171180

172181
func TestDatabaseWrapper_ExportMerged(t *testing.T) {
173-
tmp, err := ioutil.TempDir("", "go-jwlm")
174-
assert.NoError(t, err)
175-
defer os.RemoveAll(tmp)
182+
tmp := t.TempDir()
176183

177184
dbw := &DatabaseWrapper{}
178185
dbw.merged = &model.Database{}

0 commit comments

Comments
 (0)