Skip to content

Commit 6241f9d

Browse files
authored
Merge pull request #59 from LandonTClipp/copy
Add wrapper around `io.Copy`
2 parents b8352c8 + 9b62216 commit 6241f9d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

path.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,24 @@ func (p *Path) Mtime() (time.Time, error) {
615615
return Mtime(stat)
616616
}
617617

618+
// Copy copies the path to another path using io.Copy.
619+
// Returned is the number of bytes copied and any error values.
620+
// The destination file is truncated if it exists, and is created
621+
// if it does not exist.
622+
func (p *Path) Copy(other *Path) (int64, error) {
623+
srcFile, err := p.Open()
624+
if err != nil {
625+
return 0, fmt.Errorf("opening source file: %w", err)
626+
}
627+
defer srcFile.Close()
628+
dstFile, err := other.OpenFile(os.O_TRUNC | os.O_CREATE | os.O_WRONLY)
629+
if err != nil {
630+
return 0, fmt.Errorf("opening destination file: %w", err)
631+
}
632+
defer dstFile.Close()
633+
return io.Copy(dstFile, srcFile)
634+
}
635+
618636
// Mtime returns the mtime described in the given os.FileInfo object
619637
func Mtime(fileInfo os.FileInfo) (time.Time, error) {
620638
return fileInfo.ModTime(), nil

path_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,76 @@ func TestPath_Parts(t *testing.T) {
502502
})
503503
}
504504
}
505+
506+
func TestPath_Copy(t *testing.T) {
507+
tests := []struct {
508+
name string
509+
srcContents string
510+
dstContents string
511+
wantDstContents string
512+
createDstFile bool
513+
wantErr bool
514+
}{
515+
{
516+
name: "copy empty file to existing non-empty file",
517+
srcContents: "",
518+
dstContents: "foobar",
519+
wantDstContents: "",
520+
createDstFile: true,
521+
},
522+
{
523+
name: "copy empty file to existing empty file",
524+
srcContents: "",
525+
dstContents: "",
526+
wantDstContents: "",
527+
createDstFile: true,
528+
},
529+
{
530+
name: "copy empty file to non-existing file",
531+
srcContents: "",
532+
wantDstContents: "",
533+
createDstFile: false,
534+
},
535+
{
536+
name: "copy non-empty file to existing non-empty file",
537+
srcContents: "foobar",
538+
dstContents: "hello world",
539+
wantDstContents: "foobar",
540+
createDstFile: true,
541+
},
542+
{
543+
name: "copy non-empty file to existing empty file",
544+
srcContents: "foobar",
545+
dstContents: "",
546+
wantDstContents: "foobar",
547+
createDstFile: true,
548+
},
549+
{
550+
name: "copy non-empty file to non-existing file",
551+
srcContents: "foobar",
552+
wantDstContents: "foobar",
553+
createDstFile: false,
554+
},
555+
}
556+
for _, tt := range tests {
557+
t.Run(tt.name, func(t *testing.T) {
558+
tmpdir := NewPath(t.TempDir())
559+
src := tmpdir.Join("src.txt")
560+
dst := tmpdir.Join("dst.txt")
561+
require.NoError(t, src.WriteFile([]byte(tt.srcContents)))
562+
563+
if tt.createDstFile {
564+
require.NoError(t, dst.WriteFile([]byte(tt.dstContents)))
565+
}
566+
567+
_, err := src.Copy(dst)
568+
if !tt.wantErr {
569+
require.NoError(t, err)
570+
}
571+
572+
dstBytes, err := dst.ReadFile()
573+
require.NoError(t, err)
574+
assert.Equal(t, []byte(tt.wantDstContents), dstBytes)
575+
})
576+
}
577+
}

0 commit comments

Comments
 (0)