Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit 4656fa9

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]: #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 026e06d commit 4656fa9

File tree

7 files changed

+76
-17
lines changed

7 files changed

+76
-17
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
TESTS = auto check diagnostic failing known skip todo writer yaml
2-
GOPATH = $(CURDIR)/gopath
2+
GOPATH ?= $(CURDIR)/gopath
33

44
.PHONY: $(TESTS)
55

@@ -9,8 +9,8 @@ all: $(foreach t,$(TESTS),test/$(t)/test)
99
clean:
1010
rm -f test/*/test
1111

12-
test/%/test: test/%/main.go tap.go
13-
go build -o $@ $<
12+
test/%/test: test/%/*.go tap.go yaml_json.go yaml_yaml.go
13+
go build -o $@ -tags yaml ./test/$*
1414

1515
$(TESTS): %: test/%/test
1616
prove -v -e '' test/$@/test

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
}

test/yaml/json.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build !yaml
2+
3+
package main
4+
5+
const expected = `TAP version 13
6+
1..2
7+
ok 1 - test for anchoring the YAML block
8+
---
9+
{
10+
"code": 3,
11+
"message": "testing YAML blocks"
12+
}
13+
...
14+
`

test/yaml/main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,3 @@ func main() {
2424
got := buf.String()
2525
t.Ok(got == expected, "diagnostics gave expected output")
2626
}
27-
28-
const expected = `TAP version 13
29-
1..2
30-
ok 1 - test for anchoring the YAML block
31-
---
32-
{
33-
"code": 3,
34-
"message": "testing YAML blocks"
35-
}
36-
...
37-
`

test/yaml/yaml.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build yaml
2+
3+
package main
4+
5+
const expected = `TAP version 13
6+
1..2
7+
ok 1 - test for anchoring the YAML block
8+
---
9+
code: 3
10+
message: testing YAML blocks
11+
...
12+
`

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+
}

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)