11#! /bin/bash
2+ # shellcheck disable=SC2031
3+ # shellcheck disable=SC2030
24
3- COMMIT_MESSAGE_WITH_PR=" Fix: Commit for testing (#42)"
5+ setup () {
6+ export COMMIT_MESSAGE_WITH_PR=" Fix: Commit for testing (#42)"
7+ }
48
59# Mocking git commands and basename
610git () {
711 case " $1 " in
8- log) echo $COMMIT_MESSAGE_WITH_PR ;;
12+ log) echo " $COMMIT_MESSAGE_WITH_PR " ;;
913 rev-parse) echo " 1234567890abcdef" ;;
1014 config) echo " https://github.com/infinitered/sample-repo.git" ;;
1115 * ) return 1;;
@@ -21,32 +25,33 @@ source ./src/scripts/parse_commit_info.sh
2125
2226@test " It fetches the last commit message" {
2327 run FetchCommitMessage
24- echo " Debug: Output = '$output '" # Verbose log
28+ >&2 echo " Debug: Output = '$output '" # Verbose log
2529 [[ $output =~ Fix:\ Commit\ for\ testing\ \( # 42\) ]]
2630}
2731
2832@test " It fetches the commit hash" {
2933 run FetchCommitHash
30- echo " Debug: Output = '$output '" # Verbose log
34+ >&2 echo " Debug: Output = '$output '" # Verbose log
3135 [[ $output =~ 1234567890abcdef ]]
3236}
3337
3438@test " It fetches the repo URL" {
3539 run FetchRepoURL
36- echo " Debug: Output = '$output '" # Verbose log
40+ >&2 echo " Debug: Output = '$output '" # Verbose log
3741 [[ $output =~ https://github.com/infinitered/sample-repo.git ]]
3842}
3943
4044@test " It fetches the repo name" {
4145 run ParseRepoName
42- echo " Debug: Output = '$output '" # Verbose log
46+ >&2 echo " Debug: Output = '$output '" # Verbose log
4347 [[ $output =~ sample-repo ]]
4448}
4549
4650@test " It fetches PR number from commit message" {
51+ # shellcheck disable=SC2030
4752 export COMMIT_MESSAGE=$COMMIT_MESSAGE_WITH_PR
4853 run ExtractPRNumber
49- echo " Debug: Output = '$output '" # Verbose log
54+ >&2 echo " Debug: Output = '$output '" # Verbose log
5055 [[ $output =~ \4 2 ]]
5156}
5257
@@ -57,10 +62,9 @@ source ./src/scripts/parse_commit_info.sh
5762 export REPO_NAME=" sample-repo"
5863 export COMMIT_MESSAGE=" Fix: Commit for testing (#42)"
5964
60- run ConstructCommitMessage
61- echo " Debug: Output = '$output '" # Verbose log
62- [[ $output =~ https://github.com/infinitered/sample-repo/pull/42 ]]
63- [[ $output =~ orb:\ sample-repo\ --\ Fix:\ Commit\ for\ testing\ \( # 42\)\ --\ https://github.com/infinitered/sample-repo/pull/42 ]]
65+ run ConstructCommitMessage " $REPO_NAME " " $COMMIT_MESSAGE " " $PR_NUMBER " " $COMMIT_HASH "
66+ >&2 echo " Debug: Output = '$output '" # Verbose log
67+ [[ $output == " orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]]
6468 unset PR_NUMBER
6569 unset REPO_NAME
6670 unset COMMIT_MESSAGE
@@ -69,23 +73,32 @@ source ./src/scripts/parse_commit_info.sh
6973@test " It constructs commit link and commit message when PR number is absent" {
7074 # Unset PR number to simulate absence
7175 unset PR_NUMBER
72- # shellcheck disable=SC2031
7376 export REPO_NAME=" sample-repo"
7477 export COMMIT_HASH=" 1234567890abcdef"
7578 export COMMIT_MESSAGE=" Fix: Commit for testing"
7679
77- run ConstructCommitMessage
78- echo " Debug: Output = ' $output ' " # Verbose log
79- [[ $output =~ https://github.com/infinitered/sample-repo/commit/1234567890abcdef ]]
80- [[ $output =~ orb: \ sample-repo \ -- \ Fix: \ Commit \ for \ testing \ -- \ https://github.com/infinitered/sample-repo/commit/1234567890abcdef ]]
80+ run ConstructCommitMessage " $REPO_NAME " " $COMMIT_MESSAGE " " $PR_NUMBER " " $COMMIT_HASH "
81+ [[ $output =~ orb \( sample-repo \) : \ Fix: \ Commit \ for \ testing \ https://github.com/infinitered/sample-repo/commit/ $COMMIT_HASH ]]
82+ >&2 echo " Debug: Output = ' $output ' " # Verbose log
83+
8184 unset REPO_NAME
8285 unset COMMIT_HASH
8386 unset COMMIT_MESSAGE
8487}
8588
8689@test " It parses and constructs the final commit message" {
8790 run ParseCommitInfo
88- echo " Debug: Output = '$output '" # Verbose log
89- [[ $output == " orb: sample-repo -- Fix: Commit for testing (#42) -- https://github.com/infinitered/sample-repo/pull/42" ]]
91+ final_msg=$( echo " $output " | grep " ^Final constructed message:" | cut -d ' :' -f 2- | xargs)
92+ >&2 echo " Debug: Extracted Final Commit Message = '$final_msg '"
93+ [[ $final_msg == " orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]]
9094}
9195
96+
97+ teardown () {
98+ unset COMMIT_MESSAGE_WITH_PR
99+ unset PR_NUMBER
100+ unset REPO_NAME
101+ unset COMMIT_MESSAGE
102+ unset COMMIT_HASH
103+ unset final_msg
104+ }
0 commit comments