Merge lp:~sergiusens/snappy/returnErrIfErr into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Sergio Schvezov on 2015-07-01
Status: Work in progress
Proposed branch: lp:~sergiusens/snappy/returnErrIfErr
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 89 lines (+48/-2)
2 files modified
helpers/helpers.go (+5/-2)
helpers/helpers_test.go (+43/-0)
To merge this branch: bzr merge lp:~sergiusens/snappy/returnErrIfErr
Reviewer Review Type Date Requested Status
Michael Vogt 2015-07-01 Approve on 2015-07-01
Review via email: mp+263510@code.launchpad.net

Commit Message

Return er if an error occurs during copy in helpers

To post a comment you must log in.
Michael Vogt (mvo) :
review: Approve
lp:~sergiusens/snappy/returnErrIfErr updated on 2015-07-01
546. By Sergio Schvezov on 2015-07-01

Adding broken test

Unmerged revisions

546. By Sergio Schvezov on 2015-07-01

Adding broken test

545. By Sergio Schvezov on 2015-07-01

Return err if err

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'helpers/helpers.go'
2--- helpers/helpers.go 2015-06-09 19:11:54 +0000
3+++ helpers/helpers.go 2015-07-01 12:53:02 +0000
4@@ -43,6 +43,9 @@
5
6 var goarch = runtime.GOARCH
7
8+// useful for testing
9+var execCommand = exec.Command
10+
11 func init() {
12 // golang does not init Seed() itself
13 rand.Seed(time.Now().UTC().UnixNano())
14@@ -488,9 +491,9 @@
15 // XXX: we should (eventually) use CopyFile here,
16 // but we need to teach it about preserving
17 // of atime/mtime and permissions
18- output, err := exec.Command("cp", "-va", src, dst).CombinedOutput()
19+ output, err := execCommand("cp", "-va", src, dst).CombinedOutput()
20 if err != nil {
21- fmt.Errorf("Failed to copy %s to %s (%s)", src, dst, output)
22+ return fmt.Errorf("Failed to copy %s to %s (%s)", src, dst, output)
23 }
24 }
25 return nil
26
27=== modified file 'helpers/helpers_test.go'
28--- helpers/helpers_test.go 2015-06-09 19:11:54 +0000
29+++ helpers/helpers_test.go 2015-07-01 12:53:02 +0000
30@@ -32,12 +32,26 @@
31 . "gopkg.in/check.v1"
32 )
33
34+func fakeExecCommand(command string, args ...string) *exec.Cmd {
35+ cs := []string{"-test.run=TestHelperProcess", "--", command}
36+ cs = append(cs, args...)
37+ cmd := exec.Command(os.Args[0], cs...)
38+ cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
39+ return cmd
40+}
41+
42+var TestHelperProcess = func(*testing.T) { panic("mock this") }
43+
44 func Test(t *testing.T) { TestingT(t) }
45
46 type HTestSuite struct{}
47
48 var _ = Suite(&HTestSuite{})
49
50+func (ts *HTestSuite) TearDownSuite(c *C) {
51+ execCommand = exec.Command
52+}
53+
54 func (ts *HTestSuite) TestUnpack(c *C) {
55
56 // setup tmpdir
57@@ -445,3 +459,32 @@
58 c.Check(err, NotNil)
59 c.Check(err, ErrorMatches, ".*permission denied.*")
60 }
61+
62+func (ts *HTestSuite) TestSyncCopyFails(c *C) {
63+ srcDir := c.MkDir()
64+ err := os.MkdirAll(srcDir, 0755)
65+ c.Assert(err, IsNil)
66+
67+ destDir := c.MkDir()
68+ err = os.MkdirAll(destDir, 0755)
69+ c.Assert(err, IsNil)
70+
71+ err = ioutil.WriteFile(filepath.Join(srcDir, "meep"), []byte(nil), 0644)
72+ c.Assert(err, IsNil)
73+
74+ // fakeCopy
75+ TestHelperProcess = func(t *testing.T) {
76+ if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
77+ return
78+ }
79+
80+ fmt.Fprintf(os.Stderr, "Failed to copy")
81+ os.Exit(1)
82+ }
83+ execCommand = fakeExecCommand
84+
85+ // do it
86+ err = RSyncWithDelete(srcDir, destDir)
87+ c.Check(err, NotNil)
88+ c.Check(err, ErrorMatches, "Failed to copy.*")
89+}

Subscribers

People subscribed via source and target branches