|
3 | 3 | # shellcheck disable=SC2030
|
4 | 4 |
|
5 | 5 | setup() {
|
6 |
| - export COMMIT_MESSAGE_WITH_PR="Fix: Commit for testing (#42)" |
| 6 | + export COMMIT_HASH="commitHash1234567890" |
| 7 | + export COMMIT_MESSAGE_WITHOUT_PR="Test: Commit message without PR" |
| 8 | + export COMMIT_MESSAGE_WITH_PR="Test: Commit message with PR (#42)" |
| 9 | + export ORG_NAME="org-name" |
| 10 | + export REPO_NAME="repo-name" |
| 11 | + export TEST_REPO_URL="https://github.com/org-name/repo-name.git" |
| 12 | + export TEST_COMMIT_MESSAGE="$COMMIT_MESSAGE_WITH_PR" |
7 | 13 | }
|
8 | 14 |
|
9 | 15 | # Mocking git commands and basename
|
10 | 16 | git() {
|
11 | 17 | case "$1" in
|
12 |
| - log) echo "$COMMIT_MESSAGE_WITH_PR";; |
13 |
| - rev-parse) echo "1234567890abcdef";; |
14 |
| - config) echo "https://github.com/infinitered/sample-repo.git";; |
| 18 | + log) echo "$TEST_COMMIT_MESSAGE";; |
| 19 | + rev-parse) echo "$COMMIT_HASH";; |
| 20 | + config) echo "$REPO_URL_MOCK";; |
15 | 21 | *) return 1;;
|
16 | 22 | esac
|
17 | 23 | }
|
18 | 24 |
|
| 25 | +# Mock the cd command |
| 26 | +cd() { |
| 27 | + # Store the directory change in a variable for inspection |
| 28 | + # shellcheck disable=SC2034 |
| 29 | + LAST_CD_DIRECTORY="$1" |
| 30 | + # You can also echo a message to indicate that cd was called, if needed |
| 31 | + # echo "cd $1" |
| 32 | +} |
| 33 | + |
19 | 34 | basename() {
|
20 |
| - echo "sample-repo" |
| 35 | + echo "$REPO_NAME" |
21 | 36 | }
|
22 | 37 |
|
23 | 38 | # Source the script to get ParseCommitInfo function
|
24 | 39 | source ./src/scripts/parse_commit_info.sh
|
25 | 40 |
|
26 |
| -@test "It fetches the last commit message" { |
| 41 | +@test "FetchCommitMessage: It fetches the last commit message" { |
27 | 42 | run FetchCommitMessage
|
28 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
29 |
| - [[ $output =~ Fix:\ Commit\ for\ testing\ \(#42\) ]] |
| 43 | + [[ "$output" =~ Test:\ Commit\ message\ with\ PR\ \(#42\) ]] |
30 | 44 | }
|
31 | 45 |
|
32 |
| -@test "It fetches the commit hash" { |
| 46 | +@test "FetchCommitHash: It fetches the commit hash" { |
33 | 47 | run FetchCommitHash
|
34 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
35 |
| - [[ $output =~ 1234567890abcdef ]] |
| 48 | + [[ $output =~ $COMMIT_HASH ]] |
| 49 | +} |
| 50 | + |
| 51 | +@test "GetNormalizedRepoURL: It fetches and normalizes HTTPS repo URL" { |
| 52 | + export REPO_URL_MOCK="https://github.com/org-name/repo-name.git" |
| 53 | + run GetNormalizedRepoURL $REPO_URL_MOCK |
| 54 | + echo "DEBUG: output \"$output\"" |
| 55 | + [[ $output == "https://github.com/org-name/repo-name" ]] |
| 56 | +} |
| 57 | + |
| 58 | +@test "GetNormalizedRepoURL: It fetches and normalizes SSH repo URL" { |
| 59 | + export REPO_URL_MOCK= "[email protected]:org-name/repo-name.git" |
| 60 | + run GetNormalizedRepoURL $REPO_URL_MOCK |
| 61 | + echo "DEBUG: output \"$output\"" |
| 62 | + [[ $output == "https://github.com/org-name/repo-name" ]] |
| 63 | +} |
| 64 | + |
| 65 | +@test "ExtractGitHubOrgAndRepo: It extracts org and repo from HTTPS URL with .git suffix" { |
| 66 | + run ExtractGitHubOrgAndRepo "https://github.com/sample-org/sample-repo.git" |
| 67 | + [ "$status" -eq 0 ] |
| 68 | + [ "$output" = "sample-org sample-repo" ] |
36 | 69 | }
|
37 | 70 |
|
38 |
| -@test "It fetches the repo URL" { |
39 |
| - run FetchRepoURL |
40 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
41 |
| - [[ $output =~ https://github.com/infinitered/sample-repo.git ]] |
| 71 | +@test "ExtractGitHubOrgAndRepo: It extracts org and repo from HTTPS URL without .git suffix" { |
| 72 | + run ExtractGitHubOrgAndRepo "https://github.com/sample-org/sample-repo" |
| 73 | + [ "$status" -eq 0 ] |
| 74 | + [ "$output" = "sample-org sample-repo" ] |
42 | 75 | }
|
43 | 76 |
|
44 |
| -@test "It fetches the repo name" { |
45 |
| - run ParseRepoName |
46 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
47 |
| - [[ $output =~ sample-repo ]] |
| 77 | +@test "ExtractGitHubOrgAndRepo: It throws an error when given an invalid GitHub URL" { |
| 78 | + run ExtractGitHubOrgAndRepo "https://invalid.com/sample-org/sample-repo.git" |
| 79 | + [ "$status" -eq 1 ] |
| 80 | + [[ "$output" == "Error: Not a GitHub URL."* ]] |
48 | 81 | }
|
49 | 82 |
|
50 |
| -@test "It fetches PR number from commit message" { |
51 |
| - # shellcheck disable=SC2030 |
52 |
| - export COMMIT_MESSAGE=$COMMIT_MESSAGE_WITH_PR |
53 |
| - run ExtractPRNumber |
54 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
| 83 | + |
| 84 | +@test "ExtractPRNumber: It fetches PR number from commit message" { |
| 85 | + run ExtractPRNumber "$COMMIT_MESSAGE_WITH_PR" |
55 | 86 | [[ $output =~ \42 ]]
|
56 | 87 | }
|
57 | 88 |
|
58 |
| -@test "It constructs PR link and commit message when PR number is present" { |
59 |
| - # Set up PR number |
60 |
| - export PR_NUMBER=42 |
61 |
| - # shellcheck disable=SC2030 |
62 |
| - export REPO_NAME="sample-repo" |
63 |
| - export COMMIT_MESSAGE="Fix: Commit for testing (#42)" |
| 89 | +@test "ExtractPRNumber: It takes the last possible PR number in messages with multiple potential PR numbers" { |
| 90 | + export TEST_COMMIT_MESSAGE="Fix: Commit for testing (#123) (#42)" |
| 91 | + run ExtractPRNumber "$TEST_COMMIT_MESSAGE" |
| 92 | + echo "DEBUG: output \"$output\"" |
| 93 | + [[ $output =~ \42 ]] # Assuming it extracts the last PR number by default |
| 94 | +} |
64 | 95 |
|
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" ]] |
| 96 | +@test "CreatePRLink: It constructs PR link" { |
| 97 | + export PR_NUMBER=42 |
| 98 | + run CreatePRLink "$ORG_NAME" "$REPO_NAME" "$PR_NUMBER" |
| 99 | + [[ $output == https://github.com/org-name/repo-name/pull/42 ]] |
68 | 100 | unset PR_NUMBER
|
69 |
| - unset REPO_NAME |
70 |
| - unset COMMIT_MESSAGE |
71 | 101 | }
|
72 | 102 |
|
73 |
| -@test "It constructs commit link and commit message when PR number is absent" { |
74 |
| - # Unset PR number to simulate absence |
75 |
| - unset PR_NUMBER |
76 |
| - export REPO_NAME="sample-repo" |
77 |
| - export COMMIT_HASH="1234567890abcdef" |
78 |
| - export COMMIT_MESSAGE="Fix: Commit for testing" |
| 103 | +@test "CreateCommitLink: It constructs commit link" { |
| 104 | + run CreateCommitLink "$ORG_NAME" "$REPO_NAME" "$COMMIT_HASH" |
| 105 | + [[ $output == "https://github.com/org-name/repo-name/commit/$COMMIT_HASH" ]] |
| 106 | +} |
79 | 107 |
|
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 | 108 |
|
83 |
| - >&2 echo "Debug: Output = '$output'" # Verbose log |
| 109 | +@test "FetchCommitMessage: It handles commit messages with special characters" { |
| 110 | + export TEST_COMMIT_MESSAGE="Fix & Improve: Commit for !testing (#42)" |
| 111 | + run FetchCommitMessage |
| 112 | + echo "DEBUG: output \"$output\"" |
| 113 | + [[ $output =~ Fix\ \&\ Improve:\ Commit\ for\ \!testing\ \(#42\) ]] |
| 114 | +} |
84 | 115 |
|
85 |
| - unset REPO_NAME |
86 |
| - unset COMMIT_HASH |
87 |
| - unset COMMIT_MESSAGE |
| 116 | + |
| 117 | +@test "ParseCommitInfo: It handles commit messages with URL-like strings" { |
| 118 | + export TEST_COMMIT_MESSAGE="Fix: See https://example.com/issues/42 for more info (#42)" |
| 119 | + run ParseCommitInfo |
| 120 | + FINAL_MSG=$(echo "$output" | xargs) |
| 121 | + echo "DEBUG FINAL_MSG: \"$FINAL_MSG\"" |
| 122 | + echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\"" |
| 123 | + [[ $FINAL_MSG == "orb($REPO_NAME): Fix: See https://example.com/issues/42 for more info (#42) https://github.com/org-name/repo-name/pull/42" ]] |
88 | 124 | }
|
89 | 125 |
|
90 |
| -@test "It parses and constructs the final commit message" { |
| 126 | +@test "ParseCommitInfo: It parses and constructs the final commit message with PR link" { |
91 | 127 | run ParseCommitInfo
|
92 |
| - final_msg=$(echo "$output" | grep "^Final constructed message:" | cut -d ':' -f 2- | xargs) |
93 |
| - >&2 echo "Debug: Extracted Final Commit Message = '$final_msg'" |
94 |
| - [[ $final_msg == "orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]] |
| 128 | + FINAL_MSG=$(echo "$output" | xargs) |
| 129 | + echo "DEBUG FINAL_MSG: \"$FINAL_MSG\"" |
| 130 | + echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\"" |
| 131 | + [[ $FINAL_MSG == "orb(repo-name): $COMMIT_MESSAGE_WITH_PR https://github.com/org-name/repo-name/pull/42" ]] |
| 132 | +} |
| 133 | + |
| 134 | +@test "ParseCommitInfo: It parses and constructs the final commit message with commit link" { |
| 135 | + export TEST_COMMIT_MESSAGE="$COMMIT_MESSAGE_WITHOUT_PR" |
| 136 | + run ParseCommitInfo |
| 137 | + FINAL_MSG=$(echo "$output" | xargs) |
| 138 | + echo "DEBUG FINAL_MSG: \"$FINAL_MSG\"" |
| 139 | + echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\"" |
| 140 | + [[ $FINAL_MSG == "orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH" ]] |
95 | 141 | }
|
96 | 142 |
|
97 | 143 |
|
98 | 144 | teardown() {
|
| 145 | + unset COMMIT_HASH |
| 146 | + unset COMMIT_MESSAGE |
| 147 | + unset COMMIT_MESSAGE_WITHOUT_PR |
99 | 148 | unset COMMIT_MESSAGE_WITH_PR
|
| 149 | + unset FINAL_MSG |
| 150 | + unset ORG_NAME |
100 | 151 | unset PR_NUMBER
|
101 | 152 | unset REPO_NAME
|
102 |
| - unset COMMIT_MESSAGE |
103 |
| - unset COMMIT_HASH |
104 |
| - unset final_msg |
| 153 | + unset TEST_REPO_URL |
| 154 | + unset TEST_COMMIT_MESSAGE |
105 | 155 | }
|
0 commit comments