Ally/War commands
This commit is contained in:
+2
-2
@@ -165,8 +165,8 @@ func (f *fs) Read(path string, v encoding.BinaryUnmarshaler) error {
|
||||
return errors.New("can't unmarshal to a nil object")
|
||||
}
|
||||
|
||||
if f.lock != nil {
|
||||
return errors.New("lock must be released before read")
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before read")
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(f.root, path)
|
||||
|
||||
+19
-38
@@ -3,7 +3,6 @@ package fs
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/util"
|
||||
@@ -55,8 +54,10 @@ func TestExist(t *testing.T) {
|
||||
func TestWrite(t *testing.T) {
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage: %s", err)
|
||||
|
||||
unlock, err := fs.Lock()
|
||||
assert.NoError(t, err, "acquire lock: %s", err)
|
||||
|
||||
@@ -80,31 +81,31 @@ func TestWrite(t *testing.T) {
|
||||
sd := &sampleData{[]byte{0, 1, 2, 3}}
|
||||
err = fs.Write(tc.path, sd)
|
||||
if tc.err == "" {
|
||||
if err != nil {
|
||||
assert.Fail(t, "not expecting an error", "write to file %s: %s", tc.path, err)
|
||||
} else {
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
} else if tc.err != "" {
|
||||
if err == nil {
|
||||
assert.Fail(t, "expecting an error, got none", "write to file %s", tc.path)
|
||||
} else {
|
||||
assert.True(t, strings.Contains(err.Error(), tc.err), "expect: %q got: %q", tc.err, err.Error())
|
||||
}
|
||||
assert.ErrorContains(t, err, tc.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
err = unlock()
|
||||
assert.NoError(t, err, "unlocking existing lock")
|
||||
assert.NoError(t, unlock(), "unlocking existing lock")
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
root, cleanup := util.CreateWorkDir(t)
|
||||
defer cleanup()
|
||||
|
||||
sd := new(sampleData)
|
||||
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage: %s", err)
|
||||
|
||||
assert.EqualError(t, fs.Read("some.file", sd), "lock must be acquired before read")
|
||||
|
||||
unlock, err := fs.Lock()
|
||||
assert.NoError(t, err, "acquire lock: %s", err)
|
||||
|
||||
dirName := "some-dir"
|
||||
if err := os.Mkdir(filepath.Join(root, dirName), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -117,46 +118,26 @@ func TestRead(t *testing.T) {
|
||||
|
||||
for _, tc := range []struct {
|
||||
path string
|
||||
lock bool
|
||||
err string
|
||||
}{
|
||||
{path: fileName},
|
||||
{path: "/" + fileName},
|
||||
{path: fileName, lock: true, err: "lock must be released"},
|
||||
{path: lockFile, err: "read from the lock file"},
|
||||
{path: "dir/subdir/file-3.ext", err: "no such file"},
|
||||
{path: lockFile, err: "read from the lock file"},
|
||||
{path: dirName, err: "is a directory"},
|
||||
} {
|
||||
t.Run(tc.path, func(t *testing.T) {
|
||||
if tc.lock {
|
||||
unlock, err := fs.Lock()
|
||||
if err != nil {
|
||||
t.Fatalf("acquire lock: %s", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := unlock(); err != nil {
|
||||
t.Fatalf("release lock: %s", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
sd := new(sampleData)
|
||||
err = fs.Read(tc.path, sd)
|
||||
if tc.err == "" {
|
||||
if err != nil {
|
||||
assert.Fail(t, "read: not expecting an error, got: "+err.Error())
|
||||
} else {
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
|
||||
} else if tc.err != "" {
|
||||
if err == nil {
|
||||
assert.Fail(t, "read: expecting an error, got none")
|
||||
} else {
|
||||
assert.True(t, strings.Contains(err.Error(), tc.err), "expect: %q got: %q", tc.err, err.Error())
|
||||
}
|
||||
assert.ErrorContains(t, err, tc.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
assert.NoError(t, unlock(), "unlocking existing lock")
|
||||
}
|
||||
|
||||
func TestWriteErrorWithoutLock(t *testing.T) {
|
||||
@@ -167,7 +148,7 @@ func TestWriteErrorWithoutLock(t *testing.T) {
|
||||
sd := &sampleData{[]byte{0, 1, 2, 3}}
|
||||
err = fs.Write("some/path", sd)
|
||||
assert.Error(t, err, "should return error when no lock acquired")
|
||||
assert.True(t, strings.Contains(err.Error(), "lock must be acquired"), "should return missing lock error")
|
||||
assert.EqualError(t, err, "lock must be acquired before write")
|
||||
}
|
||||
|
||||
func TestNewFileStorageErrorNotExists(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user