Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Dec 22, 2020
1 parent 5954fcb commit 22f2d7a
Showing 1 changed file with 112 additions and 114 deletions.
226 changes: 112 additions & 114 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,41 +83,6 @@ func TestCopy(t *testing.T) {
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(0)
})

When(t, "symlink with Opt.OnSymlink provided", func(t *testing.T) {
opt := Options{OnSymlink: func(string) SymlinkAction { return Deep }}
err := Copy("testdata/case03", "testdata.copy/case03.deep", opt)
Expect(t, err).ToBe(nil)
info, err := os.Lstat("testdata.copy/case03.deep/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).ToBe(os.FileMode(0))

opt = Options{OnSymlink: func(string) SymlinkAction { return Shallow }}
err = Copy("testdata/case03", "testdata.copy/case03.shallow", opt)
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.shallow/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))

opt = Options{OnSymlink: func(string) SymlinkAction { return Skip }}
err = Copy("testdata/case03", "testdata.copy/case03.skip", opt)
Expect(t, err).ToBe(nil)
_, err = os.Stat("testdata.copy/case03.skip/case01")
Expect(t, os.IsNotExist(err)).ToBe(true)

err = Copy("testdata/case03", "testdata.copy/case03.default")
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.default/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))

opt = Options{OnSymlink: nil}
err = Copy("testdata/case03", "testdata.copy/case03.not-specified", opt)
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.not-specified/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))
})

When(t, "try to copy to an existing path", func(t *testing.T) {
err := Copy("testdata/case03", "testdata.copy/case03")
Expect(t, err).Not().ToBe(nil)
Expand Down Expand Up @@ -149,103 +114,136 @@ func TestCopy(t *testing.T) {
Expect(t, err).ToBe(nil)
})

When(t, "Options.Skip provided", func(t *testing.T) {
opt := Options{Skip: func(src string) (bool, error) {
switch {
case strings.HasSuffix(src, "_skip"):
return true, nil
case strings.HasSuffix(src, ".gitfake"):
return true, nil
default:
return false, nil
}
}}
err := Copy("testdata/case06", "testdata.copy/case06", opt)
Expect(t, err).ToBe(nil)
info, err := os.Stat("./testdata.copy/case06/dir_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
}

info, err = os.Stat("./testdata.copy/case06/file_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
func TestOptions_OnSymlink(t *testing.T) {
opt := Options{OnSymlink: func(string) SymlinkAction { return Deep }}
err := Copy("testdata/case03", "testdata.copy/case03.deep", opt)
Expect(t, err).ToBe(nil)
info, err := os.Lstat("testdata.copy/case03.deep/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).ToBe(os.FileMode(0))

info, err = os.Stat("./testdata.copy/case06/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)
opt = Options{OnSymlink: func(string) SymlinkAction { return Shallow }}
err = Copy("testdata/case03", "testdata.copy/case03.shallow", opt)
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.shallow/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))

info, err = os.Stat("./testdata.copy/case06/repo/.gitfake")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
opt = Options{OnSymlink: func(string) SymlinkAction { return Skip }}
err = Copy("testdata/case03", "testdata.copy/case03.skip", opt)
Expect(t, err).ToBe(nil)
_, err = os.Stat("testdata.copy/case03.skip/case01")
Expect(t, os.IsNotExist(err)).ToBe(true)

info, err = os.Stat("./testdata.copy/case06/repo/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)
err = Copy("testdata/case03", "testdata.copy/case03.default")
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.default/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))

Because(t, "if Skip func returns error, Copy should be interrupted", func(t *testing.T) {
errInsideSkipFunc := errors.New("Something wrong inside Skip")
opt := Options{Skip: func(src string) (bool, error) {
return false, errInsideSkipFunc
}}
err := Copy("testdata/case06", "testdata.copy/case06.01", opt)
Expect(t, err).ToBe(errInsideSkipFunc)
files, err := ioutil.ReadDir("./testdata.copy/case06.01")
Expect(t, err).ToBe(nil)
Expect(t, len(files)).ToBe(0)
})
})
opt = Options{OnSymlink: nil}
err = Copy("testdata/case03", "testdata.copy/case03.not-specified", opt)
Expect(t, err).ToBe(nil)
info, err = os.Lstat("testdata.copy/case03.not-specified/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(os.FileMode(0))
}

When(t, "Options.AddPermission provided", func(t *testing.T) {
func TestOptions_Skip(t *testing.T) {
opt := Options{Skip: func(src string) (bool, error) {
switch {
case strings.HasSuffix(src, "_skip"):
return true, nil
case strings.HasSuffix(src, ".gitfake"):
return true, nil
default:
return false, nil
}
}}
err := Copy("testdata/case06", "testdata.copy/case06", opt)
Expect(t, err).ToBe(nil)
info, err := os.Stat("./testdata.copy/case06/dir_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)

info, err := os.Stat("testdata/case07/dir_0500")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0500) | os.ModeDir)
info, err = os.Stat("./testdata.copy/case06/file_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)

info, err = os.Stat("testdata/case07/file_0444")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0444))
info, err = os.Stat("./testdata.copy/case06/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)

opt := Options{AddPermission: 0200}
err = Copy("testdata/case07", "testdata.copy/case07", opt)
Expect(t, err).ToBe(nil)
info, err = os.Stat("./testdata.copy/case06/repo/.gitfake")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)

info, err = os.Stat("testdata.copy/case07/dir_0500")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0500|0200) | os.ModeDir)
info, err = os.Stat("./testdata.copy/case06/repo/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)

info, err = os.Stat("testdata.copy/case07/file_0444")
Because(t, "if Skip func returns error, Copy should be interrupted", func(t *testing.T) {
errInsideSkipFunc := errors.New("Something wrong inside Skip")
opt := Options{Skip: func(src string) (bool, error) {
return false, errInsideSkipFunc
}}
err := Copy("testdata/case06", "testdata.copy/case06.01", opt)
Expect(t, err).ToBe(errInsideSkipFunc)
files, err := ioutil.ReadDir("./testdata.copy/case06.01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0444 | 0200))
Expect(t, len(files)).ToBe(0)
})
}

When(t, "Options.Sync provided", func(t *testing.T) {
// With Sync option, each file will be flushed to storage on copying.
// TODO: Since it's a bit hard to simulate real usecases here. This testcase is nonsense.
opt := Options{Sync: true}
err = Copy("testdata/case08", "testdata.copy/case08", opt)
Expect(t, err).ToBe(nil)
})
func TestOptions_AddPermission(t *testing.T) {
info, err := os.Stat("testdata/case07/dir_0500")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0500) | os.ModeDir)

When(t, "Options.PreserveTimes provided", func(t *testing.T) {
info, err = os.Stat("testdata/case07/file_0444")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0444))

err = Copy("testdata/case09", "testdata.copy/case09")
Expect(t, err).ToBe(nil)
opt := Options{PreserveTimes: true}
err = Copy("testdata/case09", "testdata.copy/case09-preservetimes", opt)
Expect(t, err).ToBe(nil)
opt := Options{AddPermission: 0200}
err = Copy("testdata/case07", "testdata.copy/case07", opt)
Expect(t, err).ToBe(nil)

for _, entry := range []string{"", "README.md", "symlink"} {
orig, err := os.Stat("testdata/case09/" + entry)
Expect(t, err).ToBe(nil)
plain, err := os.Stat("testdata.copy/case09/" + entry)
Expect(t, err).ToBe(nil)
preserved, err := os.Stat("testdata.copy/case09-preservetimes/" + entry)
Expect(t, err).ToBe(nil)
Expect(t, plain.ModTime().Unix()).Not().ToBe(orig.ModTime().Unix())
Expect(t, preserved.ModTime().Unix()).ToBe(orig.ModTime().Unix())
}
info, err = os.Stat("testdata.copy/case07/dir_0500")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0500|0200) | os.ModeDir)

})
info, err = os.Stat("testdata.copy/case07/file_0444")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0444 | 0200))
}

