Skip to content

Commit

Permalink
make tests run in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 9, 2024
1 parent c74c3ab commit f3049d6
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 84 deletions.
34 changes: 23 additions & 11 deletions backupfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/stretchr/testify/require"
)

func TestBackupFSCreate(t *testing.T) {
func TestBackupFS_Create(t *testing.T) {
t.Parallel()

var (
basePrefix = "/base"
backupPrefix = "/backup"
Expand Down Expand Up @@ -52,15 +54,17 @@ func TestBackupFSCreate(t *testing.T) {
mustNotExist(t, root, "backup"+newFilePath)
}

func TestBackupFSName(t *testing.T) {
func TestBackupFS_Name(t *testing.T) {
t.Parallel()

require := require.New(t)
_, _, _, backupFS := NewTestBackupFS("/base", "/backup")

require.Equal(backupFS.Name(), "BackupFS")
}

func TestBackupFSOpenFile(t *testing.T) {
func TestBackupFS_OpenFile(t *testing.T) {
t.Parallel()

var (
basePrefix = "/base"
Expand Down Expand Up @@ -96,7 +100,8 @@ func TestBackupFSOpenFile(t *testing.T) {
mustNotExist(t, root, "backup"+newFilePath)
}

func TestBackupFSRemove(t *testing.T) {
func TestBackupFS_Remove(t *testing.T) {
t.Parallel()

var (
basePrefix = "/base"
Expand All @@ -121,7 +126,8 @@ func TestBackupFSRemove(t *testing.T) {
mustExist(t, root, "backup"+filePath)
}

func TestBackupFSRemoveAll(t *testing.T) {
func TestBackupFS_RemoveAll(t *testing.T) {
t.Parallel()

var (
basePrefix = "/base"
Expand Down Expand Up @@ -181,10 +187,10 @@ func TestBackupFSRemoveAll(t *testing.T) {

mustExist(t, backup, fileDir)
mustExist(t, backup, fileDir2)

}

func TestBackupFSRename(t *testing.T) {
func TestBackupFS_Rename(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
Expand Down Expand Up @@ -226,7 +232,8 @@ func TestBackupFSRename(t *testing.T) {
mustNotExist(t, backup, newerDirName)
}

func TestBackupFSRollback(t *testing.T) {
func TestBackupFS_Rollback(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
Expand Down Expand Up @@ -317,7 +324,8 @@ func TestBackupFSRollback(t *testing.T) {
mustExist(t, backupFS, fileDir)
}

func TestBackupFSRollbackWithForcedBackup(t *testing.T) {
func TestBackupFS_RollbackWithForcedBackup(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
Expand Down Expand Up @@ -415,7 +423,8 @@ func TestBackupFSRollbackWithForcedBackup(t *testing.T) {
mustNotExist(t, backupFS, fileDir)
}

func TestBackupFSJSON(t *testing.T) {
func TestBackupFS_JSON(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
Expand Down Expand Up @@ -532,10 +541,10 @@ func TestBackupFSJSON(t *testing.T) {
// but old directories that did exist before should still exist
mustExist(t, base, "/test/001")
mustExist(t, backupFSNew, "/test/001")

}

func TestBackupFS_Symlink(t *testing.T) {
t.Parallel()

var (
basePrefix = "/base"
Expand Down Expand Up @@ -609,6 +618,7 @@ func TestBackupFS_Symlink(t *testing.T) {
}

func TestBackupFS_Mkdir(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
Expand Down Expand Up @@ -645,6 +655,8 @@ func TestBackupFS_Mkdir(t *testing.T) {
}

func TestBackupFS_Chmod(t *testing.T) {
t.Parallel()

var (
require = require.New(t)
basePrefix = "/base"
Expand Down
2 changes: 2 additions & 0 deletions hidden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestIsHidden(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDir := "/var/opt/backups"

Expand Down
34 changes: 17 additions & 17 deletions hiddenfs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
"path/filepath"
)

var _ File = (*HiddenFSFile)(nil)
var _ File = (*hiddenFSFile)(nil)

func newHiddenFSFile(f File, filePath string, hiddenPaths []string) *HiddenFSFile {
return &HiddenFSFile{
func newHiddenFSFile(f File, filePath string, hiddenPaths []string) *hiddenFSFile {
return &hiddenFSFile{
filePath: filePath,
f: f,
hiddenPaths: hiddenPaths,
}
}

type HiddenFSFile struct {
type hiddenFSFile struct {
f File
filePath string
hiddenPaths []string
}

func (hf *HiddenFSFile) Name() string {
func (hf *hiddenFSFile) Name() string {
return hf.f.Name()
}
func (hf *HiddenFSFile) Readdir(count int) ([]fs.FileInfo, error) {
func (hf *hiddenFSFile) Readdir(count int) ([]fs.FileInfo, error) {
var availableFiles []fs.FileInfo
if count > 0 {
availableFiles = make([]fs.FileInfo, 0, count)
Expand Down Expand Up @@ -78,7 +78,7 @@ func (hf *HiddenFSFile) Readdir(count int) ([]fs.FileInfo, error) {

return availableFiles, nil
}
func (hf *HiddenFSFile) Readdirnames(count int) ([]string, error) {
func (hf *hiddenFSFile) Readdirnames(count int) ([]string, error) {
var availableFiles []string
if count > 0 {
availableFiles = make([]string, 0, count)
Expand Down Expand Up @@ -131,39 +131,39 @@ func (hf *HiddenFSFile) Readdirnames(count int) ([]string, error) {

return availableFiles, nil
}
func (hf *HiddenFSFile) Stat() (fs.FileInfo, error) {
func (hf *hiddenFSFile) Stat() (fs.FileInfo, error) {
return hf.f.Stat()
}
func (hf *HiddenFSFile) Sync() error {
func (hf *hiddenFSFile) Sync() error {
return hf.f.Sync()
}
func (hf *HiddenFSFile) Truncate(size int64) error {
func (hf *hiddenFSFile) Truncate(size int64) error {
return hf.f.Truncate(size)
}
func (hf *HiddenFSFile) WriteString(s string) (ret int, err error) {
func (hf *hiddenFSFile) WriteString(s string) (ret int, err error) {
return hf.f.WriteString(s)
}

func (hf *HiddenFSFile) Close() error {
func (hf *hiddenFSFile) Close() error {
return hf.f.Close()
}

func (hf *HiddenFSFile) Read(p []byte) (n int, err error) {
func (hf *hiddenFSFile) Read(p []byte) (n int, err error) {
return hf.f.Read(p)
}

func (hf *HiddenFSFile) ReadAt(p []byte, off int64) (n int, err error) {
func (hf *hiddenFSFile) ReadAt(p []byte, off int64) (n int, err error) {
return hf.f.ReadAt(p, off)
}

func (hf *HiddenFSFile) Seek(offset int64, whence int) (int64, error) {
func (hf *hiddenFSFile) Seek(offset int64, whence int) (int64, error) {
return hf.f.Seek(offset, whence)
}

func (hf *HiddenFSFile) Write(p []byte) (n int, err error) {
func (hf *hiddenFSFile) Write(p []byte) (n int, err error) {
return hf.f.Write(p)
}

func (hf *HiddenFSFile) WriteAt(p []byte, off int64) (n int, err error) {
func (hf *hiddenFSFile) WriteAt(p []byte, off int64) (n int, err error) {
return hf.f.WriteAt(p, off)
}
2 changes: 1 addition & 1 deletion hiddenfs_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
)

func FuzzHiddenFSCreate(f *testing.F) {
func FuzzHiddenFS_Create(f *testing.F) {

for _, seed := range []string{".", "/", "..", "\\", "hidefs_test.txt", "/var/opt/backups", "/var/opt"} {
f.Add(seed)
Expand Down
88 changes: 48 additions & 40 deletions hiddenfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,17 @@ import (
"github.com/stretchr/testify/require"
)

func NewTestTempDirHiddenFS(hiddenPaths ...string) (base FS, hfs *HiddenFS) {
return newTestTempDirHiddenFS(0, hiddenPaths...)
}

func newTestTempDirHiddenFS(caller int, hiddenPaths ...string) (base FS, hfs *HiddenFS) {
rootPath := CallerPathTmp(caller)
root := NewTempDirPrefixFS(rootPath)

hidden := "/hidden"
err := root.MkdirAll(hidden, 0700)
if err != nil {
panic(err)
}
base = NewPrefixFS(root, hidden)
return base, NewHiddenFS(base, hiddenPaths...)
}

func SetupTempDirHiddenFSTest(t *testing.T) (hiddenDirParent, hiddenDir, hiddenFile string, base FS, fs *HiddenFS) {
hiddenDirParent = "/var/opt"
hiddenDir = "/var/opt/backups"
hiddenFile = "hidden_file.txt"

// prepare base filesystem before using the hidden fs layer
base, fs = newTestTempDirHiddenFS(1, hiddenDir)

mkdir(t, base, hiddenDirParent, 0775)
mkdirAll(t, base, hiddenDir, 0775)
createFile(t, base, filepath.Join(hiddenDir, hiddenFile), "hidden content")
return
}

func TestCountFiles(t *testing.T) {
func TestHiddenFS_CountFiles(t *testing.T) {
t.Parallel()

hiddenDirParent, hiddenDir, _, base, fsys := SetupTempDirHiddenFSTest(t)

countFiles(t, base, hiddenDir, 2)
countFiles(t, fsys, hiddenDirParent, 1)
}

func TestHiddenFSCreate(t *testing.T) {
func TestHiddenFS_Create(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)
Expand All @@ -69,7 +40,8 @@ func TestHiddenFSCreate(t *testing.T) {
countFiles(t, fsys, hiddenDirParent, 2)
}

func TestHiddenFSMkdir(t *testing.T) {
func TestHiddenFS_Mkdir(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)
Expand All @@ -93,7 +65,8 @@ func TestHiddenFSMkdir(t *testing.T) {
countFiles(t, fsys, hiddenDirParent, 2)
}

func TestHiddenFSMkdirAll(t *testing.T) {
func TestHiddenFS_MkdirAll(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)
Expand All @@ -115,7 +88,8 @@ func TestHiddenFSMkdirAll(t *testing.T) {
countFiles(t, fsys, hiddenDirParent, 6)
}

func TestHiddenFSOpenFile(t *testing.T) {
func TestHiddenFS_OpenFile(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)
Expand All @@ -139,7 +113,8 @@ func TestHiddenFSOpenFile(t *testing.T) {
countFiles(t, fsys, hiddenDirParent, 6)
}

func TestHiddenFSRemove(t *testing.T) {
func TestHiddenFS_Remove(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)
Expand All @@ -156,9 +131,10 @@ func TestHiddenFSRemove(t *testing.T) {
countFiles(t, fsys, hiddenDirParent, 1)
}

func TestHiddenFSRemoveAll(t *testing.T) {
require := require.New(t)
func TestHiddenFS_RemoveAll(t *testing.T) {
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, _, base, fsys := SetupTempDirHiddenFSTest(t)

createFile(t, fsys, filepath.Join(hiddenDir[:len(hiddenDir)-2], "should_be_created"), "text")
Expand All @@ -174,8 +150,9 @@ func TestHiddenFSRemoveAll(t *testing.T) {
}

func TestHiddenFSSymlink(t *testing.T) {
require := require.New(t)
t.Parallel()

require := require.New(t)
hiddenDirParent, hiddenDir, hiddenFile, base, fsys := SetupTempDirHiddenFSTest(t)

var (
Expand Down Expand Up @@ -213,3 +190,34 @@ func TestHiddenFSSymlink(t *testing.T) {
countFiles(t, base, hiddenDir, 2)
countFiles(t, fsys, hiddenDirParent, 4)
}

func NewTestTempDirHiddenFS(hiddenPaths ...string) (base FS, hfs *HiddenFS) {
return newTestTempDirHiddenFS(0, hiddenPaths...)
}

func newTestTempDirHiddenFS(caller int, hiddenPaths ...string) (base FS, hfs *HiddenFS) {
rootPath := CallerPathTmp(caller)
root := NewTempDirPrefixFS(rootPath)

hidden := "/hidden"
err := root.MkdirAll(hidden, 0700)
if err != nil {
panic(err)
}
base = NewPrefixFS(root, hidden)
return base, NewHiddenFS(base, hiddenPaths...)
}

func SetupTempDirHiddenFSTest(t *testing.T) (hiddenDirParent, hiddenDir, hiddenFile string, base FS, fs *HiddenFS) {
hiddenDirParent = "/var/opt"
hiddenDir = "/var/opt/backups"
hiddenFile = "hidden_file.txt"

// prepare base filesystem before using the hidden fs layer
base, fs = newTestTempDirHiddenFS(1, hiddenDir)

mkdir(t, base, hiddenDirParent, 0775)
mkdirAll(t, base, hiddenDir, 0775)
createFile(t, base, filepath.Join(hiddenDir, hiddenFile), "hidden content")
return
}
Loading

0 comments on commit f3049d6

Please sign in to comment.