Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to check colorls flags #497

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ jobs:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run tests
run: bundle exec rake
- name: Run checks
run: test/run
41 changes: 41 additions & 0 deletions test/checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- mode: sh -*-
# shellcheck shell=bash

OK colorls
OK colorls -1
OK colorls -a
OK colorls -A
OK colorls -d
OK colorls -f
OK colorls -l
OK colorls -l spec/fixtures/symlinks
( cd spec/fixtures || exit ; OUT '.hidden-file' colorls .hidden-file )
OK colorls -l README.md
OK colorls -r

OK colorls --sd
OK colorls --sf
OK colorls --hyperlink
OK colorls -t
OK colorls --sort=time
OK colorls -U
OK colorls --sort=none
OK colorls -S
OK colorls --sort=size
OK colorls -h
OK colorls --gs
OK colorls spec/fixtures/symlinks
OK colorls README.md
OK colorls ./*
OUT / colorls
OK colorls --color
OK colorls --color=auto
OK colorls --color=never
OK colorls --color=always
OK colorls --tree spec
OK colorls --tree=1

LC_ALL=C OK colorls spec/fixtures/
LC_ALL=C OK colorls --git spec/fixtures/

XFAIL 2 colorls does-not-exist
102 changes: 102 additions & 0 deletions test/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#! /usr/bin/env bash

set -eEuCo pipefail

declare RED=$'\033[31m'
declare GREEN=$'\033[32m'
declare RESET=$'\033[39m'

declare -i ERRORS=0 TESTS=0

function colorls() {
command bundle exec colorls "$@"
}

# check that the given command returns exit code 0
#
# SYNOPSIS: OK COMMAND [ARGS]
#
function OK() {
local ret

((++TESTS))

if "$@"; then
echo "$GREEN" "OK $RESET - $*" >&2
else
ret=$?

((++ERRORS))

echo "$RED" "FAIL$RESET - $* (exit code: $ret, ${BASH_SOURCE[1]}:${BASH_LINENO[0]})" >&2
fi
}

# check that the given command returns a non-zero exit code
#
# SYNOPSIS: XFAIL [exit-code] COMMAND [ARGS]
#
function XFAIL() {
local expected ret

if [[ "$1" == [1-9] ]]; then
# expect a specific non-zero exit code
expected="$1"
shift
fi

((++TESTS))

if "$@"; then
((++ERRORS))

echo "$RED" "FAIL$RESET - $* (unexpected success, ${BASH_SOURCE[1]}:${BASH_LINENO[0]})" >&2
else
ret=$?
if [[ -v expected && $expected -ne $ret ]]; then
((++ERRORS))

echo "$RED" "FAIL$RESET - $* (expected: $expected got $ret, ${BASH_SOURCE[1]}:${BASH_LINENO[0]})" >&2
else
echo "$GREEN" "OK $RESET - $*" >&2
fi
fi
}

# check that the given command returns with exit code 0 and its stdout contains a text
#
# SYNOPSIS: OUT TEXT COMMAND [ARGS]
#
function OUT() {
local STR="$1" ret
shift

((++TESTS))

if "$@" | grep -F "$STR"; then
echo "$GREEN" "OK $RESET - $*" >&2
else
ret=${PIPESTATUS[0]}

((++ERRORS))

if [[ $ret -ne 0 ]]; then
echo "$RED" "FAIL$RESET - $* (exit code: $ret, ${BASH_SOURCE[1]}:${BASH_LINENO[0]})" >&2
else
echo "$RED" "FAIL$RESET - $* ('$STR' not found in output, ${BASH_SOURCE[1]}:${BASH_LINENO[0]})" >&2
fi
fi
}

function summary() {
if [[ $ERRORS -gt 0 ]]; then
printf '\n\n %d of %d tests failed.' "$ERRORS" "$TESTS" >&2
exit 1
else
printf '\n\n %d tests passed.' "$TESTS" >&2
fi
}

trap summary EXIT

source "$(dirname "${BASH_SOURCE[0]}")/checks"