Skip to content

Commit

Permalink
test(cmd/info): compare YAMLs after parsing
Browse files Browse the repository at this point in the history
Instead of checking if the output YAML equals exactly to the expected
YAML by string checking, parse both YAMLs instead. Then compare to see
if they are equal.
  • Loading branch information
rebornplusplus committed Nov 29, 2023
1 parent 5161cc2 commit 5ca6ac8
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/chisel/cmd_info_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main_test

import (
"bytes"
"os"
"path/filepath"
"strings"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"

chisel "github.com/canonical/chisel/cmd/chisel"
"github.com/canonical/chisel/internal/testutil"
Expand Down Expand Up @@ -243,6 +244,8 @@ func (s *ChiselSuite) TestInfoCommand(c *C) {
for _, test := range infoTests {
c.Logf("Summary: %s", test.summary)

s.ResetStdStreams()

dir := c.MkDir()
for path, data := range test.input {
fpath := filepath.Join(dir, path)
Expand All @@ -257,15 +260,24 @@ func (s *ChiselSuite) TestInfoCommand(c *C) {
prefix = append(prefix, "--release", dir)
}
test.query = append(prefix, test.query...)
test.stdout = strings.TrimPrefix(test.stdout, "\n")

_, err := chisel.Parser().ParseArgs(test.query)
if test.err != "" {
c.Assert(err, ErrorMatches, test.err)
continue
} else {
c.Assert(err, IsNil)
}
c.Assert(s.Stdout(), Equals, test.stdout)
s.ResetStdStreams()

// parse the expected and output YAMLs and compare
var testYAML interface{}
dec := yaml.NewDecoder(bytes.NewBuffer([]byte(test.stdout)))
err = dec.Decode(&testYAML)
c.Assert(err, IsNil)
var outputYAML interface{}
dec = yaml.NewDecoder(bytes.NewBuffer([]byte(s.Stdout())))
err = dec.Decode(&outputYAML)
c.Assert(err, IsNil)
c.Assert(testYAML, DeepEquals, outputYAML)
}
}

0 comments on commit 5ca6ac8

Please sign in to comment.