Skip to content

Commit

Permalink
add real tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aakropotkin committed Dec 31, 2023
1 parent d3a651b commit d183ad8
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/sh-scripts/ccjs/ccjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

set -eu;
set -o pipefail;
set -x;


# ---------------------------------------------------------------------------- #

Expand Down
9 changes: 3 additions & 6 deletions src/sh-scripts/ccjs/ccjs_add
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ export JQ REALPATH MKTEMP;
# ---------------------------------------------------------------------------- #

ccjs_add() {

echo "ccjs_add $*" >&2;

_IN_PLACE='';
_FILE='';
_OUTPUT='';
Expand Down Expand Up @@ -125,17 +122,17 @@ ccjs_add() {
case "$_FILE" in
"") echo "$_as_me: Missing FILE argument." >&2; usage_add >&2; exit 1; ;;
/*) :; ;;
*) _FILE="$( "$REALPATH" "$_FILE"; )"; ;;
*) _FILE="$( "$REALPATH" -m "$_FILE"; )"; ;;
esac

case "${_OUTPUT:=${_FILE%.*}.o}" in
/*) :; ;;
*) _OUTPUT="$( "$REALPATH" "$_OUTPUT"; )"; ;;
*) _OUTPUT="$( "$REALPATH" -m "$_OUTPUT"; )"; ;;
esac

case "${_DIRECTORY:=$PWD}" in
/*) :; ;;
*) _DIRECTORY="$( "$REALPATH" "$_DIRECTORY"; )"; ;;
*) _DIRECTORY="$( "$REALPATH" -m "$_DIRECTORY"; )"; ;;
esac


Expand Down
3 changes: 1 addition & 2 deletions src/sh-scripts/ccjs/ccjs_remove
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export JQ REALPATH MKTEMP;
# ---------------------------------------------------------------------------- #

ccjs_remove() {

_IN_PLACE='';
declare -a _files;
_files=();
Expand Down Expand Up @@ -106,7 +105,7 @@ ccjs_remove() {
for _file in "${_files[@]}"; do
case "$_file" in
/*) :; ;;
*) _file="$( "$REALPATH" "$_file"; )"; ;;
*) _file="$( "$REALPATH" -m "$_file"; )"; ;;
esac
_FILES="${_FILES:+$_FILES, }\"$_file\"";
done
Expand Down
106 changes: 100 additions & 6 deletions src/sh-scripts/ccjs/tests/ccjs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#
# ---------------------------------------------------------------------------- #

# bats file_tags=ccjs

load setup_suite.bash;

# ---------------------------------------------------------------------------- #
Expand All @@ -25,7 +27,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,usage
# bats test_tags=usage
@test "ccjs usage" {
run "$CCJS" -u;
assert_success;
Expand All @@ -39,7 +41,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,help
# bats test_tags=help
@test "ccjs help" {
run "$CCJS" -h;
assert_success;
Expand All @@ -53,7 +55,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,usage,ccjs_add
# bats test_tags=usage,ccjs:add
@test "ccjs add usage" {
run "$CCJS" add -u;
assert_success;
Expand All @@ -67,7 +69,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,help,ccjs_add
# bats test_tags=help,ccjs:add
@test "ccjs add help" {
run "$CCJS" add -h;
assert_success;
Expand All @@ -81,7 +83,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,usage,ccjs_remove
# bats test_tags=usage,ccjs:remove
@test "ccjs remove usage" {
run "$CCJS" remove -u;
assert_success;
Expand All @@ -95,7 +97,7 @@ teardown() {

# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs,help,ccjs_remove
# bats test_tags=help,ccjs:remove
@test "ccjs remove help" {
run "$CCJS" remove -h;
assert_success;
Expand All @@ -107,6 +109,98 @@ teardown() {
}


# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs:add,init
@test "create a new compile_commands.json" {
refute "$TEST" -f compile_commands.json;
run "$CCJS" add main.cc;
assert_success;
refute "$TEST" -f compile_commands.json;

echo "$output" > ./compile_commands.json;

run "$JQ" -r 'length' compile_commands.json;
assert_success;
assert_output '1';

run "$JQ" -r '.[0].file' compile_commands.json;
assert_success;
assert_output "$PWD/main.cc";

run "$JQ" -r '.[0].directory' compile_commands.json;
assert_success;
assert_output "$PWD";

run "$JQ" -r '.[0].output' compile_commands.json;
assert_success;
assert_output "$PWD/main.o";

run "$JQ" -r '.[0].arguments|length' compile_commands.json;
assert_success;
assert_output '0';
}


# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs:add,init,inline
@test "create a new compile_commands.json with '-i'" {
refute "$TEST" -f compile_commands.json;
run "$CCJS" add -i main.cc;
assert_success;
assert "$TEST" -f compile_commands.json;
}


# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs:add,init,inline,missing
@test "'ccjs add' does not require files/directories to exist" {
refute "$TEST" -d src;
run "$CCJS" add -i src/main.cc;
assert_success;
}


# ---------------------------------------------------------------------------- #

# bats test_tags=ccjs:add,init,inline
@test "'ccjs add FILE -- ARGS...' has correct fallbacks" {
refute "$TEST" -f compile_commands.json;
run "$CCJS" add -i src/main.cc -- -Iinclude -Wall;
assert_success;

run "$JQ" -r 'length' compile_commands.json;
assert_success;
assert_output '1';

run "$JQ" -r '.[0].file' compile_commands.json;
assert_success;
assert_output "$PWD/src/main.cc";

run "$JQ" -r '.[0].directory' compile_commands.json;
assert_success;
assert_output "$PWD";

run "$JQ" -r '.[0].output' compile_commands.json;
assert_success;
assert_output "$PWD/src/main.o";

run "$JQ" -r '.[0].arguments|length' compile_commands.json;
assert_success;
assert_output '2';

run "$JQ" -r '.[0].arguments[0]' compile_commands.json;
assert_success;
assert_output '-Iinclude';

run "$JQ" -r '.[0].arguments[1]' compile_commands.json;
assert_success;
assert_output '-Wall';
}


# ---------------------------------------------------------------------------- #
#
#
Expand Down
36 changes: 31 additions & 5 deletions src/sh-scripts/ccjs/tests/setup_suite.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ bats_require_minimum_version '1.5.0'

# print_vars VARIABLE
# -------------------
# Print the value of VARIABLE to a user visible file descriptor.
# Print the value of VARIABLE.
print_var() {
local var="$1";
local val;
eval val="\$$var";
echo "$var='$val'" >&3;
echo "$var='$val'";
}

# print_section_sep [CHAR]
# Print a character/string 80 times.
print_section_sep() {
local _char="${1:--}";
for _ in 0 1 2 3 4 5 6 7; do
# Print 10 times.
printf '%s' "$_char$_char$_char$_char$_char$_char$_char$_char$_char$_char";
done;
printf '\n';
}


Expand All @@ -40,9 +51,14 @@ _bats_test_progs=();

# Print the value of each program.
print_progs() {
for prog in "${_bats_test_progs[@]}"; do
print_var "$prog";
done
{
print_section_sep;
echo 'Programs:';
for prog in "${_bats_test_progs[@]}"; do
printf ' ';
print_var "$prog";
done
} >&3;
}

# Setup common programs as user overridable variables.
Expand Down Expand Up @@ -97,8 +113,18 @@ ccjs_setup() {
# ---------------------------------------------------------------------------- #

setup_suite() {

print_section_sep '=' >&3;
echo "Initializing \`ccjs' Test Suite..." >&3;

progs_setup;
ccjs_setup;

{
print_section_sep;
echo 'Starting tests...';
echo '';
} >&3;
}


Expand Down

0 comments on commit d183ad8

Please sign in to comment.