VERSION 0.8
FROM --pass-args ..+base

WORKDIR /test
ARG --global REGISTRY
ARG --global EARTHLY_BUILD_ARGS="REGISTRY"
ARG --global REGISTRY_CONFIG="
[registry.\"$REGISTRY\"]
  http = true
"

COPY test.earth ./Earthfile

all:
    BUILD +test1
    BUILD +test2
    BUILD +test3

test1:
    RUN echo "content" >./input
    # Running with tmpfs mount = no local cache.
    DO --pass-args +DO_REMOTE_CACHE_EARTHLY --target=+test1

    # Not cached - before copy.
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 1

    # Not cached - after copy.
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 1

    # No change & re-run - should only have 1 line output and not re-echo.
    DO --pass-args +DO_REMOTE_CACHE_EARTHLY --target=+test1

    RUN nl=$(cat ./output | grep -F '*cached* --> RUN echo "execute-test1-run-before-copy"' | wc -l) && acbtest "$nl" -eq 1
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 0

    # Cached - should only have 1 line output and not re-echo.
    RUN nl=$(cat ./output | grep -F '*cached* --> RUN echo "execute-test1-run-after-copy"' | wc -l) && acbtest "$nl" -eq 1
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 0

    RUN echo "other content" >./input
    DO --pass-args +DO_REMOTE_CACHE_EARTHLY --target=+test1

    # Cached.
    RUN nl=$(cat ./output | grep -F '*cached* --> RUN echo "execute-test1-run-before-copy"' | wc -l) && acbtest "$nl" -eq 1
    RUN nl=$(cat ./output | grep "execute-test1-run-before-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 0

    # Not cached.
    RUN nl=$(cat ./output | grep "execute-test1-run-after-copy" | grep -v "echo" | wc -l) && acbtest "$nl" -eq 1

test2:
    RUN echo "a"
    COPY --dir test2 test2
    # Running with tmpfs mount = no local cache.
    DO --pass-args +DO_REMOTE_CACHE_EARTHLY --target=+test2
    DO --pass-args +DO_REMOTE_CACHE_EARTHLY --target=+test2
    # TODO FIXME the remote-cache caching isn't always working; we will ignore these checks until the flakiness is fixed.
    RUN (cat ./output | grep '\*cached\* --> RUN npm install') || echo "WARNING: RUN command not cached"
    RUN (cat ./output | grep '\*cached\* --> COPY test2/dist dist') || echo "WARNING: COPY classical command not cached"
    RUN (cat ./output | grep '\*cached\* --> SAVE ARTIFACT dist +test2-build/dist') || echo "WARNING: SAVE ARTIFACT command not cached"
    RUN (cat ./output | grep '\*cached\* --> COPY +test2-build/dist ./dist') || echo "WARNING: COPY target command not cached"

test3:
    # Running with tmpfs mount = no local cache.
    RUN --privileged \
        --entrypoint \
        --mount=type=tmpfs,target=/tmp/earthly \
        -- --no-output --strict --use-inline-cache --save-inline-cache --push +test3

# Work around the lack of variable overriding, since the base image already includes EARTHLY_ADDITIONAL_BUILDKIT_CONFIG
DO_REMOTE_CACHE_EARTHLY:
    FUNCTION

    ARG target

    RUN --privileged \
        --mount=type=tmpfs,target=/tmp/earthly \
        -- \
        ( EARTHLY_ADDITIONAL_BUILDKIT_CONFIG=$EARTHLY_ADDITIONAL_BUILDKIT_CONFIG$REGISTRY_CONFIG \
        /usr/bin/earthly-entrypoint.sh --use-inline-cache --save-inline-cache --strict --no-output --push \
            --build-arg REGISTRY=$REGISTRY \
            $target; echo "exit_code=$?" ) 2>&1 | tee ./output && acbtest "$(tail -n 1 output)" = "exit_code=0"
