Skip to content

Commit

Permalink
feat(lfs): fix mistaken file/directory arrangement in unit tests (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Oct 15, 2024
1 parent 5c41c96 commit 6df10c2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
12 changes: 6 additions & 6 deletions lfs/fs-move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ var _ = Describe("op: move", Ordered, func() {
given: "[from] directory exists, [to] name does not exist, [no-clash]",
should: "fail, same directory move, use rename instead",
op: "Move",
require: lab.Static.FS.Scratch,
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.To.Directory,
arrange: func(entry fsTE[lfs.UniversalFS], _ lfs.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[lfs.UniversalFS], fS lfs.UniversalFS) {
IsSameDirMoveRejectionError(fS.Move(entry.from, entry.to), entry.should)
Expand All @@ -344,16 +344,16 @@ var _ = Describe("op: move", Ordered, func() {

Entry(nil, fsTE[lfs.UniversalFS]{
given: "[from] directory exists, [to] equal to [from], [clash]",
should: "succeed, ignored",
should: "fail, directory names can't be same",
op: "Move",
require: lab.Static.FS.Scratch,
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.From.Directory,
arrange: func(entry fsTE[lfs.UniversalFS], _ lfs.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[lfs.UniversalFS], fS lfs.UniversalFS) {
Expect(fS.Move(entry.from, entry.to)).To(Succeed())
IsSameDirMoveRejectionError(fS.Move(entry.from, entry.to), entry.should)
},
}),
)
Expand Down
2 changes: 1 addition & 1 deletion lfs/fs-mover-overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type overwriteMover struct {

func (m *overwriteMover) create() mover {
m.actions = movers{
{true, false, false, false}: m.moveFileWithName, // from exists as file, to does not exist
{true, false, false, false}: m.moveItemWithName, // from exists as file, to does not exist
{true, false, true, false}: m.moveItemWithName, // from exists as dir, to does not exist
{true, true, false, true}: m.moveItemWithoutName, // from exists as file,to exists as dir
{true, true, true, true}: m.moveItemWithoutNameClash, // from exists as dir, to exists as dir
Expand Down
15 changes: 13 additions & 2 deletions lfs/fs-mover-tentative.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type tentativeMover struct {

func (m *tentativeMover) create() mover {
m.actions = movers{
{true, false, false, false}: m.moveFileWithName, // from exists as file, to does not exist
{true, false, true, false}: m.moveItemWithName, // from exists as dir, to does not exist
{true, false, false, false}: m.moveItemWithName, // from exists as file, to does not exist
{true, false, true, false}: m.moveDirectoryWithName, // from exists as dir, to does not exist
{true, true, false, true}: m.moveItemWithoutName, // from exists as file,to exists as dir
{true, true, true, true}: m.moveItemWithoutNameClash, // from exists as dir, to exists as dir
{true, true, false, false}: m.rejectOverwriteOrNoOp, // from and to may refer to the same existing file
Expand All @@ -22,6 +22,17 @@ func (m *tentativeMover) create() mover {
return m
}

func (m *tentativeMover) moveDirectoryWithName(from, to string) error {
// 'to' includes the file name eg:
// from/file.txt => to/file.txt
//
if filepath.Dir(from) == filepath.Dir(to) {
return locale.NewRejectSameDirMoveError(moveOpName, from, to)
}

return m.moveItemWithName(from, to)
}

func (m *tentativeMover) moveItemWithoutName(from, to string) error {
// 'to' does not include the file name, so it has to be appended, eg:
// from/file.txt => to/
Expand Down
12 changes: 1 addition & 11 deletions lfs/fs-mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ func (m *baseMover) peek(name string) (exists, isDir bool) {
}

func (m *baseMover) moveItemWithName(from, to string) error {
// 'to' includes the file name eg:
// from/file.txt => to/file.txt
//
return os.Rename(
filepath.Join(m.root, from),
filepath.Join(m.root, to),
)
}

func (m *baseMover) moveFileWithName(from, to string) error {
// 'to' includes the file name eg:
// from/file.txt => to/file.txt
//
Expand Down Expand Up @@ -120,7 +110,7 @@ func (m *baseMover) moveItemWithoutNameClash(from, to string) error {
// If there were a merge facility, this is where we would implement this,
// ie merge the from directory with to, instead of returning an error.
//
return locale.NewInvalidBinaryFsOpError(moveOpName, from, to)
return locale.NewRejectSameDirMoveError(moveOpName, from, to)
}

return m.moveItemWithoutName(from, to)
Expand Down
12 changes: 6 additions & 6 deletions lfs/fs-rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ var _ = Describe("op: rename", Ordered, func() {
given: "[from] directory exists, [to] name does not exist, [no-clash]",
should: "succeed",
op: "Rename",
require: lab.Static.FS.Scratch,
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.To.Directory,
arrange: func(entry fsTE[lfs.RenameFS], _ lfs.RenameFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[lfs.RenameFS], fS lfs.RenameFS) {
Expect(fS.Rename(entry.from, entry.to)).To(Succeed())
Expand All @@ -256,16 +256,16 @@ var _ = Describe("op: rename", Ordered, func() {

Entry(nil, fsTE[lfs.RenameFS]{
given: "[from] directory exists, [to] equal to [from], [clash]",
should: "succeed, ignored",
should: "fail, directory names can't be same",
op: "Rename",
require: lab.Static.FS.Scratch,
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.From.Directory,
arrange: func(entry fsTE[lfs.RenameFS], _ lfs.RenameFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[lfs.RenameFS], fS lfs.RenameFS) {
Expect(fS.Rename(entry.from, entry.to)).To(Succeed())
IsLinkError(fS.Rename(entry.from, entry.to), entry.should)
},
}),
)
Expand Down

0 comments on commit 6df10c2

Please sign in to comment.