2
* Copyright (C) 2014-2015 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28
. "launchpad.net/gocheck"
41
var _ = Suite(&cpSuite{})
43
func (s *cpSuite) mockCopyFile(fin, fout fileish, fi os.FileInfo) error {
44
return s.µ("copyfile")
47
func (s *cpSuite) mockOpenFile(name string, flag int, perm os.FileMode) (fileish, error) {
48
return &mockfile{s}, s.µ("open")
51
func (s *cpSuite) µ(msg string) (err error) {
52
s.log = append(s.log, msg)
63
func (s *cpSuite) SetUpTest(c *C) {
67
s.f1 = filepath.Join(s.dir, "f1")
68
s.f2 = filepath.Join(s.dir, "f2")
69
s.data = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
70
c.Assert(ioutil.WriteFile(s.f1, s.data, 0644), IsNil)
73
func (s *cpSuite) mock() {
74
copyfile = s.mockCopyFile
75
openfile = s.mockOpenFile
78
func (s *cpSuite) TearDownTest(c *C) {
83
func (s *cpSuite) TestCp(c *C) {
84
c.Check(CopyFile(s.f1, s.f2, CopyFlagDefault), IsNil)
85
bs, err := ioutil.ReadFile(s.f2)
87
c.Check(bs, DeepEquals, s.data)
90
func (s *cpSuite) TestCpSync(c *C) {
92
c.Check(CopyFile(s.f1, s.f2, CopyFlagDefault), IsNil)
93
c.Check(strings.Join(s.log, ":"), Not(Matches), `.*:sync(:.*)?`)
96
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), IsNil)
97
c.Check(strings.Join(s.log, ":"), Matches, `(.*:)?sync(:.*)?`)
100
func (s *cpSuite) TestCpCantOpen(c *C) {
102
s.errs = []error{errors.New("xyzzy"), nil}
104
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `unable to open \S+/f1: xyzzy`)
107
func (s *cpSuite) TestCpCantStat(c *C) {
109
s.errs = []error{nil, errors.New("xyzzy"), nil}
111
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `unable to stat \S+/f1: xyzzy`)
114
func (s *cpSuite) TestCpCantCreate(c *C) {
116
s.errs = []error{nil, nil, errors.New("xyzzy"), nil}
118
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `unable to create \S+/f2: xyzzy`)
121
func (s *cpSuite) TestCpCantCopy(c *C) {
123
s.errs = []error{nil, nil, nil, errors.New("xyzzy"), nil}
125
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `unable to copy \S+/f1 to \S+/f2: xyzzy`)
128
func (s *cpSuite) TestCpCantSync(c *C) {
130
s.errs = []error{nil, nil, nil, nil, errors.New("xyzzy"), nil}
132
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `unable to sync \S+/f2: xyzzy`)
135
func (s *cpSuite) TestCpCantStop2(c *C) {
137
s.errs = []error{nil, nil, nil, nil, nil, errors.New("xyzzy"), nil}
139
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `when closing \S+/f2: xyzzy`)
142
func (s *cpSuite) TestCpCantStop1(c *C) {
144
s.errs = []error{nil, nil, nil, nil, nil, nil, errors.New("xyzzy"), nil}
146
c.Check(CopyFile(s.f1, s.f2, CopyFlagSync), ErrorMatches, `when closing \S+/f1: xyzzy`)
149
type mockfile struct {
153
var mockst = mockstat{}
155
func (f *mockfile) Close() error { return f.s.µ("close") }
156
func (f *mockfile) Sync() error { return f.s.µ("sync") }
157
func (f *mockfile) Fd() uintptr { f.s.µ("fd"); return 42 }
158
func (f *mockfile) Read([]byte) (int, error) { return 0, f.s.µ("read") }
159
func (f *mockfile) Write([]byte) (int, error) { return 0, f.s.µ("write") }
160
func (f *mockfile) Stat() (os.FileInfo, error) { return mockst, f.s.µ("stat") }
162
type mockstat struct{}
164
func (mockstat) Name() string { return "mockstat" }
165
func (mockstat) Size() int64 { return 42 }
166
func (mockstat) Mode() os.FileMode { return 0644 }
167
func (mockstat) ModTime() time.Time { return time.Now() }
168
func (mockstat) IsDir() bool { return false }
169
func (mockstat) Sys() interface{} { return nil }