summaryrefslogtreecommitdiff
path: root/test/datetest
blob: e8f2c62312106b0d921c14c9656ecadec8b5dff2 (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
#!/bin/bash
set -eu
THIS="$(realpath "$0")"
THISDIR="$(dirname "${THIS}")"
SRCDIR="$(dirname "${THISDIR}")"
SUT="${SRCDIR}/makeself.sh"

setUp() {
  temp="$(mktemp -dt datetest.XXXXX)"
  cd "${temp}"
  mkdir src
  echo "echo This is a test" > src/startup.sh
}

tearDown() {
  # Cleanup
  cd -
  rm -rf "${temp}"
}

# Default behaviour is to insert the current date in the
# generated file.
testCurrentDate() {
  ${SUT} src src.sh alabel startup.sh

  # Validate
  actual=`strings src.sh | grep packaging`

  expected=`LC_ALL=C date +"%b"`

  if [[ ${actual} == *${expected}* ]]
  then
    found=0
  else
    echo "Substring not found: ${expected} in ${actual}"
    found=1
  fi
  assertEquals 0 ${found}
}


# A fixed packaging date can be inserted
# into the generated package.  This way
# the package may be recreated from
# source and remain byte-for-bye 
# identical.
testDateSet() {
  expected='Sat Mar  5 19:35:21 EST 2016'

  # Exercise
  ${SUT} --packaging-date "${expected}" \
    src src.sh alabel startup.sh

  # Validate
  actual=`strings src.sh | grep "Date of packaging"`
  echo "actual="${actual}
  if [[ ${actual} == *${expected}* ]]
  then
    echo date set found
    found=0
  else
    echo "Substring not found: ${expected} in ${actual}"
    found=1
  fi
  assertEquals 0 ${found}
}


# Error if --packaging-date is passed as
# an argument but the date is missing
testPackagingDateNeedsParameter() {
  # Exercise
  ${SUT} --packaging-date  \
    src src.sh alabel startup.sh || true
  actual=`test -f src.sh`

  # Validate
  echo "actual="${actual}
  assertNotEquals 0 "${actual}"
}

# With the dates set we can get a byte for
# byte identical package.
testByteforbyte()
{
  date='Sat Mar  3 19:35:21 EST 2016'

  # bsdtar does not have option --mtime
  # TODO: unstable test: first second differ: char 242, line 10

  startSkipping

  # Exercise
  ${SUT} --packaging-date "${date}" --tar-extra "--mtime 20160303" \
    src src.sh alabel startup.sh
  mv src.sh first
  ${SUT} --packaging-date "${date}" --tar-extra "--mtime 20160303" \
    src src.sh alabel startup.sh
  mv src.sh second

  # Validate
  cmp first second

  assertEquals $? 0
}

# Load and run shUnit2.
source "./shunit2/shunit2"