Skip to content

Commit

Permalink
Handle expected failures & use travis-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
keithy committed Aug 17, 2019
1 parent 6a243b2 commit c86b05b
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: bash

script:
- make
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TESTS ?= $(wildcard tests/*.sh)

include Makefile.test


102 changes: 102 additions & 0 deletions Makefile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# FROM: https://github.com/box/Makefile.test
# LICENCE: Apache2
#
# Makefile that has a convenient check target.
# It can be included from another Makefile that only has a TESTS variable
# defined like this
#
# TESTS ?=
#
# Runs the specified test executables. Prepends the test's name to each test's output
# and gives a nice summary at the end of test execution about passed failed
# tests.

# Only bash is supported
SHELL := /bin/bash

THIS_FILE := $(realpath $(lastword $(MAKEFILE_LIST)))
# The directory where Makefile.test (this file) resides
THIS_FILE_DIR := $(shell dirname $(THIS_FILE))

# FIRST_MAKEFILE may be passed from parent make to child make. If it is not
# absent, do not overwrite it.
FIRST_MAKEFILE ?= $(realpath $(firstword $(MAKEFILE_LIST)))
export FIRST_MAKEFILE

# The directory where the Makefile, that is invoked from the command line,
# resides. That makefile would define the TESTS variable. We assume that the
# binaries defined in the TESTS variable also reside in the directory as
# the Makefile. The generated intermediate files will also go to this directory.
FIRST_MAKEFILE_DIR ?= $(shell dirname $(FIRST_MAKEFILE))
export FIRST_MAKEFILE_DIR

# So that the child makefiles can see the same TESTS variable.
export TESTS

failedTestsName := .makefile_test_failed_tests
executedTestsName := .makefile_test_executed_tests
TEST_TARGETS := $(TESTS:%=TARGET_FOR_%)
export TEST_TARGETS

# If the tests need a different environment one can append to this variable.
TEST_ENVIRONMENT = PYTHONPATH=$(THIS_FILE_DIR):$$PYTHONPATH PATH=$(THIS_FILE_DIR):$$PATH

# TODO: Only write to intermediate files, if they exist already.
# https://unix.stackexchange.com/q/405497/212862
# There is still a race condition here. Maybe we should use sed for appending.
define RUN_ONE_TEST
TARGET_FOR_$(1): $$(FIRST_MAKEFILE_DIR)/$(1)
+@export PATH=$$$$(pwd):$$$$PATH; \
if [ -e $$(FIRST_MAKEFILE_DIR)/$$(executedTestsName) ]; then \
echo $$< >> $$(FIRST_MAKEFILE_DIR)/$$(executedTestsName); \
fi; \
$$(TEST_ENVIRONMENT) $$< 2>&1 | sed "s/^/ [$$$$(basename $$<)] /"; test $$$${PIPESTATUS[0]} -eq 0; \
if [ $$$$? -eq 0 ]; then \
echo " PASSED: $$$$(basename $$<)"; \
else \
echo " FAILED: $$$$(basename $$<)"; \
if [ -e $$(FIRST_MAKEFILE_DIR)/$$(failedTestsName) ]; then \
echo $$< >> $$(FIRST_MAKEFILE_DIR)/$$(failedTestsName); \
fi; \
fi;
endef

# Build the above rule to run one test, for all tests.
$(foreach currtest,$(TESTS),$(eval $(call RUN_ONE_TEST,$(currtest))))

# execute the tests and look at the generated temp files afterwards.
actualCheck: $(TEST_TARGETS)
+@failed_tests=$$(cat $(FIRST_MAKEFILE_DIR)/$(failedTestsName) 2> /dev/null | wc -l;); \
executed_tests=$$(cat $(FIRST_MAKEFILE_DIR)/$(executedTestsName) 2> /dev/null | wc -l;); \
if [ $$failed_tests -ne 0 -a $$executed_tests -ne 0 ]; then \
echo ---------------------------------; \
echo "Failed $$failed_tests out of $$executed_tests tests"; \
echo ---------------------------------; \
elif [ $$failed_tests -eq 0 ]; then \
echo ---------------------------------; \
echo "All $$executed_tests tests passed"; \
echo ---------------------------------; \
fi; \
exit $$failed_tests;

# A commonly used bash command to clean intermediate files. Instead of writing
# it every time re-use this variable.
RM_INTERMEDIATE_FILES := rm -f $(FIRST_MAKEFILE_DIR)/$(failedTestsName) $(FIRST_MAKEFILE_DIR)/$(executedTestsName)

# At the start of the make, we want to start with empty intermediate files.
TRUNCATE_INTERMEDIATE_FILES := cat /dev/null > $(FIRST_MAKEFILE_DIR)/$(failedTestsName) && cat /dev/null > $(FIRST_MAKEFILE_DIR)/$(executedTestsName)

# With trap make sure the clean step is always executed before and after the
# tests run time. Do not leave residual files in the repo.
check:
+@trap "code=\$$?; \
$(RM_INTERMEDIATE_FILES); \
exit \$${code};" EXIT; \
$(TRUNCATE_INTERMEDIATE_FILES); \
$(MAKE) -f $(THIS_FILE) actualCheck;

all: check

.PHONY: all check preCheck actualCheck $(TEST_TARGETS)
.DEFAULT_GOAL := all

11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)
[![Build Status](https://travis-ci.com/keithy/bash-spec-2.svg?branch=master)](https://travis-ci.com/keithy/bash-spec-2)
[![GitHub issues](https://img.shields.io/github/issues/keithy/bash-spec-2.svg)](https://github.com/keithy/bash-spec-2/issues)
[![Latest Version](https://img.shields.io/github/release/keithy/bash-spec-2.svg)](https://github.com/keithy/bash-spec-2/releases)

New for 2.1
===========
#### Supporting other coding styles/formats
```
old format: describe "title" "$( ... )"
alt format: describe "title" && { ... } (most readable but predictably fails one test)
alt format: describe "title" && { ... } (most readable but variables are not locally scoped)
alt format2: describe "title" && ( ... ) (compromise?)
```
#### Assertions on expressions
Expand All @@ -13,7 +18,7 @@ should_succeed
[[ some_expression ]]
should_fail
```
#### Cleaner support for arrays - pass by reference
#### Cleaner support for arrays and vars - pass by reference

```
expect_var varname to_be 5
Expand All @@ -24,6 +29,8 @@ expect_array arrayname to_contain 5

http://redsymbol.net/articles/unofficial-bash-strict-mode/

#### Travis-CI

bash-spec
=========

Expand Down
25 changes: 13 additions & 12 deletions bash-spec-baby.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env bash
#==================================================================================
# Minimal testing framework for bash scripts.
#
# [[ some_expression ]]
# should_succeed
# [[ some_expression ]]
# should_fail
#
# bash-spec Author: Dave Nicolette
# Date: 29 Jul 2014
# Modified by REA Group 2014
# bash-spec-baby by [email protected] 03/2019
## Minimal testing framework for bash scripts.
## usage:
##
## [[ some_expression ]]
## should_succeed
## [[ some_expression ]]
## should_fail
##
## bash-spec Author: Dave Nicolette
## Date: 29 Jul 2014
## Modified by REA Group 2014
## bash-spec-baby by [email protected] 03/2019
#==================================================================================

# http://redsymbol.net/articles/unofficial-bash-strict-mode/
Expand All @@ -29,7 +30,7 @@ exec > "$result_file"
function show_help {
exec 1>&6 6>&-
rm -f -- "$result_file"
grep "^##" $BASH_SOURCE | sed 's/^##.//'
grep "^##" $BASH_SOURCE | sed 's/^##//'
}

function output_results {
Expand Down
10 changes: 5 additions & 5 deletions bash-spec.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
##==================================================================================
## BDD-style testing framework for bash scripts.
##.
##
## expect variable [not] to_be value Compare scalar values for equality
## expect variable [not] to_match regex Regex match
## expect array [not] to_contain value Look for a value in an array
Expand All @@ -14,7 +14,7 @@
## should_succeed
## [[ some_expression ]]
## should_fail
##.
##
# Original Author: Dave Nicolette
# Version 1.0.0 29 Jul 2014
# Release
Expand Down Expand Up @@ -43,7 +43,7 @@ exec > "$result_file"
function show_help {
exec 1>&6 6>&-
rm -f -- "$result_file"
grep "^##" $BASH_SOURCE | sed 's/^##.//'
grep "^##" $BASH_SOURCE | sed 's/^##//'
}

function output_results {
Expand Down Expand Up @@ -246,8 +246,8 @@ eval set -- "$TEMP"
while true; do
case "$1" in
h | help ) show_help; exit 0 ;;
-- ) shift ;;
* ) shift; break ;;
-- ) break ;;
* ) break ;;
esac
done

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/bash-spec.sh"
. "$DIR/../bash-spec.sh"

describe "The bash version test" && {

Expand Down
5 changes: 4 additions & 1 deletion test_counters.sh → fails/test_counters.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /bin/bash
. ./bash-spec.sh

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/../bash-spec.sh"

describe "A simple test" "$(
Expand Down
2 changes: 2 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
These files run the tests for CI, which PASS expected failures

4 changes: 4 additions & 0 deletions tests/test_bash-altfmt.expected_fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is run in the directory context of the Makefile
./fails/test_bash-spec-altfmt.sh | grep "1 FAILED"


Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/bash-spec.sh"
. "$DIR/../bash-spec.sh"

describe "The bash version test" && (

Expand Down
2 changes: 1 addition & 1 deletion test_bash-spec.sh → tests/test_bash-spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/bash-spec.sh"
. "$DIR/../bash-spec.sh"

describe "The bash version test" "$(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_counters.expected_fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is run in the directory context of the Makefile
./fails/test_counters.sh | grep "1 FAILED"


0 comments on commit c86b05b

Please sign in to comment.