#!/bin/bash
#
# unit tests for tag2upload-fetch-inputs

set -e
. tests/lib

t-dependencies libhttp-server-simple-static-perl

t-git pari-extra 3-1

mkdir $p
cd $p
git init
t-dgit setup-new-tree
git remote add origin $tmp/git/$p.git

expect-rc () {
    local exp_rc=$1; shift
    local mregexp=$1; shift
    set +e
    (set -e; "$@" >../f-i-report)
    local got_rc=$?
    set -e
    test $exp_rc = $got_rc
    grep -P "$mregexp" ../f-i-report
}

# We construct various out-of-course and error conditions.
# This is going to involve some violence, and also some over-familiarity
# with the implementation.
#
# tag2upload-fetch-inputs runs, relevantly,
#  - git fetch
#  - curl
#  - git ls-remote
# ostensibly all on the same URL.
#
# We don't want to spin up an http server good enough to convey git.  Instead
# we use http-static-server to mock http for curl, and we use git config
# settings to cause git to talk to the filesystem (when we want it to work).

mkdir -p $tmp/fake-salsa
$troot/http-static-server >$tmp/must-clean/fake-salsa.port $tmp/fake-salsa
read <$tmp/must-clean/fake-salsa.port fake_salsa_port
fake_salsa_url=http://127.0.0.1:$fake_salsa_port

: ----- "success (doesn't involve curl/http)" -----

t-tag2upload-fetch-inputs tag=test-dummy/3-1 upstream_tag=

: ----- 'curl (http fetch) fails' -----

# The nonexistent tag causes fetch to fail.
# The remote url for origin is a filesystem URL, which curl rejects.
expect-rc 75 'http fetch failed' \
t-tag2upload-fetch-inputs tag=test-dummy/no-such-tag upstream_tag=

: ----- 'http 500' -----

# Set the origin URL "more faithfully".
git remote set-url origin $fake_salsa_url/$p.git
# git fetch will fail because our httpd is too stupid to do git things.
# HTTP::Server::Simple::Static chokes on directories, status 500
mkdir $tmp/fake-salsa/$p.git

expect-rc 75 'HTTP status code 500' \
t-tag2upload-fetch-inputs tag=no-such-tag upstream_tag=

: ----- 'http 400' -----

# Now the URL points to a 404.  (And git fetch obviously fails too still.)
rmdir $tmp/fake-salsa/$p.git
expect-rc 4 'HTTP status code 404' \
t-tag2upload-fetch-inputs tag=no-such-tag upstream_tag=

: ----- 'http 200 but git-ls-remote-fails' -----

# Plain file gives 200 from curl, but errors from git fetch.
touch $tmp/fake-salsa/$p.git
git remote set-url origin $fake_salsa_url/$p.git

expect-rc 75 'git ls-remote .* failed' \
t-tag2upload-fetch-inputs tag=no-such-tag upstream_tag=

: ----- 'http 200 but ref is missing' -----

# insteadOf detaches git's behaviour from curl's.
# (Note git insteadOf is weirdly backwards: the key is the *replacement*.)
# So git now looks in the filesystem (and can therefore work properly).
# (Plain file still gives 200 from curl, as above.)
git config url."$tmp/git".insteadOf "$fake_salsa_url"

expect-rc 3 'missing ref: refs/tags/no-such-tag' \
t-tag2upload-fetch-inputs tag=no-such-tag upstream_tag=

expect-rc 3 'missing ref: refs/tags/no-such-tag' \
t-tag2upload-fetch-inputs tag=test-dummy/3-1 upstream_tag=no-such-tag

: ----- 'mystery failure' -----

# We need a way to sabotage git fetch that won't sabotage git ls-remote.
# Having the intended tag name already in use locally has that effect:
git tag -f -m 'Different, sabotage fetch' test-dummy/3-1 test-dummy/3-1~0

# Now git fetch will fail but everything else seems fine.
expect-rc 75 'repo accessible, refs exist, but git fetch failed' \
t-tag2upload-fetch-inputs tag=test-dummy/3-1 upstream_tag=

t-ok
