4ff2bb1bf254a932a6c2d19c0fe58e6c42f62c40
   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=:
  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
  60start_p4d() {
  61        mkdir -p "$db" "$cli" "$git" &&
  62        rm -f "$pidfile" &&
  63        (
  64                cd "$db" &&
  65                {
  66                        p4d -q -p $P4DPORT &
  67                        echo $! >"$pidfile"
  68                }
  69        ) &&
  70
  71        # This gives p4d a long time to start up, as it can be
  72        # quite slow depending on the machine.  Set this environment
  73        # variable to something smaller to fail faster in, say,
  74        # an automated test setup.  If the p4d process dies, that
  75        # will be caught with the "kill -0" check below.
  76        i=${P4D_START_PATIENCE:-300}
  77        pid=$(cat "$pidfile")
  78        ready=
  79        while test $i -gt 0
  80        do
  81                # succeed when p4 client commands start to work
  82                if p4 info >/dev/null 2>&1
  83                then
  84                        ready=true
  85                        break
  86                fi
  87                # fail if p4d died
  88                kill -0 $pid 2>/dev/null || break
  89                echo waiting for p4d to start
  90                sleep 1
  91                i=$(( $i - 1 ))
  92        done
  93
  94        if test -z "$ready"
  95        then
  96                # p4d failed to start
  97                return 1
  98        fi
  99
 100        # build a p4 user so author@example.com has an entry
 101        p4_add_user author
 102
 103        # build a client
 104        client_view "//depot/... //client/..." &&
 105
 106        return 0
 107}
 108
 109p4_add_user() {
 110        name=$1 &&
 111        p4 user -f -i <<-EOF
 112        User: $name
 113        Email: $name@example.com
 114        FullName: Dr. $name
 115        EOF
 116}
 117
 118kill_p4d() {
 119        pid=$(cat "$pidfile")
 120        # it had better exist for the first kill
 121        kill $pid &&
 122        for i in 1 2 3 4 5 ; do
 123                kill $pid >/dev/null 2>&1 || break
 124                sleep 1
 125        done &&
 126        # complain if it would not die
 127        test_must_fail kill $pid >/dev/null 2>&1 &&
 128        rm -rf "$db" "$cli" "$pidfile"
 129}
 130
 131cleanup_git() {
 132        rm -rf "$git" &&
 133        mkdir "$git"
 134}
 135
 136marshal_dump() {
 137        what=$1 &&
 138        line=${2:-1} &&
 139        cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
 140        import marshal
 141        import sys
 142        for i in range($line):
 143            d = marshal.load(sys.stdin)
 144        print d['$what']
 145        EOF
 146        "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
 147}
 148
 149#
 150# Construct a client with this list of View lines
 151#
 152client_view() {
 153        (
 154                cat <<-EOF &&
 155                Client: $P4CLIENT
 156                Description: $P4CLIENT
 157                Root: $cli
 158                AltRoots: $(native_path "$cli")
 159                LineEnd: unix
 160                View:
 161                EOF
 162                printf "\t%s\n" "$@"
 163        ) | p4 client -i
 164}
 165
 166is_cli_file_writeable() {
 167        # cygwin version of p4 does not set read-only attr,
 168        # will be marked 444 but -w is true
 169        file="$1" &&
 170        if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
 171        then
 172                stat=$(stat --format=%a "$file") &&
 173                test $stat = 644
 174        else
 175                test -w "$file"
 176        fi
 177}