Skip to content

Commit 0c79496

Browse files
authored
Merge pull request #140 from C2SM/replace_ed_with_sed
Replace ed command with sed in beginner's helpers
2 parents dba2890 + 6e37702 commit 0c79496

File tree

2 files changed

+125
-11
lines changed

2 files changed

+125
-11
lines changed

beginner/helpers.sh

+11-11
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ init_simple_repo () {
6666
git add schedule_day1.txt && git commit -m "Add schedule_day1"
6767
git add schedule_day2.txt && git commit -m "Add schedule_day2"
6868

69-
printf "/program/\na\n09:00-11:00: Poster session\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
70-
printf "/program/\na\n09:00-11:00: Poster session\n.\nw\nq\n" | ed -s schedule_day2.txt > /dev/null
69+
sed -i '/program/a 09:00-11:00: Poster session' schedule_day1.txt > /dev/null
70+
sed -i '/program/a 09:00-11:00: Poster session' schedule_day2.txt > /dev/null
7171
git add * && git commit -m "Add poster sessions in the morning"
7272

73-
printf "/session/\na\n11:00-11:15: Coffee break\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
74-
printf "/session/\na\n11:00-11:15: Coffee break\n.\nw\nq\n" | ed -s schedule_day2.txt > /dev/null
73+
sed -i '/session/a 11:00-11:15: Coffee break' schedule_day1.txt > /dev/null
74+
sed -i '/session/a 11:00-11:15: Coffee break' schedule_day2.txt > /dev/null
7575
git add * && git commit -m "Add coffee break"
7676

7777
echo ""
@@ -94,8 +94,8 @@ init_simple_repo_remote () {
9494
git clone conference_planning conference_planning_remote
9595
cd conference_planning_remote
9696
git checkout -b "updated_schedules"
97-
printf "/break/\na\n11:15-12:15: Talk professor A.\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
98-
printf "/break/\na\n11:15-12:15: Talk professor B.\n.\nw\nq\n" | ed -s schedule_day2.txt > /dev/null
97+
sed -i '/break/a 11:15-12:15: Talk professor A.' schedule_day1.txt > /dev/null
98+
sed -i '/break/a 11:15-12:15: Talk professor B.' schedule_day2.txt > /dev/null
9999
git add * && git commit -m "update schedules"
100100

101101
# Dynamically get the default branch name and switch to it
@@ -109,8 +109,8 @@ init_simple_repo_remote () {
109109
init_repo () {
110110
init_simple_repo &> /dev/null
111111

112-
printf "/break/\na\n11:15-12:15: Workshop ice crystal formation\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
113-
printf "/break/\na\n11:15-12:15: Workshop secondary ice\n.\nw\nq\n" | ed -s schedule_day2.txt > /dev/null
112+
sed -i '/break/a 11:15-12:15: Workshop ice crystal formation' schedule_day1.txt > /dev/null
113+
sed -i '/break/a 11:15-12:15: Workshop secondary ice' schedule_day2.txt > /dev/null
114114
git add * && git commit -m "Add workshops"
115115

116116
sed 's/Poster session/Talk professor C./g' schedule_day1.txt > schedule_day1_tmp.txt
@@ -146,13 +146,13 @@ init_repo_remote () {
146146
git add schedule_day1.txt && git commit -m "Add schedule_day1"
147147

148148
# Edit schedule_day1.txt
149-
printf "/program/\na\n09:00-11:00: Poster session\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
149+
sed -i '/program/a 09:00-11:00: Poster session' schedule_day1.txt > /dev/null
150150
git add * && git commit -m "Add poster sessions in the morning"
151151

152-
printf "/session/\na\n11:00-11:15: Coffee break\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
152+
sed -i '/session/a 11:00-11:15: Coffee break' schedule_day1.txt > /dev/null
153153
git add * && git commit -m "Add coffee breaks"
154154

155-
printf "/break/\na\n11:15-12:15: Talk professor A.\n12:15-13:30: Lunch\n13:30-15:00: Workshop\n15:00-16:00: Talk professor B.\n.\nw\nq\n" | ed -s schedule_day1.txt > /dev/null
155+
sed -i "/break/a 11:15-12:15: Talk professor A.\n12:15-13:30: Lunch\n13:30-15:00: Workshop\n15:00-16:00: Talk professor B." schedule_day1.txt > /dev/null
156156
git add * && git commit -m "Add rest of daily program"
157157

158158
cd ..

beginner/tests/test_helpers.sh

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Calculate the absolute path to helpers.sh
6+
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
7+
HELPERS_PATH="$SCRIPT_DIR/../helpers.sh"
8+
9+
if [ ! -f "$HELPERS_PATH" ]; then
10+
echo -e "\033[31m\033[1mError: helpers.sh not found at $HELPERS_PATH\033[0m"
11+
exit 1
12+
fi
13+
14+
source "$HELPERS_PATH"
15+
16+
# Array to store test results
17+
declare -a TEST_RESULTS
18+
19+
# Function to handle test failures
20+
fail_test() {
21+
TEST_RESULTS+=("$1: \033[31m\033[1mFAIL\033[0m")
22+
}
23+
24+
# Function to handle test successes
25+
pass_test() {
26+
TEST_RESULTS+=("$1: \033[32m\033[1mPASS\033[0m")
27+
}
28+
29+
# Test reset function
30+
echo "Testing reset function..."
31+
reset_output=$(reset)
32+
if [[ "$reset_output" == *"Here we go again!"* ]]; then
33+
pass_test "reset function"
34+
else
35+
fail_test "reset function"
36+
fi
37+
38+
# Test get_default_branch_name function
39+
echo "Testing get_default_branch_name function..."
40+
default_branch=$(get_default_branch_name)
41+
if [[ "$default_branch" == "main" || "$default_branch" == "master" ]]; then
42+
pass_test "get_default_branch_name function"
43+
else
44+
fail_test "get_default_branch_name function"
45+
fi
46+
47+
# Test init_exercise function
48+
echo "Testing init_exercise function..."
49+
init_exercise
50+
if [[ -d "$SCRIPT_DIR/../../../beginners_git" ]]; then
51+
pass_test "init_exercise function"
52+
else
53+
fail_test "init_exercise function"
54+
fi
55+
56+
# Test init_repo_empty_schedule function
57+
echo "Testing init_repo_empty_schedule function..."
58+
init_repo_empty_schedule
59+
if [[ -f "$SCRIPT_DIR/../../../beginners_git/conference_planning/conference_schedule.txt" ]]; then
60+
pass_test "init_repo_empty_schedule function"
61+
else
62+
fail_test "init_repo_empty_schedule function"
63+
fi
64+
65+
# Test init_simple_repo function
66+
echo "Testing init_simple_repo function..."
67+
init_simple_repo
68+
if [[ -d "$SCRIPT_DIR/../../../beginners_git/conference_planning/.git" ]]; then
69+
pass_test "init_simple_repo function"
70+
else
71+
fail_test "init_simple_repo function"
72+
fi
73+
74+
# Test init_simple_repo_remote function
75+
echo "Testing init_simple_repo_remote function..."
76+
init_simple_repo_remote
77+
if [[ -d "$SCRIPT_DIR/../../../beginners_git/conference_planning_remote/.git" ]]; then
78+
pass_test "init_simple_repo_remote function"
79+
else
80+
fail_test "init_simple_repo_remote function"
81+
fi
82+
83+
# Test init_repo function
84+
echo "Testing init_repo function..."
85+
init_repo
86+
if [[ -d "$SCRIPT_DIR/../../../beginners_git/conference_planning/.git" ]]; then
87+
pass_test "init_repo function"
88+
else
89+
fail_test "init_repo function"
90+
fi
91+
92+
# Test init_repo_remote function
93+
echo "Testing init_repo_remote function..."
94+
init_repo_remote
95+
if [[ -d "$SCRIPT_DIR/../../../beginners_git/conference_planning/.git" && -d "$SCRIPT_DIR/../../../beginners_git/conference_planning_remote/" ]]; then
96+
pass_test "init_repo_remote function"
97+
else
98+
fail_test "init_repo_remote function"
99+
fi
100+
101+
# Test commit_to_remote_by_third_party function
102+
echo "Testing commit_to_remote_by_third_party function..."
103+
commit_to_remote_by_third_party
104+
if [[ -d "$SCRIPT_DIR/../../../beginners_git/conference_planning/.git" && -d "$SCRIPT_DIR/../../../beginners_git/conference_planning_remote/" ]]; then
105+
pass_test "commit_to_remote_by_third_party function"
106+
else
107+
fail_test "commit_to_remote_by_third_party function"
108+
fi
109+
110+
# Print summary
111+
echo -e "\n\033[1mTest Summary:\033[0m"
112+
for result in "${TEST_RESULTS[@]}"; do
113+
echo -e "$result"
114+
done

0 commit comments

Comments
 (0)