Skip to content

Commit

Permalink
YAML anchors in fixture file can now be referenced in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
DerrickGold committed Nov 13, 2021
1 parent 5b64572 commit 99e8f99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Usage of ./arp:
-file string
Path to an individual test file to execute.
-fixtures string
Path to yaml file with data to include into the test scope via test variables.
Path to yaml file with data to include into the test scope via test variables. This file is also merged with each test file such that any YAML anchors defined within it are available for reference in the test files.
-short
Print a short report for executed tests containing only the validation results. (default true)
-short-fail
Expand Down Expand Up @@ -1497,3 +1497,4 @@ tests:
- *any-string
```

The fixture and test files are concatenated together on load making anchors within the fixtures file available for reference in your test cases as well. This can be used to provide common anchors that can be used across all test files without having to redefine them in each test file.
5 changes: 3 additions & 2 deletions cmd/arp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (p *ProgramArgs) Init() {
p.Colorize = flag.Bool("colors", true, "Print test report with colors.")
p.ErrorsOnly = flag.Bool("error-report", false, "Generate a test report that only contain failing test results.")
p.TestFile = flag.String("file", "", "Path to an individual test file to execute.")
p.Fixtures = flag.String("fixtures", "", "Path to yaml file with data to include into the test scope via test variables.")
p.Fixtures = flag.String("fixtures", "", "Path to yaml file with data to include into the test scope via test variables. "+
"This file is also merged with each test file such that any YAML anchors defined within it are available for reference in the test files.")
p.Short = flag.Bool("short", true, "Print a short report for executed tests containing only the validation results.")
p.ShortErrors = flag.Bool("short-fail", false, "Keep the report short when errors are encountered rather than expanding with details.")
p.Interactive = flag.Bool("step", false, "Run tests in interactive mode. Requires a test file to be provided with '-file'")
Expand Down Expand Up @@ -365,7 +366,7 @@ func interactiveMode(args ProgramArgs) bool {
loaded := false

for !loaded {
loaded, err = suite.ReloadFile(*args.TestFile)
loaded, err = suite.ReloadFile(*args.TestFile, *args.Fixtures)
if err != nil {
fmt.Printf("Hot reload error: %v\nPlease correct your file then press 'enter' to reload...\n", err)
input := ""
Expand Down
32 changes: 27 additions & 5 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arp

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +47,7 @@ func NewTestSuite(testFile string, fixtures string) (*TestSuite, error) {
return suite, err
}

status, err := suite.LoadTests(testFile)
status, err := suite.LoadTests(testFile, fixtures)

if !status && err == nil {
return nil, nil
Expand All @@ -57,9 +58,9 @@ func NewTestSuite(testFile string, fixtures string) (*TestSuite, error) {
return suite, nil
}

func (t *TestSuite) ReloadFile(testFile string) (bool, error) {
func (t *TestSuite) ReloadFile(testFile string, fixtures string) (bool, error) {
t.Tests = make([]*TestCase, 0)
return t.LoadTests(testFile)
return t.LoadTests(testFile, fixtures)
}

func (t *TestSuite) InitializeDataStore(fixtures string) error {
Expand Down Expand Up @@ -115,8 +116,29 @@ func (t *TestSuite) Close() {
}
}

func (t *TestSuite) LoadTests(testFile string) (bool, error) {
data, err := os.ReadFile(testFile)
func (t *TestSuite) LoadTests(testFile string, fixtures string) (bool, error) {
var readers []io.Reader

if fixtures != "" {
fix, err := os.Open(fixtures)
if err != nil {
return false, fmt.Errorf("failed to open fixture file: %v - %v", fixtures, err)
}

readers = append(readers, fix)
}

tests, err := os.Open(testFile)
if err != nil {
return false, fmt.Errorf("failed to open test file: %v - %v", testFile, err)
}
readers = append(readers, tests)

// combine fixtures and test file into a single source so tests can utilize yaml anchors defined in
// the fixtures file
multiReader := io.MultiReader(readers...)

data, err := io.ReadAll(multiReader)
if err != nil {
return false, fmt.Errorf("failed to load test file: %v - %v", testFile, err)
}
Expand Down

0 comments on commit 99e8f99

Please sign in to comment.