Skip to content

Commit 8a0ec60

Browse files
authored
Merge pull request #2325 from onflow/holyfuchs/file-resolver-project-root-fallback
Fall back to project root when file resolver cannot find file
2 parents a3170ba + df4a9d8 commit 8a0ec60

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

internal/test/test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,17 @@ func fileResolver(scriptPath string, state *flowkit.State) cdcTests.FileResolver
550550
importFilePath := util.AbsolutePath(scriptPath, path)
551551

552552
content, err := state.ReadFile(importFilePath)
553-
if err != nil {
553+
if err == nil {
554+
return string(content), nil
555+
}
556+
557+
// Fall back to resolving relative to the project root (CWD, where flow.json lives).
558+
projectContent, projectErr := state.ReadFile(filepath.Clean(path))
559+
if projectErr != nil {
554560
return "", err
555561
}
556562

557-
return string(content), nil
563+
return string(projectContent), nil
558564
}
559565
}
560566

internal/test/test_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,29 @@ func TestExecutingTests(t *testing.T) {
255255
assert.NoError(t, result.Results[script.Filename][0].Error)
256256
})
257257

258+
t.Run("with file read from project root when not found relative to test file", func(t *testing.T) {
259+
_, state, rw := util.TestMocks(t)
260+
_ = rw.WriteFile(
261+
tests.SomeFile.Filename,
262+
tests.SomeFile.Source,
263+
os.ModeTemporary,
264+
)
265+
t.Parallel()
266+
267+
// Place the test script in a subdirectory. SomeFile only exists at the
268+
// project root, so the first lookup (relative to the script) will fail
269+
// and the resolver must fall back to the project root.
270+
script := tests.TestScriptWithFileRead
271+
testFiles := map[string][]byte{
272+
"subdir/" + script.Filename: script.Source,
273+
}
274+
result, err := testCode(testFiles, state, flagsTests{})
275+
276+
require.NoError(t, err)
277+
require.Len(t, result.Results, 1)
278+
assert.NoError(t, result.Results["subdir/"+script.Filename][0].Error)
279+
})
280+
258281
t.Run("with code coverage", func(t *testing.T) {
259282
// Setup
260283
_, state, _ := util.TestMocks(t)

0 commit comments

Comments
 (0)