From 5ca6ac86ec58ea0a9948a9b78d876e46e4603c03 Mon Sep 17 00:00:00 2001 From: Rafid Bin Mostofa Date: Wed, 29 Nov 2023 12:32:11 +0600 Subject: [PATCH] test(cmd/info): compare YAMLs after parsing 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. --- cmd/chisel/cmd_info_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/chisel/cmd_info_test.go b/cmd/chisel/cmd_info_test.go index 8ce66152..2bdb0380 100644 --- a/cmd/chisel/cmd_info_test.go +++ b/cmd/chisel/cmd_info_test.go @@ -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" @@ -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) @@ -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) } }