1#!/bin/bash
2
3SUT=$(realpath $(dirname $0)/../makeself.sh)
4
5setupTests() {
6  temp=`mktemp -d -t XXXXX`
7  cd ${temp}
8  mkdir src
9  echo "echo This is a test" > src/startup.sh
10}
11
12# Default behaviour is to insert the current date in the
13# generated file.
14testCurrentDate() {
15  setupTests
16
17  ${SUT} src src.sh alabel startup.sh
18
19  # Validate
20  actual=`strings src.sh | grep packaging`
21
22  expected=`LC_ALL=C date +"%b"`
23
24  if [[ ${actual} == *${expected}* ]]
25  then
26    found=0
27  else
28    echo "Substring not found: ${expected} in ${actual}"
29    found=1
30  fi
31  assertEqual 0 ${found}
32
33  # Cleanup
34  cd -
35  rm -rf ${temp}
36}
37
38
39# A fixed packaging date can be inserted
40# into the generated package.  This way
41# the package may be recreated from
42# source and remain byte-for-bye
43# identical.
44testDateSet() {
45  setupTests
46
47  expected='Sat Mar  5 19:35:21 EST 2016'
48
49  # Exercise
50  ${SUT} --packaging-date "${expected}" \
51    src src.sh alabel startup.sh
52
53  # Validate
54  actual=`strings src.sh | grep "Date of packaging"`
55  echo "actual="${actual}
56  if [[ ${actual} == *${expected}* ]]
57  then
58    echo date set found
59    found=0
60  else
61    echo "Substring not found: ${expected} in ${actual}"
62    found=1
63  fi
64  assertEqual 0 ${found}
65
66  # Cleanup
67  cd -
68  rm -rf ${temp}
69}
70
71
72# Error if --packaging-date is passed as
73# an argument but the date is missing
74testPackagingDateNeedsParameter() {
75  setupTests
76
77  # Exercise
78  ${SUT} --packaging-date  \
79    src src.sh alabel startup.sh || true
80  actual=`test -f src.sh`
81
82  # Validate
83  echo "actual="${actual}
84  assertNotEqual 0 ${actual}
85
86  # Cleanup
87  cd -
88  rm -rf ${temp}
89}
90
91# With the dates set we can get a byte for
92# byte identical package.
93testByteforbyte()
94{
95  setupTests
96
97  date='Sat Mar  3 19:35:21 EST 2016'
98
99  # Exercise
100  ${SUT} --packaging-date "${date}" --tar-extra "--mtime 20160303" \
101    src src.sh alabel startup.sh
102  mv src.sh first
103  ${SUT} --packaging-date "${date}" --tar-extra "--mtime 20160303" \
104    src src.sh alabel startup.sh
105  mv src.sh second
106
107  # Validate
108  cmp first second
109  rc=$?
110  assert $rc
111
112  # Cleanup
113  cd -
114  rm -rf ${temp}
115}
116
117source bashunit/bashunit.bash
118