Merge lp:~savilerow-team/savilerow/savvy-go-cli-test into lp:savilerow/savvy

Proposed by Chris Wayne
Status: Merged
Merged at revision: 17
Proposed branch: lp:~savilerow-team/savilerow/savvy-go-cli-test
Merge into: lp:savilerow/savvy
Diff against target: 148 lines (+68/-14)
3 files modified
savvy-tailor/add.go (+14/-13)
savvy-tailor/main.go (+42/-0)
savvy-tailor/test.go (+12/-1)
To merge this branch: bzr merge lp:~savilerow-team/savilerow/savvy-go-cli-test
Reviewer Review Type Date Requested Status
Jani Monoses (community) Approve
Review via email: mp+216239@code.launchpad.net

Description of the change

Implement the test command in the CLI app

To post a comment you must log in.
Revision history for this message
Jani Monoses (jani) wrote :

Maybe using filepath.Walk would result in shorter code than doing the IterDirectory by hand. Walk is a bit weird to figure out initially, you need to pass it a closure so it has access to whatever initial setup and variables you have, like the tar writer. You need your own IterDirectory though if you need symlinks included in the tar, which it does not traverse. This is one area where the shell script clearly beats Go (and probably anything else) :)
Is .gz optional, why is it commented out?

18. By Chris Wayne

Switching to filepath.Walk as its cleaner and somewhat less code

Revision history for this message
Chris Wayne (cwayne) wrote :

Just pushed an update to use filepath.Walk instead. As for the .gz, I decided to leave it out so that I can reuse this function when implementing build (which would build a tar.xz) and since the tests themselves aren't really big enough to need gzipping

Revision history for this message
Jani Monoses (jani) wrote :

gzipping should be a separate function from tarring, so the tarring could still be reused with xz.
Anyway not a big deal, approved.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'savvy-tailor/add.go'
2--- savvy-tailor/add.go 2014-04-14 21:58:53 +0000
3+++ savvy-tailor/add.go 2014-04-17 13:58:23 +0000
4@@ -43,7 +43,8 @@
5 exe_cmd(cmd)
6 //This bit is somewhat hacky
7 //Get the click's name and version from click info to rebuild the symlink
8- click_info := exe_cmd("click info " + click)
9+ click_info, err := exe_cmd("click info " + click)
10+ checkErr(err)
11 var clickinfo ClickInfo
12 json.Unmarshal(click_info, &clickinfo)
13 //Remove the symlink to ./src/system/custom/click and remake it to just /custom/click like it
14@@ -61,7 +62,8 @@
15 func AddContent(content, content_type string) {
16 dest := customHome + content_type
17 cmd := fmt.Sprintf("cp %s %s", content, dest)
18- a := exe_cmd(cmd)
19+ a, err := exe_cmd(cmd)
20+ checkErr(err)
21 fmt.Println(string(a))
22 }
23 func AddBookmark(title, url string) {
24@@ -72,17 +74,6 @@
25 checkErr(err)
26 }
27
28-//easy way to exec a command and get output
29-func exe_cmd(cmd string) []byte {
30- parts := strings.Fields(cmd)
31- head := parts[0]
32- parts = parts[1:]
33- comm := exec.Command(head, parts...)
34- outp, err := comm.CombinedOutput()
35- checkErr(err)
36- return outp
37-}
38-
39 func (x *AddCommand) Execute(args []string) error {
40 if len(args) > 0 {
41 add_type := args[0]
42@@ -104,6 +95,16 @@
43 return nil
44 }
45
46+//easy way to exec a command and get output
47+func exe_cmd(cmd string) ([]byte, error) {
48+ parts := strings.Fields(cmd)
49+ head := parts[0]
50+ parts = parts[1:]
51+ comm := exec.Command(head, parts...)
52+ outp, err := comm.CombinedOutput()
53+ return outp, err
54+}
55+
56 func (x *AddCommand) Usage() string {
57 return "[CONTENT_TYPE] file"
58 }
59
60=== modified file 'savvy-tailor/main.go'
61--- savvy-tailor/main.go 2014-04-12 01:02:25 +0000
62+++ savvy-tailor/main.go 2014-04-17 13:58:23 +0000
63@@ -17,9 +17,12 @@
64 package main
65
66 import (
67+ "archive/tar"
68 "fmt"
69+ "io"
70 "os"
71 "os/exec"
72+ "path/filepath"
73 "strings"
74
75 "../gopath/src/gopkg.in/v0/qml"
76@@ -122,3 +125,42 @@
77 window.Wait()
78 return nil
79 }
80+
81+func CreateTar(outFilePath string, inPath string) {
82+ // file write
83+ fw, err := os.Create(outFilePath)
84+ checkErr(err)
85+ defer fw.Close()
86+ // gzip write
87+ //gw := gzip.NewWriter(fw)
88+ //defer gw.Close()
89+ // tar write
90+ tw := tar.NewWriter(fw)
91+ defer tw.Close()
92+ writeTar := func(path string, info os.FileInfo, err error) error {
93+ if info.Mode().IsDir() {
94+ return nil
95+ }
96+ new_path := path[len(inPath):]
97+ if len(new_path) == 0 {
98+ return nil
99+ }
100+ fr, err := os.Open(path)
101+ if err != nil {
102+ return err
103+ }
104+ defer fr.Close()
105+
106+ if h, err := tar.FileInfoHeader(info, new_path); err != nil {
107+ return err
108+ } else {
109+ h.Name = new_path
110+ err = tw.WriteHeader(h)
111+ checkErr(err)
112+ }
113+ _, err = io.Copy(tw, fr)
114+ checkErr(err)
115+ return nil
116+ }
117+ filepath.Walk(inPath, writeTar)
118+}
119
120=== modified file 'savvy-tailor/test.go'
121--- savvy-tailor/test.go 2014-04-12 01:02:25 +0000
122+++ savvy-tailor/test.go 2014-04-17 13:58:23 +0000
123@@ -2,6 +2,7 @@
124
125 import (
126 "fmt"
127+ "launchpad.net/goget-ubuntu-touch/devices"
128 )
129
130 type TestCommand struct {
131@@ -10,7 +11,17 @@
132 var testCommand TestCommand
133
134 func (x *TestCommand) Execute(args []string) error {
135- fmt.Println("test")
136+ CreateTar("test.tar", "tests")
137+ adb, err := devices.NewUbuntuDebugBridge()
138+ adb.Shell("mkdir -p /home/phablet/autopilot/custom")
139+ err = adb.Push("test.tar", "/tmp")
140+ checkErr(err)
141+ adb.Shell("tar xvf /tmp/test.tar -C /home/phablet/autopilot/custom")
142+ resp, err := exe_cmd("phablet-test-run custom")
143+ if err != nil {
144+ fmt.Println("Tests failed!")
145+ }
146+ fmt.Println(string(resp))
147 return nil
148 }
149

Subscribers

People subscribed via source and target branches

to all changes: