build/tests/01-cli-flags.bats

272 lines
9.0 KiB
Bash
Executable File

#! /usr/bin/env bats
# 01-cli-flags.bats
# In this first test suite for forgebuild, we disable translations entirely.
# If you're just getting started writing your implementation, good! No need
# to disable anything. Otherwise, please make sure LANG=NONE disables
# translations entirely. eg. "$(trans error)$(trans missing_basedir)"
# outputs "errormissing_basedir"
load helpers
function setup {
ORIGDIR="$(pwd)"
TMPDIR="$(mktemp -d)"
# If no $FORGEBUILD is supplied in ENV, use the default
# (otherwise you can't `bats ./tests/` manually)
[ -z "$FORGEBUILD" ] && FORGEBUILD="forgebuild"
# Setup forgebuild tasks
FORGEBUILDDIR="$TMPDIR"/forgebuild
mkdir $FORGEBUILDDIR
echo "date +%s%N > $TMPDIR/forgebuild-time" > $FORGEBUILDDIR/time
chmod +x $FORGEBUILDDIR/time
echo "echo \$(pwd) > $TMPDIR/forgebuild-pwd" > $FORGEBUILDDIR/pwd
chmod +x $FORGEBUILDDIR/pwd
# Disable translations
export LANG="NONE"
# Enable debug for full log
export LOG="debug"
}
function teardown {
cd $ORIGDIR
if [ -d $TMPDIR ]; then rm -rf $TMPDIR; fi
}
# forgebuild being a CLI application, first we'll check that it can display
# a use help message when asked to. When called with the -h|--help flag,
# forgebuild should display a help message and quit without error.
# The first line of the help message should contain "forgebuild VER",
# where VER is the semantic version of the software.
@test "Can provide help" {
run $FORGEBUILD --help
echo "$output"
# forgebuild returns 0 exit code (no error)
[ $status = 0 ]
first_output="$output"
# the first line contains name/version eg. forgebuild 0.2.3
echo "${lines[0]}" | grep -P "forgebuild \d+[\.\d+]*"
# -h should return exactly the same output
run $FORGEBUILD -h
[ $status = 0 ]
eq "$first_output" "$output"
}
# In the same manner, forgebuild should return version information
# when called with the -V|--version flag. Note that "-V" uses a capital
# letter V, as -v is traditionally reserved for verbosity level (loglevel).
# The version information should output only one line: "forgebuild VER",
# where VER is a semantic version.
@test "Can provide version information" {
run $FORGEBUILD --version
[ $status = 0 ]
first_output="$output"
echo "$output" | run grep -P "forgebuild \d+[\.\d+]*"
[ $status = 0 ]
run $FORGEBUILD -V
[ $status = 0 ]
eq "$first_output" "$output"
}
# The basedir is the path from which we are going to load tasks.
# It is either passed with the `-b|--basedir` flag, or defaults
# to ~/.basedir. When this folder does not exist, forgebuild
# should output "errormissing_basedir" and return with a non-zero
# exit code.
@test "Detects missing basedir" {
run $FORGEBUILD -b /tmp/THISPATHDOESNOTEXIST
[ $status != 0 ]
echo "$output"
eq "$output" "$(error missing_basedir)"
if [ ! -d $HOME/.forgebuild ]; then
run $FORGEBUILD
[ $status != 0 ]
echo "$output"
eq "$output" "$(error missing_basedir)"
fi
}
# The positional arguments passed to forgebuild are a list of tasks
# to be triggered or not depending on further configuration. In this
# test suite, two tasks are declared (time, pwd) and are configured
# to run. If an unknown task is requested, forgebuild will output
# "errorunknown_arg" and return with a non-zero exit code.
@test "Doesn't recognize unknown task" {
run $FORGEBUILD -b $FORGEBUILDDIR THISTASKDOESNOTEXIST
[ $status != 0 ]
echo "$output"
eq "$output" "$(error unknown_arg)"
}
# When called with an unknown flag, forgebuild will output
# "error_unknownarg" and return with a non-zero exit code.
@test "Doesn't recognize unknown argument" {
run $FORGEBUILD -WTF -b $FORGEBUILDDIR
[ $status != 0 ]
echo "$output"
# First line is unknown_arg error, then maybe help message?
eq "${lines[0]}" "$(error unknown_arg)"
}
# When no tasks are requested as positional arguments, and no
# special flag is passed, forgebuild will output "infono_task",
# then for each executable file T found in the basedir, will
# output "debugfound_task" when LOG=debug.
@test "No tasks requested finds tasks in basedir" {
run $FORGEBUILD -b $FORGEBUILDDIR
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(info no_task)"
eq "${lines[1]}" "$(debug found_task)"
eq "${lines[2]}" "$(debug found_task)"
}
# When no tasks are requested, and no tasks are found in the basedir
# forgebuild simply prints "infono_task".
# TODO: maybe introduce a "nothing to do" message here?
@test "No task exists does not fail" {
rm -r $FORGEBUILDDIR/*
run $FORGEBUILD -b $FORGEBUILDDIR
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(info no_task)"
}
# When no tasks are requested, but some tasks are found
# in the basedir, forgebuild starts processing the tasks,
# printing "debugstart_proc", "infoprocess" then "inforun"
# for each task found.
@test "Claims to run all tasks in the basedir" {
run $FORGEBUILD -b $FORGEBUILDDIR
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(info no_task)"
eq "${lines[1]}" "$(debug found_task)"
eq "${lines[2]}" "$(debug found_task)"
# Keep a copy of the lines to match against later.
# WTF i don't know how to copy an array in bash!! Everything
# i try gives me only one entry in the array...
# Quick hack: copy the entire output..
prev_output="$output"
# Maybe there's some additional output (like config)
# to suppress from this test.
CONTEXT="$output" run find_line "$(debug start_proc)"
[ $status = 0 ]
# The line number matching start_proc
i="$output"
# Get the lines again from the original forgebuild output
run echo "$prev_output"
# Now, for each of the two tasks triggered we should receive
# debugstart_proc infoprocess and inforun
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
}
# When a single task is requested via a positional argument, only
# a single task is processed.
@test "Claims to run single task" {
run $FORGEBUILD -b $FORGEBUILDDIR time
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(debug found_task)"
# Quick hack: copy the entire output.. (TODO)
prev_output="$output"
# Maybe there's some additional output (like config)
# to suppress from this test.
CONTEXT="$output" run find_line "$(debug start_proc)"
[ $status = 0 ]
# The line number matching start_proc
i="$output"
# Get the lines again from the original forgebuild output
run echo "$prev_output"
# Now, we should receive debugstart_proc infoprocess and inforun
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
}
# When multiple tasks are requested via positional arguments and they exist,
# all those tasks are run.
@test "Claims to run multiple tasks" {
run $FORGEBUILD -b $FORGEBUILDDIR time pwd
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(debug found_task)"
eq "${lines[1]}" "$(debug found_task)"
# Quick hack: copy the entire output.. (TODO)
prev_output="$output"
# Maybe there's some additional output (like config)
# to suppress from this test.
CONTEXT="$output" run find_line "$(debug start_proc)"
[ $status = 0 ]
# The line number matching start_proc
i="$output"
# Get the lines again from the original forgebuild output
run echo "$prev_output"
# Now, for each of the two tasks triggered we should receive
# debugstart_proc infoprocess and inforun
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
}
# In case duplicate tasks are passed as positional arguments, each
# unique task is triggered only once
@test "Doesn't reprocess duplicate tasks" {
run $FORGEBUILD -b $FORGEBUILDDIR time pwd time pwd time
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(debug found_task)"
eq "${lines[1]}" "$(debug found_task)"
# Quick hack: copy the entire output.. (TODO)
prev_output="$output"
# Maybe there's some additional output (like config)
# to suppress from this test.
CONTEXT="$output" run find_line "$(debug start_proc)"
[ $status = 0 ]
# The line number matching start_proc
i="$output"
# Get the lines again from the original forgebuild output
run echo "$prev_output"
# Now, for each of the two tasks triggered we should receive
# debugstart_proc infoprocess and inforun
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
eq "${lines[i++]}" "$(debug start_proc)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
}