Skip to content

Commit d3dbb19

Browse files
committed
assert: make YAML dependency pluggable via build tags
Make the YAML dependency required for {assert,require}.YAMLEq{,f} pluggable. The implementation can be selected using build tags: - testify_yaml_default (default): gopkg.in/yaml.v3 is used, like before - testify_yaml_fail: YAML deserialization is not implemented and always fails. So assert.YAMLEq always fails. This is useful if the test suite package doesn't use assert.YAMLEq (very common case). - testify_yaml_custom: the github.com/stretchr/testify/assert/yaml package exposes an Unmarshal variable of type func([]byte, any) error (same as gopkg.in/yaml.v3) that allows to plug any alternate implementation. For example github.com/goccy/go-yaml.Unmarshal. This allows to avoid the link constraints of the license of gopkg.in/yaml.v3 (see PR #1120). Usage: go test -tags testify_yaml_fail To install the alternate implementation with testify_yaml_custom: //go:build testify_yaml_custom package my_pkg_test import ( goyaml "github.com/goccy/go-yaml" "github.com/stretchr/testify/assert/yaml" ) func init() { yaml.Unmarshal = goyaml.Unmarshal }
1 parent be3fbeb commit d3dbb19

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

‎assert/assertions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919

2020
"github.com/davecgh/go-spew/spew"
2121
"github.com/pmezard/go-difflib/difflib"
22-
"gopkg.in/yaml.v3"
22+
23+
// Wrapper around gopkg.in/yaml.v3
24+
"github.com/stretchr/testify/assert/yaml"
2325
)
2426

2527
//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl"

‎assert/yaml/yaml_custom.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build testify_yaml_custom && !testify_yaml_fail && !testify_yaml_default
2+
// +build testify_yaml_custom,!testify_yaml_fail,!testify_yaml_default
3+
4+
// Package yaml is an implementation of YAML functions that calls a pluggable implementation.
5+
//
6+
// This implementation is selected with the testify_yaml_custom build tag.
7+
//
8+
// go test -tags testify_yaml_custom
9+
//
10+
// This implementation can be used at build time to replace the default implementation
11+
// to avoid linking with [gopkg.in/yaml.v3].
12+
//
13+
// In your test package:
14+
//
15+
// import assertYaml "github.com/stretchr/testify/assert/yaml"
16+
//
17+
// func init() {
18+
// assertYaml.Unmarshall = func (in []byte, out interface{}) error {
19+
// // ...
20+
// return nil
21+
// }
22+
// }
23+
package yaml
24+
25+
var Unmarshal func(in []byte, out interface{}) error

‎assert/yaml/yaml_default.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build !testify_yaml_fail && !testify_yaml_custom
2+
// +build !testify_yaml_fail,!testify_yaml_custom
3+
4+
// Package yaml is just an indirection to handle YAML deserialization.
5+
//
6+
// This package is just an indirection that allows the builder to override the
7+
// indirection with an alternative implementation of this package that uses
8+
// another implemantation of YAML deserialization. This allows to not either not
9+
// use YAML deserialization at all, or to use another implementation than
10+
// [gopkg.in/yaml.v3] (for example for license compatibility reasons, see [PR #1120]).
11+
//
12+
// Alternative implementations are selected using build tags:
13+
//
14+
// - testify_yaml_fail: [Unmarshal] always fails with an error
15+
// - testify_yaml_custom: [Unmarshal] is a variable. Caller must initialize it
16+
// before calling any of [github.com/stretchr/testify/assert.YAMLEq] or
17+
// [github.com/stretchr/testify/assert.YAMLEqf].
18+
//
19+
// Usage:
20+
//
21+
// go test -tags testify_yaml_fail
22+
//
23+
// You can check with "go list" which implementation is linked:
24+
//
25+
// go list -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml
26+
// go list -tags testify_yaml_fail -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml
27+
// go list -tags testify_yaml_custom -f '{{.Imports}}' github.com/stretchr/testify/assert/yaml
28+
//
29+
// [PR #1120]: https://github.com/stretchr/testify/pull/1120
30+
package yaml
31+
32+
import goyaml "gopkg.in/yaml.v3"
33+
34+
// Unmarshal is just a wrapper of [gopkg.in/yaml.v3.Unmarshal].
35+
func Unmarshal(in []byte, out interface{}) error {
36+
return goyaml.Unmarshal(in, out)
37+
}

‎assert/yaml/yaml_fail.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build testify_yaml_fail && !testify_yaml_custom && !testify_yaml_default
2+
// +build testify_yaml_fail,!testify_yaml_custom,!testify_yaml_default
3+
4+
// Package yaml is an implementation of YAML functions that always fail.
5+
//
6+
// This implementation can be used at build time to replace the default implementation
7+
// to avoid linking with [gopkg.in/yaml.v3]:
8+
//
9+
// go test -tags testify_yaml_fail
10+
package yaml
11+
12+
import "errors"
13+
14+
var errNotImplemented = errors.New("YAML functions are not available (see https://pkg.go.dev/github.com/stretchr/testify/assert/yaml)")
15+
16+
func Unmarshal([]byte, interface{}) error {
17+
return errNotImplemented
18+
}

0 commit comments

Comments
 (0)