diff --git a/features/features_test.go b/features/features_test.go index d2ac895d14..5031783327 100644 --- a/features/features_test.go +++ b/features/features_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + "github.com/EarthBuild/earthbuild/internal/earthfile" "github.com/stretchr/testify/require" ) @@ -36,6 +37,21 @@ func TestMustParseVersion(t *testing.T) { } } +func TestGetRejectsFeatureFlagOverride(t *testing.T) { + t.Parallel() + + // A bool feature flag given an explicit argument must be rejected. Mirrors + // the invalid-feature-flag-override.earth integration fixture end-to-end: + // the parser accepts the VERSION line (0.6 is a valid version), and Get + // rejects the malformed flag when it parses the VERSION arguments. + tree, err := earthfile.Parse("Earthfile", "VERSION --referenced-save-only=false 0.6") + require.NoError(t, err) + + _, _, err = Get(tree.Version) + require.Error(t, err) + require.ErrorContains(t, err, "bool flag `--referenced-save-only' cannot have an argument") +} + func TestFeaturesStringEnabled(t *testing.T) { t.Parallel() diff --git a/tests/version/comment-and-whitespace-before-version.earth b/internal/earthfile/testdata/version/comment-and-whitespace-before-version.earth similarity index 100% rename from tests/version/comment-and-whitespace-before-version.earth rename to internal/earthfile/testdata/version/comment-and-whitespace-before-version.earth diff --git a/tests/version/comment-then-version.earth b/internal/earthfile/testdata/version/comment-then-version.earth similarity index 100% rename from tests/version/comment-then-version.earth rename to internal/earthfile/testdata/version/comment-then-version.earth diff --git a/tests/version/invalid-feature-flag-override.earth b/internal/earthfile/testdata/version/invalid-feature-flag-override.earth similarity index 100% rename from tests/version/invalid-feature-flag-override.earth rename to internal/earthfile/testdata/version/invalid-feature-flag-override.earth diff --git a/internal/earthfile/testdata/version/invalid-format-version.earth b/internal/earthfile/testdata/version/invalid-format-version.earth new file mode 100644 index 0000000000..02a4055325 --- /dev/null +++ b/internal/earthfile/testdata/version/invalid-format-version.earth @@ -0,0 +1 @@ +VERSION 0.8 --try # we should consider making this format valid, but for now it isn't and we should test it diff --git a/tests/version/invalid-major-version.earth b/internal/earthfile/testdata/version/invalid-major-version.earth similarity index 100% rename from tests/version/invalid-major-version.earth rename to internal/earthfile/testdata/version/invalid-major-version.earth diff --git a/tests/version/invalid-minor-version.earth b/internal/earthfile/testdata/version/invalid-minor-version.earth similarity index 100% rename from tests/version/invalid-minor-version.earth rename to internal/earthfile/testdata/version/invalid-minor-version.earth diff --git a/tests/version/invalid-patch-version.earth b/internal/earthfile/testdata/version/invalid-patch-version.earth similarity index 100% rename from tests/version/invalid-patch-version.earth rename to internal/earthfile/testdata/version/invalid-patch-version.earth diff --git a/tests/version/multi-line-with-args.earth b/internal/earthfile/testdata/version/multi-line-with-args.earth similarity index 100% rename from tests/version/multi-line-with-args.earth rename to internal/earthfile/testdata/version/multi-line-with-args.earth diff --git a/tests/version/multi-line-with-args2.earth b/internal/earthfile/testdata/version/multi-line-with-args2.earth similarity index 100% rename from tests/version/multi-line-with-args2.earth rename to internal/earthfile/testdata/version/multi-line-with-args2.earth diff --git a/tests/version/multi-line-with-comment.earth b/internal/earthfile/testdata/version/multi-line-with-comment.earth similarity index 100% rename from tests/version/multi-line-with-comment.earth rename to internal/earthfile/testdata/version/multi-line-with-comment.earth diff --git a/tests/version/multi-line-with-comment2.earth b/internal/earthfile/testdata/version/multi-line-with-comment2.earth similarity index 100% rename from tests/version/multi-line-with-comment2.earth rename to internal/earthfile/testdata/version/multi-line-with-comment2.earth diff --git a/tests/version/multi-line-with-comment3.earth b/internal/earthfile/testdata/version/multi-line-with-comment3.earth similarity index 100% rename from tests/version/multi-line-with-comment3.earth rename to internal/earthfile/testdata/version/multi-line-with-comment3.earth diff --git a/tests/version/multi-line-with-comment4.earth b/internal/earthfile/testdata/version/multi-line-with-comment4.earth similarity index 100% rename from tests/version/multi-line-with-comment4.earth rename to internal/earthfile/testdata/version/multi-line-with-comment4.earth diff --git a/tests/version/multi-line-with-empty-newline.earth b/internal/earthfile/testdata/version/multi-line-with-empty-newline.earth similarity index 100% rename from tests/version/multi-line-with-empty-newline.earth rename to internal/earthfile/testdata/version/multi-line-with-empty-newline.earth diff --git a/tests/version/multi-line.earth b/internal/earthfile/testdata/version/multi-line.earth similarity index 100% rename from tests/version/multi-line.earth rename to internal/earthfile/testdata/version/multi-line.earth diff --git a/tests/version/single-line-with-args.earth b/internal/earthfile/testdata/version/single-line-with-args.earth similarity index 100% rename from tests/version/single-line-with-args.earth rename to internal/earthfile/testdata/version/single-line-with-args.earth diff --git a/tests/version/single-line-with-comment.earth b/internal/earthfile/testdata/version/single-line-with-comment.earth similarity index 100% rename from tests/version/single-line-with-comment.earth rename to internal/earthfile/testdata/version/single-line-with-comment.earth diff --git a/internal/earthfile/testdata/version/single-line.earth b/internal/earthfile/testdata/version/single-line.earth new file mode 100644 index 0000000000..9556194ce3 --- /dev/null +++ b/internal/earthfile/testdata/version/single-line.earth @@ -0,0 +1,5 @@ +VERSION 0.8 + +FROM alpine:3.24.0 +test: + RUN echo "pass" diff --git a/internal/earthfile/testdata/version/version-only-import.earth b/internal/earthfile/testdata/version/version-only-import.earth new file mode 100644 index 0000000000..3ed1e5dac2 --- /dev/null +++ b/internal/earthfile/testdata/version/version-only-import.earth @@ -0,0 +1,6 @@ +VERSION 0.8 +IMPORT ./subdir AS empty-earthfile-only-containing-a-version + +test: + FROM alpine:3.24.0 + RUN echo "pass" diff --git a/internal/earthfile/testdata/version/version-only.earth b/internal/earthfile/testdata/version/version-only.earth new file mode 100644 index 0000000000..a8078d524a --- /dev/null +++ b/internal/earthfile/testdata/version/version-only.earth @@ -0,0 +1 @@ +VERSION 0.8 diff --git a/tests/version/whitespace-then-version.earth b/internal/earthfile/testdata/version/whitespace-then-version.earth similarity index 100% rename from tests/version/whitespace-then-version.earth rename to internal/earthfile/testdata/version/whitespace-then-version.earth diff --git a/internal/earthfile/version_test.go b/internal/earthfile/version_test.go index 98393712e6..b7d36a52b8 100644 --- a/internal/earthfile/version_test.go +++ b/internal/earthfile/version_test.go @@ -1,6 +1,7 @@ package earthfile import ( + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -25,3 +26,63 @@ func TestParseVersionFile_Error(t *testing.T) { r.Error(err) r.ErrorContains(err, "earthfile: unable to open file") } + +func TestVersionFixtures(t *testing.T) { + t.Parallel() + + validFixtures := []string{ + "single-line.earth", + "single-line-with-args.earth", + "single-line-with-comment.earth", + "multi-line.earth", + "multi-line-with-comment.earth", + "multi-line-with-comment2.earth", + "multi-line-with-comment3.earth", + "multi-line-with-comment4.earth", + "multi-line-with-args.earth", + "multi-line-with-args2.earth", + "multi-line-with-empty-newline.earth", + "version-only-import.earth", + "version-only.earth", + "comment-and-whitespace-before-version.earth", + "whitespace-then-version.earth", + } + + for _, fixture := range validFixtures { + t.Run(fixture, func(t *testing.T) { + t.Parallel() + + tree, err := ParseFile(filepath.Join("testdata", "version", fixture)) + require.NoError(t, err) + require.NotNil(t, tree.Version) + }) + } +} + +func TestInvalidVersionFixtures(t *testing.T) { + t.Parallel() + + // The parser's version validator funnels every unsupported VERSION value + // (bad major/minor/patch, or an unrecognised trailing token) to the same + // message. Feature-flag validation (e.g. invalid-feature-flag-override.earth) + // lives in the features package, not here, so it is exercised by the + // integration tests under tests/version, not by this unit test. + const wantErr = "invalid VERSION in Earthfile, supported versions are 0.6, 0.7, or 0.8" + + fixtures := []string{ + "invalid-major-version.earth", + "invalid-minor-version.earth", + "invalid-patch-version.earth", + "invalid-format-version.earth", + } + + for _, fixture := range fixtures { + t.Run(fixture, func(t *testing.T) { + t.Parallel() + + _, err := ParseFile(filepath.Join("testdata", "version", fixture)) + require.Error(t, err) + require.ErrorContains(t, err, wantErr) + }) + } +} diff --git a/tests/version/Earthfile b/tests/version/Earthfile index a03aef6c4c..09673ed918 100644 --- a/tests/version/Earthfile +++ b/tests/version/Earthfile @@ -5,89 +5,21 @@ IMPORT .. AS tests WORKDIR /test -test-single-line: +test-cli-smoke: + # Detailed VERSION syntax coverage lives in internal/earthfile/version_test.go. Keep one + # successful build, one version-only import, and one invalid-version CLI + # smoke here so the full command path still gets exercised. DO --pass-args +RUN_EARTHLY_ARGS --earthfile=single-line.earth --target=+test -test-single-line-with-args: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=single-line-with-args.earth --target=+test - -test-single-line-with-comment: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=single-line-with-comment.earth --target=+test - -test-multi-line: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line.earth --target=+test - -test-multi-line-with-comment: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment.earth --target=+test - -test-multi-line-with-comment2: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment2.earth --target=+test - -test-multi-line-with-comment3: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment3.earth --target=+test - -test-multi-line-with-comment4: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-comment4.earth --target=+test - -test-multi-line-with-args: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-args.earth --target=+test - -test-multi-line-with-args2: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-args2.earth --target=+test - -test-multi-line-with-newline: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=multi-line-with-empty-newline.earth --target=+test - -test-no-feature-flag-overrides: - DO --pass-args +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-feature-flag-override.earth --target=+test --output_contains="bool flag .--referenced-save-only. cannot have an argument" - -test-version-only-import: RUN mkdir subdir RUN echo "VERSION 0.8" > subdir/Earthfile DO --pass-args +RUN_EARTHLY_ARGS --earthfile=version-only-import.earth --target=+test -# test-version-only-without-newline tests that earthly will still work with non-POSIX text files -test-version-only-without-newline: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=version-only.earth --target=+base - RUN test "$(cat Earthfile | wc -l)" = "0" # check Earthfile doesn't contain a newline - -test-comment-and-whitespace-before-version: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=comment-and-whitespace-before-version.earth --target=+test - -test-whitespace-then-version: - DO --pass-args +RUN_EARTHLY_ARGS --earthfile=whitespace-then-version.earth --target=+test - -test-invalid-versions: - DO --pass-args +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-major-version.earth --target=+base - RUN acbgrep 'invalid VERSION in Earthfile, supported versions are 0.6, 0.7, or 0.8' earthly.output - - DO --pass-args +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-minor-version.earth --target=+base - RUN acbgrep 'invalid VERSION in Earthfile, supported versions are 0.6, 0.7, or 0.8' earthly.output - - DO --pass-args +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-patch-version.earth --target=+base - RUN acbgrep 'unexpected VERSION arguments; should be VERSION \[flags\] .' earthly.output - DO --pass-args +RUN_EARTHLY_ARGS --should_fail=true --earthfile=invalid-format-version.earth --target=+base RUN acbgrep 'unexpected VERSION arguments; should be VERSION \[flags\] .' earthly.output test-all: - BUILD +test-single-line - BUILD +test-single-line-with-args - BUILD +test-single-line-with-comment - BUILD +test-multi-line - BUILD +test-multi-line-with-comment - BUILD +test-multi-line-with-comment2 - BUILD +test-multi-line-with-comment3 - BUILD +test-multi-line-with-comment4 - BUILD +test-multi-line-with-args - BUILD +test-multi-line-with-args2 - BUILD +test-multi-line-with-newline - BUILD +test-version-only-without-newline - BUILD +test-comment-and-whitespace-before-version - BUILD +test-whitespace-then-version - BUILD +test-version-only-import - BUILD +test-invalid-versions - BUILD +test-no-feature-flag-overrides + BUILD +test-cli-smoke RUN_EARTHLY_ARGS: FUNCTION diff --git a/tests/version/version-only.earth b/tests/version/version-only.earth deleted file mode 100644 index 06a75fdfd4..0000000000 --- a/tests/version/version-only.earth +++ /dev/null @@ -1 +0,0 @@ -VERSION 0.8 \ No newline at end of file