Skip to content

Commit 730ca95

Browse files
committed
c/snap, t/main/snap-tasks: fix spread test, add formatMixin
1 parent bd143ac commit 730ca95

4 files changed

Lines changed: 37 additions & 26 deletions

File tree

cmd/snap/cmd_changes.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package main
2121

2222
import (
23-
"encoding/json"
2423
"fmt"
2524
"regexp"
2625
"sort"
@@ -51,17 +50,15 @@ type cmdChanges struct {
5150
type cmdTasks struct {
5251
timeMixin
5352
changeIDMixin
54-
Format string `long:"format" required:"false"`
53+
formatMixin
5554
}
5655

5756
func init() {
5857
addCommand("changes", shortChangesHelp, longChangesHelp,
5958
func() flags.Commander { return &cmdChanges{} }, timeDescs, nil)
6059
addCommand("tasks", shortTasksHelp, longTasksHelp,
6160
func() flags.Commander { return &cmdTasks{} },
62-
changeIDMixinOptDesc.also(timeDescs).also(map[string]string{
63-
"format": i18n.G("Output format (supported: json)."),
64-
}),
61+
changeIDMixinOptDesc.also(timeDescs).also(formatArgsHelp),
6562
changeIDMixinArgDesc).alias = "change"
6663
}
6764

@@ -163,15 +160,9 @@ func (c *cmdTasks) showChange(chid string) error {
163160
return err
164161
}
165162

166-
if c.Format == "json" {
167-
data, err := json.Marshal(chg)
168-
if err != nil {
169-
return err
170-
}
171-
fmt.Fprint(Stdout, string(data))
172-
return nil
173-
} else if c.Format != "" {
174-
return fmt.Errorf(i18n.G("unsupported format: %s"), c.Format)
163+
if c.Format != "text" && c.Format != "" {
164+
err = c.formatNonText(chg)
165+
return err
175166
}
176167

177168
w := tabWriter()

cmd/snap/cmd_changes_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,12 @@ func (s *SnapSuite) TestTasksJSON(c *check.C) {
257257
c.Check(chg.Tasks[0].Summary, check.Equals, "some summary")
258258
c.Check(chg.Tasks[0].Status, check.Equals, "Do")
259259

260+
// If format (which has defined values) gets passed an invalid value, the parser wraps it in `'.
261+
_, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--format=", "42"})
262+
c.Assert(err, check.ErrorMatches, ".*Invalid value `' for option `--format'. Allowed values are: .* or json")
260263
_, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--format=random", "42"})
261-
c.Assert(err, check.ErrorMatches, ".*unsupported format: random")
264+
c.Assert(err, check.ErrorMatches, ".*Invalid value `random' for option `--format'. Allowed values are: .* or json")
265+
262266
}
263267

264268
func (s *SnapSuite) TestNoChanges(c *check.C) {

cmd/snap/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package main
2121

2222
import (
23+
"encoding/json"
2324
"errors"
2425
"fmt"
2526
"io"
@@ -215,6 +216,28 @@ func (ch *clientMixin) setClient(cli *client.Client) {
215216
ch.client = cli
216217
}
217218

219+
type formatMixin struct {
220+
//lint:ignore SA5008 "choice" tag is intentionally duplicated
221+
Format string `long:"format" default:"text" choice:"text" choice:"json"`
222+
}
223+
224+
var formatArgsHelp = map[string]string{
225+
"format": i18n.G("Output format"),
226+
}
227+
228+
func (mx formatMixin) formatNonText(result any) error {
229+
switch mx.Format {
230+
case "json":
231+
data, err := json.Marshal(result)
232+
if err != nil {
233+
return err
234+
}
235+
fmt.Fprintln(Stdout, string(data))
236+
return nil
237+
}
238+
panic(fmt.Sprintf("internal error: invalid format option %q", mx.Format))
239+
}
240+
218241
func firstNonOptionIsRun() bool {
219242
if len(os.Args) < 2 {
220243
return false

tests/main/snap-tasks/tasks.yaml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@ details: |
44
Verifies that the command 'snap tasks' properly lists the tasks
55
associated with a change. Also check the error scenarios.
66
7-
prepare: |
8-
# Install local test snap
9-
"$TESTSTOOLS"/install-local test-snap-with-components
10-
11-
restore: |
12-
snap remove --purge test-snap-tasks || true
13-
147
execute: |
15-
echo "Install component to trigger a change"
16-
snap install test-snap-with-components+comp1
8+
echo "Install snap to trigger a change"
9+
"$TESTSTOOLS"/install-local test-snap-with-components
1710
1811
echo "Get the change ID for the install operation"
19-
CHANGE_ID_1=$(snap changes | grep "Install snap \"test-snap-with-components\"" | awk '{print $1}')
12+
CHANGE_ID=$(snap changes | grep "Install \"test-snap-with-components\" snap" | awk '{print $1}')
2013
2114
# Check that valid JSON output is produced for the change, and that it contains the expected task with the expected state.
2215
echo "Check that 'snap tasks' lists the task for the change (and accepts the --format option)"
23-
snap tasks --format json "$CHANGE_ID_1" | jq -e '.tasks | any(.id == "remove+comp1" and (.status == "Done" or .status == "done" or .state == "done"))' > /dev/null
16+
snap tasks --format json "$CHANGE_ID" | jq -e '.tasks | any(.id == "$CHANGE_ID" and (.status == "Done" or .status == "done" or .state == "done"))' > /dev/null

0 commit comments

Comments
 (0)