Skip to content

Commit

Permalink
Allow updating golden files of the pkg/generate unit tests (#65)
Browse files Browse the repository at this point in the history
* Simplify unit tests, allow updating

* Rename files

* Add command to changelog

* Fix file names
  • Loading branch information
marians committed Mar 6, 2024
1 parent 53053ab commit fb7f65d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Improve `pkg/generate` unit tests to support updating golden files via `go test ./pkg/generate -update`.

## [0.0.6] - 2024-03-06

### Changed
Expand Down Expand Up @@ -41,7 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.0.1] - 2023-04-28


- Initial release

[Unreleased]: https://github.com/giantswarm/schemadocs/compare/v0.0.6...HEAD
[0.0.6]: https://github.com/giantswarm/schemadocs/compare/v0.0.5...v0.0.6
Expand Down
52 changes: 40 additions & 12 deletions pkg/generate/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package generate

import (
"flag"
"io"
"os"
"testing"

Expand All @@ -10,6 +12,10 @@ import (
pkgerror "github.com/giantswarm/schemadocs/pkg/error"
)

var (
update = flag.Bool("update", false, "update the golden files of this test")
)

func Test_Generator(t *testing.T) {

testCases := []struct {
Expand All @@ -22,13 +28,13 @@ func Test_Generator(t *testing.T) {
{
name: "case 0: Generate markdown from a valid JSON schema",
layout: "default",
schemaPath: "testdata/schema.json",
outputPath: "testdata/output.txt",
schemaPath: "schema.json",
outputPath: "output_tabular.golden",
},
{
name: "case 1: Fail to generate markdown from an existing invalid JSON schema",
layout: "default",
schemaPath: "testdata/schema_invalid.json",
schemaPath: "schema_invalid.json",
expectedErr: pkgerror.InvalidSchemaFile,
},
{
Expand All @@ -39,14 +45,14 @@ func Test_Generator(t *testing.T) {
{
name: "case 3: Generate markdown from a valid JSON schema in linear layout",
layout: "linear",
schemaPath: "testdata/schema.json",
outputPath: "testdata/output-linear.txt",
schemaPath: "schema.json",
outputPath: "output_linear.golden",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
docs, err := Generate(tc.schemaPath, tc.layout)
docs, err := Generate("testdata/"+tc.schemaPath, tc.layout)

if err != nil {
if err != tc.expectedErr && microerror.Cause(err) != tc.expectedErr {
Expand All @@ -55,16 +61,38 @@ func Test_Generator(t *testing.T) {
return
}

expectedResult, err := os.ReadFile(tc.outputPath)
if err != nil {
t.Fatal(err)
}

diff := cmp.Diff(docs, string(expectedResult))
expectedResult := goldenValue(t, tc.outputPath, docs, *update)
diff := cmp.Diff(docs, expectedResult)
if diff != "" {
t.Fatalf("value of generated docs not expected, got: \n %s", diff)
}
})
}

}

func goldenValue(t *testing.T, goldenFile string, actual string, update bool) string {
t.Helper()
goldenPath := "testdata/" + goldenFile

f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644)
if err != nil {
t.Fatalf("Error opening file %s: %s", goldenPath, err)
}
defer f.Close()

if update {
_, err := f.WriteString(actual)
if err != nil {
t.Fatalf("Error writing to file %s: %s", goldenPath, err)
}

return actual
}

content, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Error opening file %s: %s", goldenPath, err)
}
return string(content)
}
File renamed without changes.
File renamed without changes.

0 comments on commit fb7f65d

Please sign in to comment.