Skip to content

Commit 9b4cea6

Browse files
committed
add test and github workflow
1 parent 53b5f58 commit 9b4cea6

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

.github/workflows/tests.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
tests:
11+
name: "Run tests"
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: "Install bashunit"
18+
run: "curl -s https://bashunit.typeddevs.com/install.sh"
19+
20+
- name: "Test"
21+
run: "./lib/bashunit tests/*_test.sh"

cruft_remover.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
read -rp "Which folder you want to cruft? " FOLDER
3+
read -rp "Which folder you want to scan? " FOLDER
44

55
if [[ ! -d "$FOLDER" ]]; then
66
echo "Folder you type is not exists."
@@ -12,10 +12,12 @@ read -rp "How many days files should be unmodified for to be considered cruft? "
1212
readarray -t FILES < <(find "$FOLDER" -maxdepth 1 -atime +"$DAY" -type f)
1313

1414
if [ "${#FILES[@]}" -eq 0 ]; then
15-
echo "The folder of $FOLDER does not contains any file"
15+
echo "There is no files matched."
1616
exit 1
1717
fi
1818

1919
for FILE in "${FILES[@]}"; do
2020
rm "$FILE";
2121
done
22+
23+
exit 0

tests/cruft_remover_test.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
#!/usr/bin/env bash
22

33
# Set up a temporary test directory
4-
TEST_DIR="./tmp"
4+
TEST_DIR="tmp"
5+
TEST_DIR_NOT_CREATED="tmp_not_created"
56

67
function set_up() {
78
mkdir -p "$TEST_DIR"
89

9-
touch -d "10 days ago" "$TEST_DIR/file_1.txt"
10+
touch -d "2 days ago" "$TEST_DIR/file_1.txt" "$TEST_DIR/file_2.txt"
1011

1112
touch "$TEST_DIR/file_3.txt"
1213
}
1314

14-
function teat_down() {
15+
function tear_down() {
1516
rm -rf "$TEST_DIR"
1617
}
1718

1819
function test_files_to_be_delete() {
19-
local result
20-
result=$(echo -e "$TEST_DIR\n9" | ./cruft_remover.sh)
20+
local result="$(echo -e "$TEST_DIR\n1" | ./cruft_remover.sh)"
2121

22-
assert_not_contains $result "The folder of $TEST_DIR does not contains any file"
23-
assert_contains "$(ls $TEST_DIR)" "file_3.txt"
22+
assert_equals "$(ls $TEST_DIR)" "file_3.txt"
23+
}
24+
25+
function test_files_cannot_to_be_delete() {
26+
local result="$(echo -e "$TEST_DIR\n2" | ./cruft_remover.sh)"
27+
28+
assert_equals "$result" "There is no files matched."
29+
}
30+
31+
function test_wrong_dir_to_scan() {
32+
local result="$(echo -e "$TEST_DIR_NOT_CREATED" | ./cruft_remover.sh)"
33+
34+
assert_equals "$result" "Folder you type is not exists."
2435
}

tmp/file_3.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)