Skip to content

Commit

Permalink
WIP: Add example exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
magaupp committed Jan 6, 2025
1 parent ca754d4 commit 7d7ecad
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/main/resources/templates/bash/exercise/exercise.bash

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/resources/templates/bash/exercise/script.bash
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions src/main/resources/templates/bash/readme
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 0 additions & 3 deletions src/main/resources/templates/bash/solution/exercise.bash

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/resources/templates/bash/solution/script.bash
Original file line number Diff line number Diff line change
@@ -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
69 changes: 66 additions & 3 deletions src/main/resources/templates/bash/test/test.bats
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions src/main/resources/templates/bash/test/test_data/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2.718
2.718 2.718 2.718
21718
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3.1415
3.1415 3.1415 3.1415
21718
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example content
Original file line number Diff line number Diff line change
@@ -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 <actual_path> <expected_path>
#
# IO:
# STDERR - unified diff, on failure
# Options:
# <actual_path> The file being compared.
# <expected_path> 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() { :; }

0 comments on commit 7d7ecad

Please sign in to comment.