func TestOptions_Sync(t *testing.T) {
// With Sync option, each file will be flushed to storage on copying.
// TODO: Since it's a bit hard to simulate real usecases here. This testcase is nonsense.
opt := Options{Sync: true}
err := Copy("testdata/case08", "testdata.copy/case08", opt)
Expect(t, err).ToBe(nil)
}

func TestOptions_PreserveTimes(t *testing.T) {
err := Copy("testdata/case09", "testdata.copy/case09")
Expect(t, err).ToBe(nil)
opt := Options{PreserveTimes: true}
err = Copy("testdata/case09", "testdata.copy/case09-preservetimes", opt)
Expect(t, err).ToBe(nil)

for _, entry := range []string{"", "README.md", "symlink"} {
orig, err := os.Stat("testdata/case09/" + entry)
Expect(t, err).ToBe(nil)
plain, err := os.Stat("testdata.copy/case09/" + entry)
Expect(t, err).ToBe(nil)
preserved, err := os.Stat("testdata.copy/case09-preservetimes/" + entry)
Expect(t, err).ToBe(nil)
Expect(t, plain.ModTime().Unix()).Not().ToBe(orig.ModTime().Unix())
Expect(t, preserved.ModTime().Unix()).ToBe(orig.ModTime().Unix())
}
}

func TestOptions_OnDirExists(t *testing.T) {
Expand Down

0 comments on commit 22f2d7a

Please sign in to comment.