2017-08-28 18:07:44 +02:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
2019-04-04 12:39:17 +02:00
|
|
|
INSTALL_DIR=
|
2019-05-20 23:00:23 +02:00
|
|
|
BATS_TMPDIR="${BATS_TMPDIR:-/tmp}"
|
2019-04-23 08:38:38 +02:00
|
|
|
fancyx='✗'
|
|
|
|
checkmark='✓'
|
2019-04-04 12:39:17 +02:00
|
|
|
## We need to create a new directory so that .hasrc file in the root does not get read by the `has` instance under test
|
2018-11-16 11:31:07 +01:00
|
|
|
setup() {
|
2019-05-20 23:00:23 +02:00
|
|
|
export HAS_TMPDIR="${BATS_TMPDIR}/tmp-for-test"
|
|
|
|
mkdir -p "${HAS_TMPDIR}"
|
|
|
|
cp -f "${BATS_TEST_DIRNAME}"/has "${HAS_TMPDIR}"
|
|
|
|
cd "${HAS_TMPDIR}" || return
|
|
|
|
export has="${HAS_TMPDIR}/has"
|
2018-11-16 11:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
2019-05-20 23:00:23 +02:00
|
|
|
if [[ -d "${HAS_TMPDIR}" ]]; then
|
|
|
|
rm -rf "${HAS_TMPDIR}"
|
2019-04-04 12:39:17 +02:00
|
|
|
fi
|
2018-11-16 11:31:07 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
@test "invoking 'has' without arguments prints usage" {
|
|
|
|
run $has
|
|
|
|
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "${lines[0]%% *}" = 'has' ]
|
|
|
|
[ "${lines[1]%%:*}" = 'USAGE' ]
|
|
|
|
[ "${lines[2]}" = 'EXAMPLE: has git curl node' ]
|
|
|
|
}
|
|
|
|
|
2019-04-04 12:39:17 +02:00
|
|
|
@test "make install creates a valid installation" {
|
2019-05-20 23:00:23 +02:00
|
|
|
INSTALL_DIR="${HAS_TMPDIR}/.local"
|
2019-04-04 12:39:17 +02:00
|
|
|
cd "${BATS_TEST_DIRNAME}"
|
|
|
|
run make PREFIX="${INSTALL_DIR}" install
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ -x "${INSTALL_DIR}/bin/has" ]
|
|
|
|
|
|
|
|
# has reads .hasrc from $PWD, so change anywhere else.
|
|
|
|
cd "${INSTALL_DIR}"
|
|
|
|
run "${INSTALL_DIR}/bin/has"
|
|
|
|
[ "$status" -eq 0 ]
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "${lines[0]%% *}" = 'has' ]
|
|
|
|
[ "${lines[1]%%:*}" = 'USAGE' ]
|
|
|
|
[ "${lines[2]}" = 'EXAMPLE: has git curl node' ]
|
|
|
|
# [ "${lines[2]%%:*}" = 'EXAMPLE' ]
|
2019-04-04 12:39:17 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 08:38:38 +02:00
|
|
|
@test "..even if 'has' is missing from directory" {
|
2019-05-20 23:00:23 +02:00
|
|
|
INSTALL_DIR="${HAS_TMPDIR}/system_local"
|
2019-04-04 12:39:17 +02:00
|
|
|
cd "${BATS_TEST_DIRNAME}"
|
|
|
|
mv has has-been
|
|
|
|
run make PREFIX="${INSTALL_DIR}" install
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ -x "${INSTALL_DIR}/bin/has" ]
|
|
|
|
cd "${BATS_TEST_DIRNAME}"
|
|
|
|
mv has-been has
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "make update runs git fetch" {
|
|
|
|
cd "${BATS_TEST_DIRNAME}"
|
2019-05-20 23:00:23 +02:00
|
|
|
skip "make update overwrites my changes"
|
2019-04-04 12:39:17 +02:00
|
|
|
run make update
|
2019-04-23 08:38:38 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "${lines[*]}" =~ "git fetch --verbose" ]
|
2019-04-04 12:39:17 +02:00
|
|
|
}
|
2019-04-23 08:38:38 +02:00
|
|
|
|
2017-08-28 18:07:44 +02:00
|
|
|
@test "works with single command check" {
|
2019-05-20 23:00:23 +02:00
|
|
|
run $has git
|
2017-08-28 18:07:44 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "$(echo "${lines[0]}" | grep "git")" ]
|
2017-08-28 18:07:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
@test "'has' warns about tools not configured" {
|
|
|
|
run $has foobar
|
2017-08-28 18:07:44 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 1 ]
|
|
|
|
[ "$(echo "${output}" | grep ${fancyx} | grep "foobar not understood")" ]
|
2017-08-28 18:07:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
@test "env var 'HAS_ALLOW_UNSAFE' overrides safety check" {
|
|
|
|
HAS_ALLOW_UNSAFE=y run $has foobar
|
2017-08-28 18:07:44 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 1 ]
|
|
|
|
[ "$(echo "${output}" | grep ${fancyx} | grep "foobar")" ]
|
2017-08-28 18:07:44 +02:00
|
|
|
}
|
2017-08-29 05:48:13 +02:00
|
|
|
|
2017-08-29 05:53:21 +02:00
|
|
|
@test "status code reflects number of failed commands" {
|
2019-05-20 23:00:23 +02:00
|
|
|
HAS_ALLOW_UNSAFE=y run $has foobar bc git barbaz
|
2017-08-29 05:48:13 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 2 ]
|
|
|
|
[ "$(echo "${output}" | grep ${fancyx} | grep "foobar")" ]
|
|
|
|
[ "$(echo "${output}" | grep ${fancyx} | grep "barbaz")" ]
|
2017-08-29 05:48:13 +02:00
|
|
|
}
|
2018-11-16 03:27:56 +01:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
@test "status code reflects number of failed commands up to 126" {
|
|
|
|
run $has $(for i in {1..256}; do echo foo; done)
|
2018-11-16 03:27:56 +01:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 126 ]
|
2018-11-16 03:27:56 +01:00
|
|
|
}
|
2018-11-16 11:31:07 +01:00
|
|
|
|
2018-11-16 11:53:22 +01:00
|
|
|
@test "loads commands from .hasrc file and excludes comments" {
|
|
|
|
printf "bash\n#comment\nmake\n" >> .hasrc
|
2018-11-16 11:31:07 +01:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
run $has
|
2018-11-16 11:31:07 +01:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "bash")" ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "make")" ]
|
2018-11-16 11:31:07 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 08:38:38 +02:00
|
|
|
@test "loads commands from .hasrc file and honors CLI args as well" {
|
2018-11-16 11:31:07 +01:00
|
|
|
printf "bash\nmake\ngit" >> .hasrc
|
2019-05-20 23:00:23 +02:00
|
|
|
HAS_ALLOW_UNSAFE=y run $has git bc
|
2018-11-16 11:31:07 +01:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "bash")" ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "make")" ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "git")" ]
|
|
|
|
[ "$(echo "${output}" | grep ${checkmark} | grep "bc")" ]
|
2019-04-23 08:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "testing PASS output with unicode" {
|
2019-05-20 23:00:23 +02:00
|
|
|
run $has git
|
2019-04-23 08:38:38 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 0 ]
|
2019-04-23 08:38:38 +02:00
|
|
|
[[ "printf '%b\n' ${lines[0]}" =~ '✓' ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "testing FAIL output with unicode" {
|
2019-05-20 23:00:23 +02:00
|
|
|
run $has foobar
|
2019-04-23 08:38:38 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 1 ]
|
2019-04-23 08:38:38 +02:00
|
|
|
[[ "printf '%b\n' ${lines[0]}" =~ '✗' ]]
|
2018-11-16 11:31:07 +01:00
|
|
|
}
|
2019-04-23 08:38:38 +02:00
|
|
|
|
|
|
|
@test "fail count 3: testing output with and without unicode" {
|
2019-05-20 23:00:23 +02:00
|
|
|
run $has git foobar barbaz barfoo
|
2019-04-23 08:38:38 +02:00
|
|
|
|
2019-05-20 23:00:23 +02:00
|
|
|
[ "$status" -eq 3 ]
|
2019-04-23 08:38:38 +02:00
|
|
|
[[ "printf '%b\n' ${lines[0]}" =~ "${checkmark}" ]]
|
|
|
|
[[ "printf '%b\n' ${lines[2]}" =~ '✗' ]]
|
|
|
|
}
|
2019-05-20 23:12:04 +02:00
|
|
|
|
|
|
|
@test "testing archiving commands" {
|
2019-05-21 00:13:36 +02:00
|
|
|
run $has tar unzip gzip xz unar pv zip
|
2019-05-20 23:12:04 +02:00
|
|
|
|
|
|
|
[ "$status" -eq 0 ]
|
2019-05-21 00:13:36 +02:00
|
|
|
[ "$(echo "${lines[0]}" | grep "tar")" ]
|
|
|
|
[ "$(echo "${lines[1]}" | grep "unzip")" ]
|
|
|
|
[ "$(echo "${lines[2]}" | grep "gzip")" ]
|
|
|
|
[ "$(echo "${lines[3]}" | grep "xz")" ]
|
|
|
|
[ "$(echo "${lines[4]}" | grep "unar")" ]
|
|
|
|
[ "$(echo "${lines[5]}" | grep "pv")" ]
|
|
|
|
[ "$(echo "${lines[6]}" | grep "zip")" ]
|
2019-05-20 23:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "testing coreutils commands" {
|
|
|
|
run $has coreutils sed awk grep sudo file linux-utils
|
|
|
|
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "$(echo "${lines[0]}" | grep "gnu_coreutils")" ]
|
|
|
|
[ "$(echo "${lines[5]}" | grep "file")" ]
|
|
|
|
[ "$(echo "${lines[6]}" | grep "gnu_coreutils")" ]
|
|
|
|
}
|
2019-05-21 00:13:36 +02:00
|
|
|
|
|
|
|
@test "testing hub version is different to git version" {
|
|
|
|
run $has hub git
|
|
|
|
|
|
|
|
[ "$status" -eq 0 ]
|
|
|
|
[ "$(echo "${lines[0]}" | grep "hub")" ]
|
|
|
|
[ "$(echo "${lines[1]}" | grep "git")" ]
|
|
|
|
[ ! "${lines[0]##*\ }" = "${lines[1]##*\ }" ]
|
|
|
|
}
|