Skip to content

Commit 29a1d63

Browse files
committed
Add 'yaml' build tag for conditionally using gopkg.in/yaml.v2
So folks who are comfortable with the additional dependency can get prove-compatible output. Michael wants to stick with prove for tap-go because it is widely installed [1], but folks using node-tap or other consumers that can handle the JSON subset of YAML probably don't want the external dependency. Folks who want yaml.v2 can enable the yaml build tag [2]. Folks who are ok with JSON don't have to set any build tags. The go-yaml dependency is the only Go producer listed in [3]. There may be others, I haven't checked. The Makefile changes include new uses of wildcards [4] to pick up test/%/*.go siblings. And I'm using the stem variable $* [5] in the rule to pick up the whole test package (and not just main.go). I'm not sure how Michael wants vendoring to work. For the moment, I've softened the 'GOPATH =' to a 'GOPATH ?=' and installed the package in my local GOPATH. It's possible that we want to stick with 'GOPATH =' and drop the package under gopath/ (via a Git submodule?). [1]: mndrix/tap-go#7 (comment) [2]: https://golang.org/pkg/go/build/#hdr-Build_Constraints [3]: http://yaml.org/ [4]: https://www.gnu.org/software/make/manual/html_node/Wildcard-Examples.html [5]: https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
1 parent 7e9d1ac commit 29a1d63

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

util/tap.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package tap // import "github.com/mndrix/tap-go"
2222

2323
import (
24-
"encoding/json"
2524
"fmt"
2625
"io"
2726
"os"
@@ -155,12 +154,12 @@ func (t *T) Diagnosticf(format string, a ...interface{}) {
155154

156155
// YAML generates a YAML block from the message.
157156
func (t *T) YAML(message interface{}) error {
158-
bytes, err := json.MarshalIndent(message, " ", " ")
157+
bytes, err := yaml(message, " ")
159158
if err != nil {
160159
return err
161160
}
162161
t.printf(" ---\n ")
163162
t.printf(string(bytes))
164-
t.printf("\n ...\n")
163+
t.printf(" ...\n")
165164
return nil
166165
}

util/yaml_json.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// +build !yaml
2+
3+
package tap
4+
5+
import (
6+
"encoding/json"
7+
)
8+
9+
// yaml serializes a message to YAML. This implementation uses JSON,
10+
// which is a subset of YAML [1] and is implemented by Go's standard
11+
// library.
12+
//
13+
// [1]: http://www.yaml.org/spec/1.2/spec.html#id2759572
14+
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
15+
marshaled, err = json.MarshalIndent(message, prefix, " ")
16+
if err != nil {
17+
return marshaled, err
18+
}
19+
20+
marshaled = append(marshaled, []byte("\n")...)
21+
return marshaled, err
22+
}

util/yaml_yaml.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// +build yaml
2+
3+
package tap
4+
5+
import (
6+
"bytes"
7+
8+
goyaml "gopkg.in/yaml.v2"
9+
)
10+
11+
// yaml serializes a message to YAML. This implementation uses
12+
// non-JSON YAML, which has better prove support [1].
13+
//
14+
// [1]: https://rt.cpan.org/Public/Bug/Display.html?id=121606
15+
func yaml(message interface{}, prefix string) (marshaled []byte, err error) {
16+
marshaled, err = goyaml.Marshal(message)
17+
if err != nil {
18+
return marshaled, err
19+
}
20+
21+
marshaled = bytes.Replace(marshaled, []byte("\n"), []byte("\n"+prefix), -1)
22+
return marshaled[:len(marshaled)-len(prefix)], err
23+
}

0 commit comments

Comments
 (0)