test(tour) ensure template renders without failure
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 13, 2014 at 01:09 UTC
ff6c4ce6a429507a997d498ad04050bd1280fe5f
2 files changed
+56
-11
cmd/ipfs2/tour.go
+32
-11
@@ -3,6 +3,7 @@ package main
3
import (
4
"bytes"
5
"fmt"
6
+ "html/template"
7
"io"
8
"os"
9
@@ -45,12 +46,17 @@ IPFS very quickly. To start, run:
46
return nil, err
47
}
48
48
- topic := tour.TopicID(cfg.Tour.Last)
49
+ id := tour.TopicID(cfg.Tour.Last)
50
if len(strs) > 0 {
50
- topic = tour.TopicID(strs[0])
51
+ id = tour.TopicID(strs[0])
52
}
53
53
- err = tourShow(out, topic)
54
+ t, err := tourGet(id)
55
+ if err != nil {
56
+ return nil, err
57
+ }
58
+
59
+ err = tourShow(out, t)
60
if err != nil {
61
return nil, err
62
}
@@ -72,14 +78,18 @@ var cmdIpfsTourNext = &cmds.Command{
78
return nil, err
79
}
80
75
- topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
81
+ id := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
82
+ topic, err := tourGet(id)
83
+ if err != nil {
84
+ return nil, err
85
+ }
86
if err := tourShow(&w, topic); err != nil {
87
return nil, err
88
}
89
90
// topic changed, not last. write it out.
81
- if string(topic) != cfg.Tour.Last {
82
- cfg.Tour.Last = string(topic)
91
+ if string(id) != cfg.Tour.Last {
92
+ cfg.Tour.Last = string(id)
93
err := writeConfig(path, cfg)
94
if err != nil {
95
return nil, err
@@ -147,14 +157,25 @@ func tourListCmd(w io.Writer, cfg *config.Config) {
157
}
158
}
159
150
-func tourShow(w io.Writer, id tour.ID) error {
160
+func tourShow(w io.Writer, t *tour.Topic) error {
161
+ tmpl := `
162
+Tour {{ .ID }} - {{ .Title }}
163
+
164
+{{ .Text }}
165
+ `
166
+ ttempl, err := template.New("tour").Parse(tmpl)
167
+ if err != nil {
168
+ return err
169
+ }
170
+ return ttempl.Execute(w, t)
171
+}
172
+
173
+func tourGet(id tour.ID) (*tour.Topic, error) {
174
t, found := tour.Topics[id]
175
if !found {
153
- return fmt.Errorf("no topic with id: %s", id)
176
+ return nil, fmt.Errorf("no topic with id: %s", id)
177
}
155
-
156
- fmt.Fprintf(w, "Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text)
157
- return nil
178
+ return &t, nil
179
}
180
181
// TODO share func
cmd/ipfs2/tour_test.go
new
+24
@@ -0,0 +1,24 @@
1
+package main
2
+
3
+import (
4
+ "bytes"
5
+ "testing"
6
+
7
+ "github.com/jbenet/go-ipfs/tour"
8
+)
9
+
10
+func TestParseTourTemplate(t *testing.T) {
11
+ topic := &tour.Topic{
12
+ ID: "42",
13
+ Title: "IPFS CLI test files",
14
+ Text: `Welcome to the IPFS test files
15
+ This is where we test our beautiful command line interfaces
16
+ `,
17
+ }
18
+ var buf bytes.Buffer
19
+ err := tourShow(&buf, topic)
20
+ if err != nil {
21
+ t.Fatal(err)
22
+ }
23
+ t.Log(buf.String())
24
+}