create basic controller

This commit is contained in:
Ilia Denisov
2025-09-26 20:54:34 +03:00
parent 282150a253
commit 66eeefc65d
13 changed files with 357 additions and 165 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ func (f *fs) Lock() (func() error, error) {
return unlock, nil
}
func (f *fs) Exist(path string) (bool, error) {
func (f *fs) Exists(path string) (bool, error) {
return fileExists(filepath.Join(f.root, path))
}
+14 -20
View File
@@ -6,35 +6,33 @@ import (
"strings"
"testing"
"github.com/iliadenisov/galaxy/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestNewFileStorageSuccess(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
_, err := NewFileStorage(root)
assert.NoError(t, err)
}
func TestLock(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
fs, err := NewFileStorage(root)
assert.NoError(t, err, "create file storage")
unlock, err := fs.Lock()
assert.NoError(t, err, "acquire lock")
exists, err := fileExists(filepath.Join(root, lockFile))
assert.NoError(t, err, "check that the lock file should exist")
assert.True(t, exists, "lock file must exists")
lockPath := filepath.Join(root, lockFile)
assert.FileExists(t, lockPath, "lock file should be created")
err = unlock()
assert.NoError(t, err, "unlocking existing lock")
exists, err = fileExists(filepath.Join(root, lockFile))
assert.NoError(t, err, "check that the lock file does not exist")
assert.False(t, exists, "lock file must be removed")
assert.NoFileExists(t, lockPath, "lock file must be removed")
}
func TestExist(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
fileName := "some-file.ext"
@@ -45,17 +43,17 @@ func TestExist(t *testing.T) {
fs, err := NewFileStorage(root)
assert.NoError(t, err, "create file storage")
exist, err := fs.Exist(fileName)
exist, err := fs.Exists(fileName)
assert.NoError(t, err)
assert.True(t, exist)
exist, err = fs.Exist("random/path")
exist, err = fs.Exists("random/path")
assert.NoError(t, err)
assert.False(t, exist)
}
func TestWrite(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
fs, err := NewFileStorage(root)
assert.NoError(t, err, "create file storage: %s", err)
@@ -85,9 +83,7 @@ func TestWrite(t *testing.T) {
if err != nil {
assert.Fail(t, "not expecting an error", "write to file %s: %s", tc.path, err)
} else {
exists, err := fileExists(filepath.Join(root, tc.path))
assert.NoError(t, err, "check is written file exists")
assert.True(t, exists, "the written file should exist")
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
}
} else if tc.err != "" {
if err == nil {
@@ -104,7 +100,7 @@ func TestWrite(t *testing.T) {
}
func TestRead(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
fs, err := NewFileStorage(root)
assert.NoError(t, err, "create file storage: %s", err)
@@ -150,9 +146,7 @@ func TestRead(t *testing.T) {
if err != nil {
assert.Fail(t, "read: not expecting an error, got: "+err.Error())
} else {
exists, err := fileExists(filepath.Join(root, tc.path))
assert.NoError(t, err, "check is written file exists")
assert.True(t, exists, "the written file should exist")
assert.FileExists(t, filepath.Join(root, tc.path), "the written file should exist")
}
} else if tc.err != "" {
if err == nil {
@@ -166,7 +160,7 @@ func TestRead(t *testing.T) {
}
func TestWriteErrorWithoutLock(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
fs, err := NewFileStorage(root)
assert.NoError(t, err, "create file storage")
-15
View File
@@ -1,28 +1,13 @@
package fs
import (
"os"
"slices"
"testing"
)
const (
nonWritableDir = "/usr/lib"
)
func createWorkDir(t *testing.T) (string, func()) {
t.Helper()
dir, err := os.MkdirTemp("", "fs-test-workdir")
if err != nil {
t.Fatalf("create temp dir: %s", err)
}
return dir, func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatalf("remove temp dir: %s", err)
}
}
}
type sampleData struct {
data []byte
}
+5 -4
View File
@@ -6,30 +6,31 @@ import (
"testing"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestPathExists(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
testDirExistsFunc(t, root, func(s string) (bool, error) { return pathExists(s, true) })
testFileExistsFunc(t, root, func(s string) (bool, error) { return pathExists(s, false) })
}
func TestDirExists(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
testDirExistsFunc(t, root, dirExists)
}
func TestFileExists(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
testFileExistsFunc(t, root, fileExists)
}
func TestWritable(t *testing.T) {
root, cleanup := createWorkDir(t)
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
ok, err := writable(root)
assert.NoError(t, err, "directory writable check")