Skip to content

Commit

Permalink
wip: fixes relative paths in scan result
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Aug 31, 2024
1 parent e13af54 commit b7e333e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:

jobs:
run:
name: ${{ matrix.earthfile }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand Down
8 changes: 4 additions & 4 deletions forge/cli/cmd/testdata/scan/1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions forge/cli/cmd/testdata/scan/2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions forge/cli/cmd/testdata/scan/3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion forge/cli/pkg/earthfile/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"path/filepath"
"strings"

w "github.com/input-output-hk/catalyst-forge/forge/cli/pkg/walker"
)
Expand Down Expand Up @@ -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
})
Expand Down
11 changes: 7 additions & 4 deletions forge/cli/pkg/earthfile/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ foo2:
`,
},
expectedResult: map[string][]string{
"/tmp1/Earthfile": {"foo1", "foo2"},
"/tmp1": {"foo1", "foo2"},
},
callbackErr: nil,
walkErr: nil,
Expand All @@ -55,8 +55,8 @@ foo2:
`,
},
expectedResult: map[string][]string{
"/tmp1/Earthfile": {"foo1"},
"/tmp2/Earthfile": {"foo2"},
"/tmp1": {"foo1"},
"/tmp2": {"foo2"},
},
callbackErr: nil,
walkErr: nil,
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit b7e333e

Please sign in to comment.