forked from opencontainers/runtime-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* yaml: Add 'yaml' build tag for conditionally using gopkg.in/yaml.v2 Add YAML() for generating YAML blocks
- Loading branch information
Showing
7 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// +build !yaml | ||
|
||
package main | ||
|
||
const expected = `TAP version 13 | ||
1..2 | ||
ok 1 - test for anchoring the YAML block | ||
--- | ||
{ | ||
"code": 3, | ||
"message": "testing YAML blocks" | ||
} | ||
... | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
|
||
tap "github.com/mndrix/tap-go" | ||
) | ||
|
||
func main() { | ||
// collect output for comparison later | ||
buf := new(bytes.Buffer) | ||
t := tap.New() | ||
t.Writer = io.MultiWriter(os.Stdout, buf) | ||
|
||
t.Header(2) | ||
t.Pass("test for anchoring the YAML block") | ||
message := map[string]interface{}{ | ||
"message": "testing YAML blocks", | ||
"code": 3, | ||
} | ||
t.YAML(message) | ||
got := buf.String() | ||
t.Ok(got == expected, "diagnostics gave expected output") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build yaml | ||
|
||
package main | ||
|
||
const expected = `TAP version 13 | ||
1..2 | ||
ok 1 - test for anchoring the YAML block | ||
--- | ||
code: 3 | ||
message: testing YAML blocks | ||
... | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// +build !yaml | ||
|
||
package tap | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// yaml serializes a message to YAML. This implementation uses JSON, | ||
// which is a subset of YAML [1] and is implemented by Go's standard | ||
// library. | ||
// | ||
// [1]: http://www.yaml.org/spec/1.2/spec.html#id2759572 | ||
func yaml(message interface{}, prefix string) (marshaled []byte, err error) { | ||
marshaled, err = json.MarshalIndent(message, prefix, " ") | ||
if err != nil { | ||
return marshaled, err | ||
} | ||
|
||
marshaled = append(marshaled, []byte("\n")...) | ||
return marshaled, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// +build yaml | ||
|
||
package tap | ||
|
||
import ( | ||
"bytes" | ||
|
||
goyaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
// yaml serializes a message to YAML. This implementation uses | ||
// non-JSON YAML, which has better prove support [1]. | ||
// | ||
// [1]: https://rt.cpan.org/Public/Bug/Display.html?id=121606 | ||
func yaml(message interface{}, prefix string) (marshaled []byte, err error) { | ||
marshaled, err = goyaml.Marshal(message) | ||
if err != nil { | ||
return marshaled, err | ||
} | ||
|
||
marshaled = bytes.Replace(marshaled, []byte("\n"), []byte("\n"+prefix), -1) | ||
return marshaled[:len(marshaled)-len(prefix)], err | ||
} |