-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Programming exercises: Add Bash programming exercise template (#10089)
- Loading branch information
Showing
21 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
export AEOLUS_INITIAL_DIRECTORY=${PWD} | ||
set_permissions () { | ||
echo '⚙️ executing set_permissions' | ||
find "${studentParentWorkingDirectoryName}" -type f -exec chmod +x "{}" + | ||
} | ||
|
||
create_results_directory () { | ||
echo '⚙️ executing create_results_directory' | ||
mkdir results | ||
} | ||
|
||
test () { | ||
echo '⚙️ executing test' | ||
bats --report-formatter junit --output results "${testWorkingDirectory}" || true | ||
} | ||
|
||
main () { | ||
if [[ "${1}" == "aeolus_sourcing" ]]; then | ||
return 0 # just source to use the methods in the subshell, no execution | ||
fi | ||
local _script_name | ||
_script_name=${BASH_SOURCE[0]:-$0} | ||
cd "${AEOLUS_INITIAL_DIRECTORY}" | ||
bash -c "source ${_script_name} aeolus_sourcing; set_permissions" | ||
cd "${AEOLUS_INITIAL_DIRECTORY}" | ||
bash -c "source ${_script_name} aeolus_sourcing; create_results_directory" | ||
cd "${AEOLUS_INITIAL_DIRECTORY}" | ||
bash -c "source ${_script_name} aeolus_sourcing; test" | ||
} | ||
|
||
main "${@}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
api: v0.0.1 | ||
metadata: | ||
name: "Bash" | ||
id: bash | ||
actions: | ||
- name: set_permissions | ||
script: 'find "${studentParentWorkingDirectoryName}" -type f -exec chmod +x "{}" +' | ||
- name: create_results_directory | ||
script: 'mkdir results' | ||
- name: test | ||
script: 'bats --report-formatter junit --output results "${testWorkingDirectory}" || true' | ||
results: | ||
- name: Bats Test Results | ||
path: "results/*.xml" | ||
type: junit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Bash Scripting Exercise | ||
|
||
This exercise is designed to help you practice basic Bash scripting skills. | ||
Complete the following tasks by writing a Bash script that performs each action. | ||
|
||
1. [task][Add a Shebang](shebang,shebang_custom_message) | ||
Insert a "shebang" at the top of your script to specify that it should be executed with Bash. | ||
Assume Bash is installed at a standard path. | ||
|
||
2. [task][List Directory Entries](list_dir) | ||
Write a command to list all entries in the current directory, including hidden files, and print the output to the terminal. | ||
|
||
3. [task][Create File](file_creation) | ||
Create a file named `create_me.txt` in the current directory. | ||
|
||
4. [task][Delete a File](file_deletion) | ||
Delete the file named `delete_me.txt` from the current directory. | ||
|
||
5. [task][Rename a File](rename) | ||
Rename the file `rename_me.txt` to `renamed.txt`. | ||
|
||
6. [task][Find and Replace Text](replace) | ||
Replace all occurrences of the number `2.718` with `3.1415` in the file `numbers.txt`. Ensure the changes are saved. | ||
|
||
7. [task][Exit with Success](status_code) | ||
Exit the script with a successful status code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 "file_creation" { | ||
run script.bash | ||
|
||
assert_file_exists create_me.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2.718 | ||
2.718 2.718 2.718 | ||
21718 |
3 changes: 3 additions & 0 deletions
3
src/main/resources/templates/bash/test/test_data/numbers_expected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
3.1415 | ||
3.1415 3.1415 3.1415 | ||
21718 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
example content |
34 changes: 34 additions & 0 deletions
34
src/main/resources/templates/bash/test/test_helper/common-setup.bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
_common_setup() { | ||
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() { :; } |
55 changes: 55 additions & 0 deletions
55
src/main/resources/templates/jenkins/bash/regularRuns/pipeline.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* This file configures the actual build steps for the automatic grading. | ||
* | ||
* !!! | ||
* For regular exercises, there is no need to make changes to this file. | ||
* Only this base configuration is actively supported by the Artemis maintainers | ||
* and/or your Artemis instance administrators. | ||
* !!! | ||
*/ | ||
|
||
dockerImage = '#dockerImage' | ||
dockerFlags = '#dockerArgs' | ||
|
||
/** | ||
* Main function called by Jenkins. | ||
*/ | ||
void testRunner() { | ||
docker.image(dockerImage).inside(dockerFlags) { c -> | ||
runTestSteps() | ||
} | ||
} | ||
|
||
private void runTestSteps() { | ||
test() | ||
} | ||
|
||
/** | ||
* Run unit tests | ||
*/ | ||
private void test() { | ||
stage('Set Permissions') { | ||
sh 'find ./assignment -type f -exec chmod +x "{}" +' | ||
} | ||
stage('Create Results Directory') { | ||
sh 'mkdir results' | ||
} | ||
stage('Test') { | ||
sh 'bats --report-formatter junit --output results ./tests || true' | ||
} | ||
} | ||
|
||
/** | ||
* Script of the post build tasks aggregating all JUnit files in $WORKSPACE/results. | ||
* | ||
* Called by Jenkins. | ||
*/ | ||
void postBuildTasks() { | ||
sh ''' | ||
sed -i 's/[^[:print:]\t]/�/g' $WORKSPACE/results/*.xml || true | ||
''' | ||
} | ||
|
||
// very important, do not remove | ||
// required so that Jenkins finds the methods defined in this script | ||
return this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.