Skip to content

Commit

Permalink
Rewrite Copy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Feb 25, 2017
1 parent bd54a6f commit 6f787c2
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 123 deletions.
19 changes: 9 additions & 10 deletions fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"os"
"path"
)

Expand All @@ -22,8 +21,8 @@ func IsExists(osfs OS, path string) (bool, error) {
}

// Copy copies a file from src to dst.
func Copy(src, dst string) error {
sfi, err := os.Lstat(src)
func Copy(osfs OS, src, dst string) error {
sfi, err := osfs.Lstat(src)
if err != nil {
return err
}
Expand All @@ -32,9 +31,9 @@ func Copy(src, dst string) error {
return fmt.Errorf("Non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}

dfi, err := os.Stat(dst)
dfi, err := osfs.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
if !osfs.IsNotExist(err) {
return err
}
// file not exists - do not do anything
Expand All @@ -45,16 +44,16 @@ func Copy(src, dst string) error {
}
}

err = copyFileContents(src, dst)
err = copyFileContents(osfs, src, dst)
return err
}

// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
func copyFileContents(osfs OS, src, dst string) (err error) {
in, err := osfs.Open(src)
if err != nil {
return
}
Expand All @@ -66,12 +65,12 @@ func copyFileContents(src, dst string) (err error) {
}
}()

err = os.MkdirAll(path.Dir(dst), 0700)
err = osfs.MkdirAll(path.Dir(dst), 0700)
if err != nil {
return err
}

out, err := os.Create(dst)
out, err := osfs.Create(dst)
if err != nil {
return
}
Expand Down
218 changes: 108 additions & 110 deletions fileutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"errors"
"io/ioutil"
"os"
"path"
"reflect"
"testing"
)
Expand All @@ -19,7 +17,7 @@ func TestIsExists(t *testing.T) {
{
os: &FakeOS{
StatFileInfo: nil, // does not matter
StatError: errors.New("Permission denied"),
StatError: errors.New("Permission denied"),
IsNotExistResult: false,
},
name: "/path/that/errors/on/stat",
Expand Down Expand Up @@ -60,115 +58,115 @@ func TestIsExists(t *testing.T) {
}
}

func TestCopy(t *testing.T) {
testCopyPositive(t)
testCopyNegativeLstat(t)
testCopyNegativeSymlink(t)
}

func testCopyPositive(t *testing.T) {
// set up

src := "/tmp/dotbro/fileutils/original.txt"
content := []byte("Some Content")

if err := os.MkdirAll(path.Dir(src), 0755); err != nil {
t.Fatal(err)
}

if err := ioutil.WriteFile(src, content, 0755); err != nil {
t.Fatal(err)
}

// test

dest := "/tmp/dotbro/fileutils/copy.txt"
if err := Copy(src, dest); err != nil {
t.Error(err)
}

copyContent, err := ioutil.ReadFile(dest)
if err != nil {
t.Error(err)
}

if string(copyContent) != string(content) {
t.Error(err)
}

// tear down

if err := os.Remove(src); err != nil {
t.Error(err)
}

if err := os.Remove(dest); err != nil {
t.Error(err)
}
}

func testCopyNegativeLstat(t *testing.T) {
// set up

src := "/tmp/dotbro/fileutils/original.txt"

if err := os.MkdirAll(path.Dir(src), 0755); err != nil {
t.Fatal(err)
}

// no read permissions
if err := ioutil.WriteFile(src, nil, 0333); err != nil {
t.Fatal(err)
}

// test

dest := "/tmp/dotbro/fileutils/copy.txt"
err := Copy(dest, dest)
if err == nil {
t.Error("No error!")
}

// tear down

if err := os.Remove(src); err != nil {
t.Error(err)
}
}

func testCopyNegativeSymlink(t *testing.T) {
// set up

original := "/tmp/dotbro/fileutils/original.txt"

if err := os.MkdirAll(path.Dir(original), 0755); err != nil {
t.Fatal(err)
}

if err := ioutil.WriteFile(original, nil, 0755); err != nil {
t.Fatal(err)
}

symlink := "/tmp/dotbro/fileutils/symlink"
if err := os.Symlink(original, symlink); err != nil {
t.Fatal(err)
}

// test

dest := "/tmp/dotbro/fileutils/symlink-copy.txt"
err := Copy(symlink, dest)
if err == nil {
t.Error("No error!")
func TestCopyReal(t *testing.T) {
cases := []struct {
os *FakeOS
src string
dest string
expectedError error
}{
{
os: &FakeOS{
LstatError: errors.New("Permission denied"),
IsNotExistResult: false,
},
src: "/path/to/source",
dest: "/path/to/dest",
expectedError: errors.New("Permission denied"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "source",
ModeValue: os.ModeDir,
},
},
src: "/path/to/source",
dest: "/path/to/dest",
expectedError: errors.New("Non-regular source file source (\"d---------\")"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "source",
ModeValue: 0,
},
StatError: errors.New("Permisson denied 123"),
},
src: "/path/to/source",
dest: "/path/to/dest",
expectedError: errors.New("Permisson denied 123"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "source",
ModeValue: 0,
},
StatFileInfo: &FakeFileInfo{
NameValue: "dest",
ModeValue: os.ModeDir,
},
},
src: "/path/to/source",
dest: "/path/to/dest",
expectedError: errors.New("Non-regular destination file dest (\"d---------\")"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "sourcefile",
ModeValue: 0,
},
StatFileInfo: &FakeFileInfo{
NameValue: "destfile",
ModeValue: 0,
},
OpenError: errors.New("Cannot open file sourcefile"),
},
src: "/path/to/sourcefile",
dest: "/path/to/destfile",
expectedError: errors.New("Cannot open file sourcefile"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "source",
ModeValue: 0,
},
StatFileInfo: &FakeFileInfo{
NameValue: "dest",
ModeValue: 0,
},
MkdirAllError: errors.New("Cannot create dir"),
} ,
src: "/path/to/source",
dest: "/path/to/dest",
expectedError: errors.New("Cannot create dir"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
NameValue: "sourcefile",
ModeValue: 0,
},
StatFileInfo: &FakeFileInfo{
NameValue: "destfile",
ModeValue: 0,
},
CreateError: errors.New("Cannot create file destfile"),
},
src: "/path/to/sourcefile",
dest: "/path/to/destfile",
expectedError: errors.New("Cannot create file destfile"),
},
}

// tear down

if err := os.Remove(original); err != nil {
t.Error(err)
}
for _, testcase := range cases {
err := Copy(testcase.os, testcase.src, testcase.dest)

if err := os.Remove(symlink); err != nil {
t.Error(err)
if !reflect.DeepEqual(err, testcase.expectedError) {
t.Errorf("Expected err to be %v but it was %v\n", testcase.expectedError, err)
}
}
}
Loading

0 comments on commit 6f787c2

Please sign in to comment.