From 99e8f993939c8cf5a0bc97bf4b69b623ba3bd22f Mon Sep 17 00:00:00 2001 From: Derrick Gold Date: Fri, 12 Nov 2021 22:05:47 -0800 Subject: [PATCH] YAML anchors in fixture file can now be referenced in test files --- README.md | 3 ++- cmd/arp/main.go | 5 +++-- suite.go | 32 +++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6c1009d..068ab83 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. \ No newline at end of file diff --git a/cmd/arp/main.go b/cmd/arp/main.go index 0dcefa3..346aebb 100644 --- a/cmd/arp/main.go +++ b/cmd/arp/main.go @@ -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'") @@ -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 := "" diff --git a/suite.go b/suite.go index 641cb8c..8cf4cb5 100755 --- a/suite.go +++ b/suite.go @@ -2,6 +2,7 @@ package arp import ( "fmt" + "io" "os" "path/filepath" "strings" @@ -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 @@ -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 { @@ -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) }