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

IMPORT .. AS tests

WORKDIR /test

test:
    COPY expected-*.yml .

    # Ensure earthly doesn't create overridden config files when they don't exist
    RUN earthly --config config.yml config global.disable_analytics true 2>&1 | grep 'failed to read from config.yml: open config.yml: no such file or directory'

    RUN touch config.yml

    # Adding various configs of different types
    RUN earthly --config config.yml config global.disable_analytics true
    RUN test "$(cat config.yml)" = "$(cat expected-1.yml)"

    RUN earthly --config config.yml config 'git."example.com".password' hunter2
    RUN test "$(cat config.yml)" = "$(cat expected-2.yml)"

    RUN earthly --config config.yml config global.buildkit_additional_args "['userns', '--host']"
    RUN test "$(cat config.yml)" = "$(cat expected-3.yml)"

    RUN earthly --config config.yml config global.conversion_parallelism 5
    RUN test "$(cat config.yml)" = "$(cat expected-4.yml)"

    # --delete should remove a config
    RUN earthly --config config.yml config global.conversion_parallelism --delete
    RUN test "$(cat config.yml)" = "$(cat expected-5.yml)"

    # --help and -h should succeed
    RUN earthly config global.conversion_parallelism --help
    RUN earthly config global.conversion_parallelism -h
    # Ensure configs haven't changed by running help
    RUN test "$(cat config.yml)" = "$(cat expected-5.yml)"

    # Edge cases
    RUN earthly --config config.yml config global.conversion_parallelism oops; test $? = 1
    RUN earthly --config config.yml config global.conversion_parallelism ""; test $? = 1
    RUN earthly --config config.yml config global.buildkit_image ""

    # test earthly runs when no default config is present
    RUN ! test -f /root/.earthly/config.yml
    DO --pass-args +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    # test earthly can write to default config location
    RUN earthly config global.disable_analytics true
    RUN test "$(cat /root/.earthly/config.yml)" = "$(cat expected-1.yml)"

    # test earthly fails when explicitly set to use a different config that doesn't exist
    DO --pass-args +RUN_EARTHLY_ARGS --extra_args="--config=this-does-not-exist.yml" --earthfile="hello.earth" --target="+hello" --should_fail="true" --output_contains="failed to read from this-does-not-exist.yml"

    # test earthly runs with new cache percentage setting
    RUN earthly config global.cache_size_pct 50
    DO --pass-args +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    # test that it still runs alongside a size settings
    RUN earthly config global.cache_size_mb 100
    RUN test "$(cat /root/.earthly/config.yml)" = "$(cat expected-6.yml)"
    DO --pass-args +RUN_EARTHLY_ARGS --earthfile="hello.earth" --target="+hello" --output_contains="greetings"

    RUN touch /tmp/config.yml
    RUN chmod 400 /tmp/config.yml

    # test permission-related errors
    RUN addgroup -S testgroup
    RUN adduser -S -G testgroup testuser

    # required to allow testuser to call +RUN_EARTHLY_ARGS
    RUN chmod 0777 /tmp/earthly-script

    # required to allow testuser to call +RUN_EARTHLY_ARGS

    USER testuser
    WORKDIR /home/testuser

    # test correct user location is used by default
    RUN earthly config global.disable_analytics true
    RUN test "$(cat /home/testuser/.earthly/config.yml)" = "$(cat /test/expected-1.yml)"

    # test correct user location is used when set elsewhere
    RUN touch ~/.earthly/other-config.yml
    RUN EARTHLY_CONFIG=~/.earthly/other-config.yml earthly config global.disable_analytics true
    RUN test "$(cat /home/testuser/.earthly/other-config.yml)" = "$(cat /test/expected-1.yml)"

    # test correct user location is used when set elsewhere with different installation dir
    RUN mkdir ~/.earthly-not-test && touch ~/.earthly-not-test/other-config.yml
    RUN EARTHLY_INSTALLATION_NAME=earthly-test EARTHLY_CONFIG=~/.earthly-not-test/other-config.yml earthly config global.disable_analytics true
    RUN test "$(cat /home/testuser/.earthly-not-test/other-config.yml)" = "$(cat /test/expected-1.yml)"

    # test correct user location is used when set with different installation dir
    RUN EARTHLY_INSTALLATION_NAME=earthly-test2 earthly config global.disable_analytics true
    RUN test "$(cat /home/testuser/.earthly-test2/config.yml)" = "$(cat /test/expected-1.yml)"

    # check permission error is correctly returned
    COPY hello.earth Earthfile
    RUN ! earthly --config=/tmp/config.yml --verbose +hello > output.txt 2>&1
    RUN cat output.txt | grep 'Error: read config: failed to read from /tmp/config.yml: open /tmp/config.yml: permission denied'

    # check permission error is correctly returned even when earthly attempts to read from the default location
    # to make this test work, a symbolic link is required (chown root:root /home/testuser/.earthly/config.yml doesnt work)
    RUN rm /home/testuser/.earthly/config.yml
    RUN ln -s /tmp/config.yml /home/testuser/.earthly/config.yml

    RUN ! earthly --verbose +hello > output.txt 2>&1
    RUN cat output.txt | grep 'Error: read config: failed to read from /home/testuser/.earthly/config.yml: open /home/testuser/.earthly/config.yml: permission denied'

    # make it obvious that this test passed, due to the previous grep displaying "Error: ..." when it passes
    RUN echo "config test passed"


RUN_EARTHLY_ARGS:
    FUNCTION
    ARG earthfile
    ARG target="+all"
    ARG extra_args
    ARG exec_cmd
    ARG should_fail=false
    ARG output_contains
    DO --pass-args tests+RUN_EARTHLY \
        --earthfile=$earthfile \
        --target=$target \
        --should_fail=$should_fail \
        --exec_cmd=$exec_cmd \
        --output_contains=$output_contains \
        --extra_args=$extra_args
