Skip to content

Commit

Permalink
Unexport renamer function
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed May 24, 2024
1 parent 7930c13 commit 4ae2303
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 114 deletions.
18 changes: 4 additions & 14 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ import (
"github.com/otiai10/copy"
)

// Renamer sets the function for renaming a file, defaulting to os.Rename.
// Changing Renamer should only be done in tests.
var Renamer = os.Rename
// renamer sets the function for renaming a file, defaulting to os.Rename.
// Changing renamer should only be done in tests.
var renamer = os.Rename

// BaseNoExt returns the last element of path with any file extensions removed.
//
// e.g.
// "/home/dir/archive.tar.gz" -> "archive"
// "/home/dir/README.md" -> "README"
// "/home/dir/README" -> "README"
// "/home/dir/" -> "dir"
// "/" -> "/"
// ".." -> ".."
// "." -> "."
// "" -> "."
func BaseNoExt(path string) string {
base := filepath.Base(path)
if base == "." || base == ".." {
Expand All @@ -47,7 +37,7 @@ func Move(src, dst string) error {
}

// Rename when possible.
err := Renamer(src, dst)
err := renamer(src, dst)
if err == nil {
return nil
}
Expand Down
109 changes: 109 additions & 0 deletions fsutil/fsutil_move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package fsutil

import (
"errors"
"os"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
)

var dirOpts = []fs.PathOp{
fs.WithDir(
"child1",
fs.WithFile(
"foo.txt",
"foo",
),
),
fs.WithDir(
"child2",
fs.WithFile(
"bar.txt",
"bar",
),
),
}

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

t.Run("It fails if destination already exists", func(t *testing.T) {
t.Parallel()

tmpDir := fs.NewDir(t, "enduro")
fs.Apply(t, tmpDir, fs.WithFile("foobar.txt", ""))
fs.Apply(t, tmpDir, fs.WithFile("barfoo.txt", ""))

src := tmpDir.Join("foobar.txt")
dst := tmpDir.Join("barfoo.txt")
err := Move(src, dst)

assert.Error(t, err, "destination already exists")
})

t.Run("It moves files", func(t *testing.T) {
t.Parallel()

tmpDir := fs.NewDir(t, "enduro")
fs.Apply(t, tmpDir, fs.WithFile("foobar.txt", ""))

src := tmpDir.Join("foobar.txt")
dst := tmpDir.Join("barfoo.txt")
err := Move(src, dst)

assert.NilError(t, err)

_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)

_, err = os.Stat(dst)
assert.NilError(t, err)
})

t.Run("It moves directories", func(t *testing.T) {
t.Parallel()

tmpSrc := fs.NewDir(t, "enduro", dirOpts...)
src := tmpSrc.Path()
srcManifest := fs.ManifestFromDir(t, src)
tmpDst := fs.NewDir(t, "enduro")
dst := tmpDst.Join("nested")

err := Move(src, dst)

assert.NilError(t, err)
_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Assert(t, fs.Equal(dst, srcManifest))
})

t.Run("It copies directories when using different filesystems", func(t *testing.T) {
// This subtest isn't run in parallel because it modifies global state.
renamer = func(src, dst string) error {
return &os.LinkError{
Op: "rename",
Old: src,
New: dst,
Err: errors.New("invalid cross-device link"),
}
}
t.Cleanup(func() {
renamer = os.Rename
})

tmpSrc := fs.NewDir(t, "enduro", dirOpts...)
src := tmpSrc.Path()
srcManifest := fs.ManifestFromDir(t, src)
tmpDst := fs.NewDir(t, "enduro")
dst := tmpDst.Join("nested")

err := Move(src, dst)

assert.NilError(t, err)
_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Assert(t, fs.Equal(dst, srcManifest))
})
}
112 changes: 12 additions & 100 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package fsutil_test

import (
"errors"
"os"
"fmt"
"path/filepath"
"testing"

Expand All @@ -12,23 +11,15 @@ import (
"go.artefactual.dev/tools/fsutil"
)

var Renamer = os.Rename

var dirOpts = []fs.PathOp{
fs.WithDir(
"child1",
fs.WithFile(
"foo.txt",
"foo",
),
),
fs.WithDir(
"child2",
fs.WithFile(
"bar.txt",
"bar",
),
),
func ExampleBaseNoExt() {
fmt.Println(fsutil.BaseNoExt("/home/dir/archive.tar.gz"))
fmt.Println(fsutil.BaseNoExt("/home/dir/README.md"))
fmt.Println(fsutil.BaseNoExt("/home/dir/README"))
fmt.Println(fsutil.BaseNoExt("/home/dir/"))
// Output: archive
// README
// README
// dir
}

func TestBaseNoExt(t *testing.T) {
Expand Down Expand Up @@ -80,93 +71,14 @@ func TestBaseNoExt(t *testing.T) {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

b := fsutil.BaseNoExt(tc.path)
assert.Equal(t, b, tc.want)
})
}
}

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

t.Run("It fails if destination already exists", func(t *testing.T) {
t.Parallel()

tmpDir := fs.NewDir(t, "enduro")
fs.Apply(t, tmpDir, fs.WithFile("foobar.txt", ""))
fs.Apply(t, tmpDir, fs.WithFile("barfoo.txt", ""))

src := tmpDir.Join("foobar.txt")
dst := tmpDir.Join("barfoo.txt")
err := fsutil.Move(src, dst)

assert.Error(t, err, "destination already exists")
})

t.Run("It moves files", func(t *testing.T) {
t.Parallel()

tmpDir := fs.NewDir(t, "enduro")
fs.Apply(t, tmpDir, fs.WithFile("foobar.txt", ""))

src := tmpDir.Join("foobar.txt")
dst := tmpDir.Join("barfoo.txt")
err := fsutil.Move(src, dst)

assert.NilError(t, err)

_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)

_, err = os.Stat(dst)
assert.NilError(t, err)
})

t.Run("It moves directories", func(t *testing.T) {
t.Parallel()

tmpSrc := fs.NewDir(t, "enduro", dirOpts...)
src := tmpSrc.Path()
srcManifest := fs.ManifestFromDir(t, src)
tmpDst := fs.NewDir(t, "enduro")
dst := tmpDst.Join("nested")

err := fsutil.Move(src, dst)

assert.NilError(t, err)
_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Assert(t, fs.Equal(dst, srcManifest))
})

t.Run("It copies directories when using different filesystems", func(t *testing.T) {
fsutil.Renamer = func(src, dst string) error {
return &os.LinkError{
Op: "rename",
Old: src,
New: dst,
Err: errors.New("invalid cross-device link"),
}
}
t.Cleanup(func() {
fsutil.Renamer = os.Rename
})

tmpSrc := fs.NewDir(t, "enduro", dirOpts...)
src := tmpSrc.Path()
srcManifest := fs.ManifestFromDir(t, src)
tmpDst := fs.NewDir(t, "enduro")
dst := tmpDst.Join("nested")

err := fsutil.Move(src, dst)

assert.NilError(t, err)
_, err = os.Stat(src)
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Assert(t, fs.Equal(dst, srcManifest))
})
}

func TestSetFileModes(t *testing.T) {
td := fs.NewDir(t, "enduro-test-fsutil",
fs.WithDir("transfer", fs.WithMode(0o755),
Expand Down

0 comments on commit 4ae2303

Please sign in to comment.