t / lib-git-p4.shon commit t9502: fix &&-chain breakage (d8cd327)
   1#
   2# Library code for git p4 tests
   3#
   4
   5# p4 tests never use the top-level repo; always build/clone into
   6# a subdirectory called "$git"
   7TEST_NO_CREATE_REPO=NoThanks
   8
   9. ./test-lib.sh
  10
  11if ! test_have_prereq PYTHON
  12then
  13        skip_all='skipping git p4 tests; python not available'
  14        test_done
  15fi
  16( p4 -h && p4d -h ) >/dev/null 2>&1 || {
  17        skip_all='skipping git p4 tests; no p4 or p4d'
  18        test_done
  19}
  20
  21# On cygwin, the NT version of Perforce can be used.  When giving
  22# it paths, either on the command-line or in client specifications,
  23# be sure to use the native windows form.
  24#
  25# Older versions of perforce were available compiled natively for
  26# cygwin.  Those do not accept native windows paths, so make sure
  27# not to convert for them.
  28native_path() {
  29        path="$1" &&
  30        if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
  31        then
  32                path=$(cygpath --windows "$path")
  33        else
  34                path=$(test-path-utils real_path "$path")
  35        fi &&
  36        echo "$path"
  37}
  38
  39# Try to pick a unique port: guess a large number, then hope
  40# no more than one of each test is running.
  41#
  42# This does not handle the case where somebody else is running the
  43# same tests and has chosen the same ports.
  44testid=${this_test#t}
  45git_p4_test_start=9800
  46P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
  47
  48P4PORT=localhost:$P4DPORT
  49P4CLIENT=client
  50P4USER=author
  51P4EDITOR=true
  52unset P4CHARSET
  53export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
  54
  55db="$TRASH_DIRECTORY/db"
  56cli="$TRASH_DIRECTORY/cli"
  57git="$TRASH_DIRECTORY/git"
  58pidfile="$TRASH_DIRECTORY/p4d.pid"
  59
  60# git p4 submit generates a temp file, which will
  61# not get cleaned up if the submission fails.  Don't
  62# clutter up /tmp on the test machine.
  63TMPDIR="$TRASH_DIRECTORY"
  64export TMPDIR
  65
  66start_p4d() {
  67        mkdir -p "$db" "$cli" "$git" &&
  68        rm -f "$pidfile" &&
  69        (
  70                cd "$db" &&
  71                {
  72                        p4d -q -p $P4DPORT &
  73                        echo $! >"$pidfile"
  74                }
  75        ) &&
  76
  77        # This gives p4d a long time to start up, as it can be
  78        # quite slow depending on the machine.  Set this environment
  79        # variable to something smaller to fail faster in, say,
  80        # an automated test setup.  If the p4d process dies, that
  81        # will be caught with the "kill -0" check below.
  82        i=${P4D_START_PATIENCE:-300}
  83        pid=$(cat "$pidfile")
  84        ready=
  85        while test $i -gt 0
  86        do
  87                # succeed when p4 client commands start to work
  88                if p4 info >/dev/null 2>&1
  89                then
  90                        ready=true
  91                        break
  92                fi
  93                # fail if p4d died
  94                kill -0 $pid 2>/dev/null || break
  95                echo waiting for p4d to start
  96                sleep 1
  97                i=$(( $i - 1 ))
  98        done
  99
 100        if test -z "$ready"
 101        then
 102                # p4d failed to start
 103                return 1
 104        fi
 105
 106        # build a p4 user so author@example.com has an entry
 107        p4_add_user author
 108
 109        # build a client
 110        client_view "//depot/... //client/..." &&
 111
 112        return 0
 113}
 114
 115p4_add_user() {
 116        name=$1 &&
 117        p4 user -f -i <<-EOF
 118        User: $name
 119        Email: $name@example.com
 120        FullName: Dr. $name
 121        EOF
 122}
 123
 124kill_p4d() {
 125        pid=$(cat "$pidfile")
 126        # it had better exist for the first kill
 127        kill $pid &&
 128        for i in 1 2 3 4 5 ; do
 129                kill $pid >/dev/null 2>&1 || break
 130                sleep 1
 131        done &&
 132        # complain if it would not die
 133        test_must_fail kill $pid >/dev/null 2>&1 &&
 134        rm -rf "$db" "$cli" "$pidfile"
 135}
 136
 137cleanup_git() {
 138        rm -rf "$git" &&
 139        mkdir "$git"
 140}
 141
 142marshal_dump() {
 143        what=$1 &&
 144        line=${2:-1} &&
 145        cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
 146        import marshal
 147        import sys
 148        for i in range($line):
 149            d = marshal.load(sys.stdin)
 150        print d['$what']
 151        EOF
 152        "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
 153}
 154
 155#
 156# Construct a client with this list of View lines
 157#
 158client_view() {
 159        (
 160                cat <<-EOF &&
 161                Client: $P4CLIENT
 162                Description: $P4CLIENT
 163                Root: $cli
 164                AltRoots: $(native_path "$cli")
 165                LineEnd: unix
 166                View:
 167                EOF
 168                printf "\t%s\n" "$@"
 169        ) | p4 client -i
 170}
 171
 172is_cli_file_writeable() {
 173        # cygwin version of p4 does not set read-only attr,
 174        # will be marked 444 but -w is true
 175        file="$1" &&
 176        if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
 177        then
 178                stat=$(stat --format=%a "$file") &&
 179                test $stat = 644
 180        else
 181                test -w "$file"
 182        fi
 183}