Merge lp:~kyrofa/unity-scope-snappy/libertine_parser into lp:~unity-api-team/unity-scope-snappy/trunk

Proposed by Kyle Fazzari
Status: Needs review
Proposed branch: lp:~kyrofa/unity-scope-snappy/libertine_parser
Merge into: lp:~unity-api-team/unity-scope-snappy/trunk
Prerequisite: lp:~kyrofa/unity-scope-snappy/launcher_scope
Diff against target: 226 lines (+181/-2)
7 files modified
Makefile (+3/-2)
libertine/configuration/application.go (+6/-0)
libertine/configuration/configuration.go (+6/-0)
libertine/configuration/container.go (+9/-0)
libertine/fakes/fake_reader.go (+15/-0)
libertine/parser.go (+41/-0)
libertine/parser_test.go (+101/-0)
To merge this branch: bzr merge lp:~kyrofa/unity-scope-snappy/libertine_parser
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Unity API Team Pending
Review via email: mp+266425@code.launchpad.net

Commit message

Add Libertine JSON parser.

Description of the change

Add Libertine JSON parser.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Unmerged revisions

35. By Kyle Fazzari

Add Libertine JSON parser.

34. By Kyle Fazzari

Add basic launcher scope with debian packaging and integration tests.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2015-07-30 15:00:59 +0000
3+++ Makefile 2015-07-30 15:00:59 +0000
4@@ -5,8 +5,9 @@
5 GOFMT=gofmt -w
6
7 TOP_PACKAGE := launchpad.net/unity-scope-snappy
8-EXECUTABLES := launcher store package-management-daemon
9-PACKAGES_TO_TEST := package-management-daemon/daemon \
10+EXECUTABLES := launcher package-management-daemon store
11+PACKAGES_TO_TEST := libertine \
12+ package-management-daemon/daemon \
13 store/actions \
14 store/packages \
15 store/packages/fakes \
16
17=== added directory 'libertine'
18=== added directory 'libertine/configuration'
19=== added file 'libertine/configuration/application.go'
20--- libertine/configuration/application.go 1970-01-01 00:00:00 +0000
21+++ libertine/configuration/application.go 2015-07-30 15:00:59 +0000
22@@ -0,0 +1,6 @@
23+package configuration
24+
25+type Application struct {
26+ Status string `json:"appStatus"`
27+ Name string `json:"packageName"`
28+}
29
30=== added file 'libertine/configuration/configuration.go'
31--- libertine/configuration/configuration.go 1970-01-01 00:00:00 +0000
32+++ libertine/configuration/configuration.go 2015-07-30 15:00:59 +0000
33@@ -0,0 +1,6 @@
34+package configuration
35+
36+type Configuration struct {
37+ DefaultContainer string
38+ ContainerList []Container
39+}
40
41=== added file 'libertine/configuration/container.go'
42--- libertine/configuration/container.go 1970-01-01 00:00:00 +0000
43+++ libertine/configuration/container.go 2015-07-30 15:00:59 +0000
44@@ -0,0 +1,9 @@
45+package configuration
46+
47+type Container struct {
48+ Id string
49+ Name string
50+ Image string
51+ InstallStatus string
52+ InstalledApps []Application
53+}
54
55=== added directory 'libertine/fakes'
56=== added file 'libertine/fakes/fake_reader.go'
57--- libertine/fakes/fake_reader.go 1970-01-01 00:00:00 +0000
58+++ libertine/fakes/fake_reader.go 2015-07-30 15:00:59 +0000
59@@ -0,0 +1,15 @@
60+package fakes
61+
62+import "fmt"
63+
64+type FakeReader struct {
65+ Error bool
66+}
67+
68+func (fake FakeReader) Read(p []byte) (n int, err error) {
69+ if fake.Error {
70+ return 0, fmt.Errorf("Failed at user request")
71+ }
72+
73+ return 1, nil
74+}
75
76=== added file 'libertine/parser.go'
77--- libertine/parser.go 1970-01-01 00:00:00 +0000
78+++ libertine/parser.go 2015-07-30 15:00:59 +0000
79@@ -0,0 +1,41 @@
80+package libertine
81+
82+import (
83+ "fmt"
84+ "launchpad.net/unity-scope-snappy/libertine/configuration"
85+ "encoding/json"
86+ "io"
87+ "io/ioutil"
88+ "os"
89+)
90+
91+type Parser struct {
92+ configuration configuration.Configuration
93+}
94+
95+// ParseFile opens up the given JSON file path and parses it.
96+//
97+// It assumes that the given file was written by Libertine.
98+func (parser *Parser) ParseFile(filePath string) error {
99+ file, err := os.Open(filePath)
100+ if err != nil {
101+ return fmt.Errorf(`Unable to open "%s": %s`, filePath, err)
102+ }
103+
104+ return parser.Parse(file)
105+}
106+
107+// Parse parses the given Libertine JSON.
108+func (parser *Parser) Parse(reader io.Reader) error {
109+ bytes, err := ioutil.ReadAll(reader)
110+ if err != nil {
111+ return fmt.Errorf("Unable to read: %s", err)
112+ }
113+
114+ err = json.Unmarshal(bytes, &parser.configuration)
115+ if err != nil {
116+ return fmt.Errorf(`Unable to parse: %s`, err)
117+ }
118+
119+ return nil
120+}
121
122=== added file 'libertine/parser_test.go'
123--- libertine/parser_test.go 1970-01-01 00:00:00 +0000
124+++ libertine/parser_test.go 2015-07-30 15:00:59 +0000
125@@ -0,0 +1,101 @@
126+package libertine
127+
128+import (
129+ "launchpad.net/unity-scope-snappy/libertine/fakes"
130+ "os"
131+ "io"
132+ "io/ioutil"
133+ "strings"
134+ "testing"
135+)
136+
137+var (
138+ jsonString string
139+ jsonReader io.Reader
140+)
141+
142+func setup() {
143+ jsonString = `{
144+ "containerList": [
145+ {
146+ "id": "wily",
147+ "image": "wily",
148+ "installStatus": "new",
149+ "installedApps": [
150+ {
151+ "appStatus": "new",
152+ "packageName": "foo"
153+ }
154+ ],
155+ "name": "Ubuntu 'Wily Werewolf'"
156+ }
157+ ],
158+ "defaultContainer": "wily"
159+ }`
160+ jsonReader = strings.NewReader(jsonString)
161+}
162+
163+func setupInvalid() {
164+ jsonString = `{
165+ "containerList": "bar"
166+ }`
167+ jsonReader = strings.NewReader(jsonString)
168+}
169+
170+// Test typical ParseFile usage.
171+func TestParseFile(t *testing.T) {
172+ setup()
173+
174+ file, err := ioutil.TempFile("", "unity-scope-snappy")
175+ if err != nil { panic(err) }
176+ defer os.Remove(file.Name())
177+ ioutil.WriteFile(file.Name(), []byte(jsonString), 0644)
178+
179+ var parser Parser
180+ err = parser.ParseFile(file.Name())
181+ if err != nil {
182+ t.Errorf("Unexpected error parsing file: %s", err)
183+ }
184+}
185+
186+// Test that ParseFile handles an invalid file path
187+func TestParseFile_invalidFilePath(t *testing.T) {
188+ var parser Parser
189+ err := parser.ParseFile("/foo/bar/baz")
190+ if err == nil {
191+ t.Error("Expected an error due to invalid file path")
192+ }
193+}
194+
195+// Test typical Parse usage.
196+func TestParse(t *testing.T) {
197+ setup()
198+
199+ var parser Parser
200+ err := parser.Parse(jsonReader)
201+ if err != nil {
202+ t.Errorf("Unexpected error while parsing: %s", err)
203+ }
204+}
205+
206+// Test that Parse handles parsing invalid JSON
207+func TestParse_invalidJson(t *testing.T) {
208+ setupInvalid()
209+
210+ var parser Parser
211+ err := parser.Parse(jsonReader)
212+ if err == nil {
213+ t.Error("Expected an error due to invalid JSON")
214+ }
215+}
216+
217+// Test that Parse handles an error while reading
218+func TestParse_readError(t *testing.T) {
219+ reader := fakes.FakeReader{Error: true}
220+
221+ var parser Parser
222+ err := parser.Parse(reader)
223+ if err == nil {
224+ t.Error("Expected an error due to read error")
225+ }
226+}

Subscribers

People subscribed via source and target branches