summaryrefslogtreecommitdiff
path: root/test/utils.bash
blob: cd6fcf0c455e96ae33a39f6d2700f11d311b26f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash

workdir=test-run

# TODO: assert that the exit code is 1 for 'usage' outputs.
exit_usage=1
exit_usage_wrong=0

setup() {
  ORIG_PATH=$PATH
  APPSTORE_HOME=$(cd $BATS_TEST_DIRNAME/..; echo `pwd`)
  PATH=/bin:/usr/bin
  PATH=$PATH:$APPSTORE_HOME

  rm -rf $BATS_TMPDIR/appstore
  mkdir $BATS_TMPDIR/appstore

  HOME=$BATS_TMPDIR/appstore-home

  cd $BATS_TMPDIR/appstore

  REPO=$BATS_TMPDIR/repo
  REPO_URL="file://$REPO"
  FIXED_REPO_URL="file://`fix_path $REPO`"

  if [ "`declare -f setup_inner >/dev/null; echo $?`" = 0 ]
  then
    setup_inner
  fi
}

echo_lines() {
  echo lines:
  for line in "${lines[@]}"; do echo $line; done
  echo status=$status
}

mkzip() {
(
  cd $BATS_TEST_DIRNAME/data/$1
  rm -f ../$1.zip
  zip -qr ../$1.zip *
)
}

install_artifact() {
  local artifactId=${1}; shift
  local version=${1-1.0-SNAPSHOT}
  describe -Dfile=`fix_path $APPSTORE_HOME/test/data/$artifactId.zip` -DgeneratePom
  PATH=$ORIG_PATH mvn deploy:deploy-file -Durl=$FIXED_REPO_URL \
    -Dfile=`fix_path $APPSTORE_HOME/test/data/$artifactId.zip` -DgeneratePom \
    -DgroupId=org.example -DartifactId=$artifactId -Dversion=$version -Dpackaging=zip
}

check_status=yes

app() {
  echo app $@
  run $APPSTORE_HOME/app $@
  echo_lines

  if [ "$check_status" = yes ]
  then
    eq '$status' 0
  fi

  check_status=yes
}

app_libexec() {
  local x=`PATH=$APPSTORE_HOME/libexec:/bin:/usr/bin which $1`

  echo libexec/$@
  shift
  run "$x" $@

  echo_lines

  if [ "$check_status" = yes ]
  then
    eq '$status' 0
  fi

  check_status=yes
}

fix_path_uname=`uname -s`
fix_path() {
  case $fix_path_uname in
    CYGWIN_NT*)
      cygpath -wa $1
      ;;
    *)
      echo $1
      ;;
  esac
}

describe() {
  echo "# " $@ >&3
}

can_read() {
  if [ -r "$1" ]
  then
    return 0
  else
    echo "Can't read $1"
    return 1
  fi
}

can_not_read() {
  if [ ! -r "$1" ]
  then
    return 0
  else
    echo "Can read $1"
    return 1
  fi
}

is_directory() {
  if [ ! -d "$1" ]
  then
    echo "Not a directory: $1" 2>&1
    return 1
  fi
}

eq() {
  local ex="$1"
  local e="$2"
  local a="`eval echo $ex`"

  if [[ $e == $a ]]
  then
    return 0
  fi

  echo "Assertion failed: $ex"
  echo "Expected: $e"
  echo "Actual:   $a"
  exit 1
}

neq() {
  local ex="$1"
  local e="$2"
  local a="`eval echo $ex`"

  if [[ $e != $a ]]
  then
    return 0
  fi

  echo "Not-equal assertion failed: $ex"
  echo "Expected: $e"
  echo "Actual:   $a"
  exit 1
}

match() {
  local ex="$1"
  local regex="$2"
  local a="`eval echo $ex`"

  if [[ $a =~ $regex ]]
  then
    return 0
  fi

  echo "Match failed: $ex =~ $regex"
  echo "Value:    $a"
  exit 1
}