From b7e333ebf0e0dab2891c995c0996899ddfbf228c Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Fri, 30 Aug 2024 21:04:14 -0400 Subject: [PATCH] wip: fixes relative paths in scan result --- .github/workflows/run.yml | 1 + forge/cli/cmd/testdata/scan/1.txt | 8 ++++---- forge/cli/cmd/testdata/scan/2.txt | 4 ++-- forge/cli/cmd/testdata/scan/3.txt | 8 ++++---- forge/cli/pkg/earthfile/scan.go | 10 +++++++++- forge/cli/pkg/earthfile/scan_test.go | 11 +++++++---- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index d0bd80f..7420ae2 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -9,6 +9,7 @@ on: jobs: run: + name: ${{ matrix.earthfile }} runs-on: ubuntu-latest strategy: fail-fast: false diff --git a/forge/cli/cmd/testdata/scan/1.txt b/forge/cli/cmd/testdata/scan/1.txt index f28a313..e4cf4df 100644 --- a/forge/cli/cmd/testdata/scan/1.txt +++ b/forge/cli/cmd/testdata/scan/1.txt @@ -11,13 +11,13 @@ exec forge scan --absolute --enumerate . cmpenv stdout golden_2_enum.txt -- golden_1.txt -- -{"Earthfile":["foo","bar"]} +{".":["foo","bar"]} -- golden_1_enum.txt -- -["Earthfile+bar","Earthfile+foo"] +[".+bar",".+foo"] -- golden_2.txt -- -{"$WORK/Earthfile":["foo","bar"]} +{"$WORK":["foo","bar"]} -- golden_2_enum.txt -- -["$WORK/Earthfile+bar","$WORK/Earthfile+foo"] +["$WORK+bar","$WORK+foo"] -- Earthfile -- VERSION 0.7 diff --git a/forge/cli/cmd/testdata/scan/2.txt b/forge/cli/cmd/testdata/scan/2.txt index 6fdeda6..c51e97d 100644 --- a/forge/cli/cmd/testdata/scan/2.txt +++ b/forge/cli/cmd/testdata/scan/2.txt @@ -5,9 +5,9 @@ exec forge scan --enumerate . cmp stdout golden_enum.txt -- golden.txt -- -{"Earthfile":["foo","bar"],"dir1/Earthfile":["foo","bar"],"dir1/dir2/Earthfile":["foo","bar"],"dir3/dir4/dir5/Earthfile":["foo"]} +{".":["foo","bar"],"./dir1":["foo","bar"],"./dir1/dir2":["foo","bar"],"./dir3/dir4/dir5":["foo"]} -- golden_enum.txt -- -["Earthfile+bar","Earthfile+foo","dir1/Earthfile+bar","dir1/Earthfile+foo","dir1/dir2/Earthfile+bar","dir1/dir2/Earthfile+foo","dir3/dir4/dir5/Earthfile+foo"] +[".+bar",".+foo","./dir1+bar","./dir1+foo","./dir1/dir2+bar","./dir1/dir2+foo","./dir3/dir4/dir5+foo"] -- Earthfile -- VERSION 0.7 diff --git a/forge/cli/cmd/testdata/scan/3.txt b/forge/cli/cmd/testdata/scan/3.txt index 9eabac8..dbc9308 100644 --- a/forge/cli/cmd/testdata/scan/3.txt +++ b/forge/cli/cmd/testdata/scan/3.txt @@ -11,13 +11,13 @@ exec forge scan --enumerate --filter check --filter build-\w+ --filter test$ . cmp stdout golden_3_enum.txt -- golden_1.txt -- -{"check":{"Earthfile":["check"],"dir3/dir4/dir5/Earthfile":["check-foo"]}} +{"check":{".":["check"],"./dir3/dir4/dir5":["check-foo"]}} -- golden_2.txt -- -{"build":{"Earthfile":["build"],"dir1/Earthfile":["build"],"dir1/dir2/Earthfile":["build-thing"]},"check":{"Earthfile":["check"],"dir3/dir4/dir5/Earthfile":["check-foo"]}} +{"build":{".":["build"],"./dir1":["build"],"./dir1/dir2":["build-thing"]},"check":{".":["check"],"./dir3/dir4/dir5":["check-foo"]}} -- golden_3.txt -- -{"build-\\w+":{"dir1/dir2/Earthfile":["build-thing"]},"check":{"Earthfile":["check"],"dir3/dir4/dir5/Earthfile":["check-foo"]},"test$":{"dir3/dir4/dir5/Earthfile":["test"]}} +{"build-\\w+":{"./dir1/dir2":["build-thing"]},"check":{".":["check"],"./dir3/dir4/dir5":["check-foo"]},"test$":{"./dir3/dir4/dir5":["test"]}} -- golden_3_enum.txt -- -{"build-\\w+":["dir1/dir2/Earthfile+build-thing"],"check":["Earthfile+check","dir3/dir4/dir5/Earthfile+check-foo"],"test$":["dir3/dir4/dir5/Earthfile+test"]} +{"build-\\w+":["./dir1/dir2+build-thing"],"check":[".+check","./dir3/dir4/dir5+check-foo"],"test$":["./dir3/dir4/dir5+test"]} -- Earthfile -- VERSION 0.7 diff --git a/forge/cli/pkg/earthfile/scan.go b/forge/cli/pkg/earthfile/scan.go index 20ffbef..4208e90 100644 --- a/forge/cli/pkg/earthfile/scan.go +++ b/forge/cli/pkg/earthfile/scan.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "path/filepath" + "strings" w "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/walker" ) @@ -34,7 +35,14 @@ func ScanEarthfiles(rootPath string, walker w.Walker, logger *slog.Logger) (map[ return fmt.Errorf("error parsing %s: %w", path, err) } - earthfiles[filepath.Dir(path)] = earthfile + // We need to drop the Earthfile suffix and make sure relative paths + // include a leading "./" to avoid confusing the Earthly CLI + path = filepath.Dir(path) + if !strings.HasPrefix(rootPath, "/") && path != "." { + path = fmt.Sprintf("./%s", path) + } + + earthfiles[path] = earthfile return nil }) diff --git a/forge/cli/pkg/earthfile/scan_test.go b/forge/cli/pkg/earthfile/scan_test.go index 92097c8..0b1f376 100644 --- a/forge/cli/pkg/earthfile/scan_test.go +++ b/forge/cli/pkg/earthfile/scan_test.go @@ -33,7 +33,7 @@ foo2: `, }, expectedResult: map[string][]string{ - "/tmp1/Earthfile": {"foo1", "foo2"}, + "/tmp1": {"foo1", "foo2"}, }, callbackErr: nil, walkErr: nil, @@ -55,8 +55,8 @@ foo2: `, }, expectedResult: map[string][]string{ - "/tmp1/Earthfile": {"foo1"}, - "/tmp2/Earthfile": {"foo2"}, + "/tmp1": {"foo1"}, + "/tmp2": {"foo2"}, }, callbackErr: nil, walkErr: nil, @@ -108,7 +108,8 @@ foo1: return tt.walkErr }, } - result, err := ScanEarthfiles("", walker, slog.New(slog.NewTextHandler(io.Discard, nil))) + result, err := ScanEarthfiles("/", walker, slog.New(slog.NewTextHandler(io.Discard, nil))) + fmt.Printf("result: %v\n", result) if tt.callbackErr != nil && err == nil { t.Error("expected error, got nil") @@ -124,11 +125,13 @@ foo1: if len(result) != len(tt.expectedResult) { t.Errorf("expected %d earthfiles, got %d", len(tt.expectedResult), len(result)) + return } for path, targets := range tt.expectedResult { if len(result[path].Targets()) != len(targets) { t.Errorf("expected %d targets for %s, got %d", len(targets), path, len(result[path].Targets())) + return } for i, target := range targets {