diff --git a/src/main/resources/templates/bash/exercise/exercise.bash b/src/main/resources/templates/bash/exercise/exercise.bash deleted file mode 100644 index efb72406889d..000000000000 --- a/src/main/resources/templates/bash/exercise/exercise.bash +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -# TODO: print "Hello World!" to stdout diff --git a/src/main/resources/templates/bash/exercise/script.bash b/src/main/resources/templates/bash/exercise/script.bash new file mode 100644 index 000000000000..1c4b4ceea780 --- /dev/null +++ b/src/main/resources/templates/bash/exercise/script.bash @@ -0,0 +1,14 @@ +# TODO: add shebang + +# TODO: list directory entries + +# TODO: create create_me.txt + +# TODO: delete delete_me.txt + +# TODO: rename rename_me.txt to renamed.txt + +# TODO: replace 2.718 with 3.1415 in numbers.txt + +# TODO: exit with an successful status code +exit 1 diff --git a/src/main/resources/templates/bash/readme b/src/main/resources/templates/bash/readme new file mode 100644 index 000000000000..71d78933ff0e --- /dev/null +++ b/src/main/resources/templates/bash/readme @@ -0,0 +1,22 @@ +# Bash + +1. [task][Shebang](shebang,shebang_custom_message) +Insert a "Shebang" for Bash at the first line of the script. You may assume that Bash is installed at a standard path. + +2. [task][List Directory Entries](list_dir) +Print all entries of the current directory (including hidden files) to stdout. + +3. [task][Create File](file_creation) +Create a file named `file.txt`. + +4. [task][Delete File](file_deletion) +Delete the file named `delete_me.txt`. + +5. [task][Rename File](rename) +Rename "rename_me.txt" to `renamed.txt`. + +6. [task][Find and Replace](replace) +Replace all occurrences of `2.718` with `3.1415` in `numbers.txt`. + +7. [task][Status Code](status_code) +Exit the script with a successful status code. diff --git a/src/main/resources/templates/bash/solution/exercise.bash b/src/main/resources/templates/bash/solution/exercise.bash deleted file mode 100644 index d6954d951641..000000000000 --- a/src/main/resources/templates/bash/solution/exercise.bash +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -echo "Hello World!" diff --git a/src/main/resources/templates/bash/solution/script.bash b/src/main/resources/templates/bash/solution/script.bash new file mode 100644 index 000000000000..aaa638f3dd4f --- /dev/null +++ b/src/main/resources/templates/bash/solution/script.bash @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +ls -a + +touch create_me.txt + +rm delete_me.txt + +mv rename_me.txt renamed.txt + +sed -i 's/2\.718/3.1415/g' numbers.txt + +exit 0 diff --git a/src/main/resources/templates/bash/test/test.bats b/src/main/resources/templates/bash/test/test.bats index f7a571ffa8ec..e5d886c22ed0 100644 --- a/src/main/resources/templates/bash/test/test.bats +++ b/src/main/resources/templates/bash/test/test.bats @@ -1,9 +1,72 @@ +setup_file() { + BATS_TEST_TIMEOUT=10 +} + setup() { load "test_helper/common-setup" _common_setup + + TEST_DATA="$BATS_TEST_DIRNAME/test_data" + + cp "$TEST_DATA"/{numbers.txt,rename_me.txt} "$BATS_TEST_TMPDIR" + + cd "$BATS_TEST_TMPDIR" + touch delete_me.txt + touch .hidden +} + +@test "shebang" { + first_line=$(head -n 1 "$ASSIGNMENT_ROOT/script.bash") + assert_regex "$first_line" '^#!(/usr)?/bin/(env )?bash$' +} + +@test "shebang_custom_message" { + first_line=$(head -n 1 "$ASSIGNMENT_ROOT/script.bash") + + if ! assert_regex "$first_line" '^#!(/usr)?/bin/(env )?bash$' 2>/dev/null; then + echo "$first_line" \ + | batslib_decorate "first line is not a valid shebang" \ + | fail + fi +} + +@test "list_dir" { + run script.bash + + assert_output --partial delete_me.txt + assert_output --partial numbers.txt + assert_output --partial rename_me.txt + assert_output --partial .hidden } -@test "hello world" { - run exercise.bash - assert_output "Hello World!" +@test "file_creation" { + run script.bash + + assert_file_exists file.txt +} + +@test "file_deletion" { + run script.bash + + assert_file_not_exists delete_me.txt +} + +@test "rename" { + run script.bash + + assert_file_not_exists rename_me.txt + assert_file_exists renamed.txt + _assert_file_contents renamed.txt "$TEST_DATA/rename_me.txt" +} + +@test "replace" { + run script.bash + + _assert_file_contents numbers.txt "$TEST_DATA/numbers_expected.txt" +} + +@test "status_code" { + run script.bash + + assert_success } diff --git a/src/main/resources/templates/bash/test/test_data/numbers.txt b/src/main/resources/templates/bash/test/test_data/numbers.txt new file mode 100644 index 000000000000..a252bd33309e --- /dev/null +++ b/src/main/resources/templates/bash/test/test_data/numbers.txt @@ -0,0 +1,3 @@ +2.718 +2.718 2.718 2.718 +21718 diff --git a/src/main/resources/templates/bash/test/test_data/numbers_expected.txt b/src/main/resources/templates/bash/test/test_data/numbers_expected.txt new file mode 100644 index 000000000000..44121ba5f026 --- /dev/null +++ b/src/main/resources/templates/bash/test/test_data/numbers_expected.txt @@ -0,0 +1,3 @@ +3.1415 +3.1415 3.1415 3.1415 +21718 diff --git a/src/main/resources/templates/bash/test/test_data/rename_me.txt b/src/main/resources/templates/bash/test/test_data/rename_me.txt new file mode 100644 index 000000000000..0bc73a7a2f76 --- /dev/null +++ b/src/main/resources/templates/bash/test/test_data/rename_me.txt @@ -0,0 +1 @@ +example content diff --git a/src/main/resources/templates/bash/test/test_helper/common-setup.bash b/src/main/resources/templates/bash/test/test_helper/common-setup.bash index 72a2e85be8e5..23bca120a979 100644 --- a/src/main/resources/templates/bash/test/test_helper/common-setup.bash +++ b/src/main/resources/templates/bash/test/test_helper/common-setup.bash @@ -1,10 +1,34 @@ _common_setup() { - load "/usr/lib/bats/bats-support/load" - load "/usr/lib/bats/bats-assert/load" - # get the containing directory of this file - # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, - # as those will point to the bats executable's location or the preprocessed file respectively - PROJECT_ROOT="$( cd "$( dirname "$BATS_TEST_FILENAME" )/.." >/dev/null 2>&1 && pwd )" - # make executables in ${studentParentWorkingDirectoryName}/ visible to PATH - PATH="$PROJECT_ROOT/${studentParentWorkingDirectoryName}:$PATH" + bats_load_library "bats-support" + bats_load_library "bats-assert" + bats_load_library "bats-file" + + PROJECT_ROOT="$( cd "$BATS_TEST_DIRNAME/.." >/dev/null 2>&1 && pwd )" + ASSIGNMENT_ROOT="$PROJECT_ROOT/${studentParentWorkingDirectoryName}" + + PATH="$ASSIGNMENT_ROOT:$PATH" } + +# _assert_file_contents +# ============ +# +# Fail if the actual and expected file contents differ. +# +# Usage: _assert_file_contents +# +# IO: +# STDERR - unified diff, on failure +# Options: +# The file being compared. +# The file to compare against. +_assert_file_contents() { + if ! diff_output=$(diff -u --label="actual" --label="expected" "$1" "$2" 2>&1); then + echo "$diff_output" \ + | batslib_decorate "$1: file contents differ" \ + | fail + fi +} + +# reduce output +bats_print_stack_trace() { :; } +bats_print_failed_command() { :; }