-
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.
- Loading branch information
Showing
10 changed files
with
158 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 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 |
40 changes: 32 additions & 8 deletions
40
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 |
---|---|---|
@@ -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() { :; } |