From d1fda8a813ce090cc2a337b14a41fcba58ab4624 Mon Sep 17 00:00:00 2001 From: plastikfan Date: Fri, 27 Sep 2024 11:05:16 +0100 Subject: [PATCH] test(laboratory): fix test utils (#193) --- internal/filtering/filter-regex_test.go | 2 +- internal/laboratory/test-utilities.go | 66 +++++++------------------ 2 files changed, 20 insertions(+), 48 deletions(-) diff --git a/internal/filtering/filter-regex_test.go b/internal/filtering/filter-regex_test.go index 2b8de16..289e8dc 100644 --- a/internal/filtering/filter-regex_test.go +++ b/internal/filtering/filter-regex_test.go @@ -290,11 +290,11 @@ var _ = Describe("feature", Ordered, func() { Relative: "", Subscription: enums.SubscribeFolders, Mandatory: []string{"PROGRESSIVE-HOUSE"}, + Prohibited: []string{"Blue Amazon", "The Javelin"}, ExpectedNoOf: lab.Quantities{ Files: 0, Folders: 1, }, - Prohibited: []string{"Blue Amazon", "The Javelin"}, }, Description: "top items that contain 'HOUSE'", Pattern: "HOUSE", diff --git a/internal/laboratory/test-utilities.go b/internal/laboratory/test-utilities.go index 53dbfd5..46ac102 100644 --- a/internal/laboratory/test-utilities.go +++ b/internal/laboratory/test-utilities.go @@ -2,15 +2,30 @@ package lab import ( "fmt" - "os" "os/exec" "path/filepath" "strings" ) +// Path creates a path from the parent combined with the relative path. The relative +// path is a file system path so should only contain forward slashes, not the standard +// file path separator as denoted by filepath.Separator, typically used when interacting +// with the local file system. Do not use trailing "/". func Path(parent, relative string) string { - segments := strings.Split(relative, "/") - return filepath.Join(append([]string{parent}, segments...)...) + if relative == "" { + return parent + } + + return parent + "/" + relative +} + +// Repo gets the path of the repo with relative joined on +func Repo(relative string) string { + cmd := exec.Command("git", "rev-parse", "--show-toplevel") + output, _ := cmd.Output() + repo := strings.TrimSpace(string(output)) + + return Path(repo, relative) } func Normalise(p string) string { @@ -25,49 +40,6 @@ func Reason(name string) string { return fmt.Sprintf("❌ for item named: '%v'", name) } -func BecauseQuantity(name string, expected, actual int) string { - return fmt.Sprintf("❌ incorrect no of items for: '%v', expected: '%v', actual: '%v'", - name, expected, actual, - ) -} - -func JoinCwd(segments ...string) string { - if current, err := os.Getwd(); err == nil { - parent, _ := filepath.Split(current) - grand := filepath.Dir(parent) - great := filepath.Dir(grand) - all := append([]string{great}, segments...) - - return filepath.Join(all...) - } - - panic("could not get root path") -} - -func Root() string { - if current, err := os.Getwd(); err == nil { - return current - } - - panic("could not get root path") -} - -func Repo(relative string) string { - cmd := exec.Command("git", "rev-parse", "--show-toplevel") - output, _ := cmd.Output() - repo := strings.TrimSpace(string(output)) - - return Path(repo, relative) -} - func Log() string { - if current, err := os.Getwd(); err == nil { - parent, _ := filepath.Split(current) - grand := filepath.Dir(parent) - great := filepath.Dir(grand) - - return filepath.Join(great, "Test", "test.log") - } - - panic("could not get root path") + return Repo("Test/test.log") }