diff --git a/src/engine/commit_message.go b/src/engine/commit_message.go new file mode 100644 index 00000000..98d20569 --- /dev/null +++ b/src/engine/commit_message.go @@ -0,0 +1,46 @@ +/* +Copyright (c) 2024 Murex + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package engine + +import "fmt" + +// CommitMessage is a struct that holds information of the commit message +type CommitMessage struct { + Emoji rune + Tag string + Description string +} + +var ( + messagePassed = CommitMessage{Emoji: '✅', Tag: "[TCR - PASSED]", Description: "tests passing"} + messageFailed = CommitMessage{Emoji: '❌', Tag: "[TCR - FAILED]", Description: "tests failing"} + messageReverted = CommitMessage{Emoji: '⏪', Tag: "[TCR - REVERTED]", Description: "revert changes"} +) + +func (cm CommitMessage) toString(withEmoji bool) string { + textOnly := fmt.Sprintf("%s %s", cm.Tag, cm.Description) + if withEmoji { + return fmt.Sprintf("%c %s", cm.Emoji, textOnly) + } + return textOnly +} diff --git a/src/engine/commit_message_test.go b/src/engine/commit_message_test.go new file mode 100644 index 00000000..71032a28 --- /dev/null +++ b/src/engine/commit_message_test.go @@ -0,0 +1,80 @@ +/* +Copyright (c) 2024 Murex + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package engine + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_building_commit_messages(t *testing.T) { + tests := []struct { + desc string + commitMessage CommitMessage + withEmoji bool + expected string + }{ + { + "Passing Message", + CommitMessage{ + Emoji: '✅', + Tag: "[TCR - PASSED]", + Description: "tests passing", + }, + true, + passedCommitMessage, + }, { + "Failing Message", + CommitMessage{ + Emoji: '❌', + Tag: "[TCR - FAILED]", + Description: "tests failing", + }, + true, + failedCommitMessage, + }, { + "Reverting Message", + CommitMessage{ + Emoji: '⏪', + Tag: "[TCR - REVERTED]", + Description: "revert changes", + }, + true, + revertedCommitMessage, + }, { + "Passing Message without Emoji", + CommitMessage{ + Emoji: '✅', + Tag: "[TCR - PASSED]", + Description: "tests passing", + }, + false, + "[TCR - PASSED] tests passing", + }, + } + for _, test := range tests { + t.Run(test.desc, func(t *testing.T) { + assert.Equal(t, test.expected, test.commitMessage.toString(test.withEmoji)) + }) + } +} diff --git a/src/engine/tcr.go b/src/engine/tcr.go index 54f268f4..6f09a101 100644 --- a/src/engine/tcr.go +++ b/src/engine/tcr.go @@ -24,7 +24,6 @@ package engine import ( "errors" - "fmt" "github.com/murex/tcr/checker" "github.com/murex/tcr/events" "github.com/murex/tcr/filesystem" @@ -118,27 +117,6 @@ const ( testSuccessMessage = "Tests passed!" ) -// CommitMessage is a struct that holds information of the commit message -type CommitMessage struct { - Emoji rune - Tag string - Description string -} - -var ( - messagePassed = CommitMessage{Emoji: '✅', Tag: "[TCR - PASSED]", Description: "tests passing"} - messageFailed = CommitMessage{Emoji: '❌', Tag: "[TCR - FAILED]", Description: "tests failing"} - messageReverted = CommitMessage{Emoji: '⏪', Tag: "[TCR - REVERTED]", Description: "revert changes"} -) - -func (cm CommitMessage) toString(withEmoji bool) string { - textOnly := fmt.Sprintf("%s %s", cm.Tag, cm.Description) - if withEmoji { - return fmt.Sprintf("%c %s", cm.Emoji, textOnly) - } - return textOnly -} - var ( // TCR is TCR Engine singleton instance TCR TCRInterface @@ -275,7 +253,13 @@ func isTCRCommitMessage(msg string) bool { return strings.Index(msg, messagePassed.toString(true)) == 0 || strings.Index(msg, messageFailed.toString(true)) == 0 } -func parseCommitMessage(message string) (event events.TCREvent) { +func parseCommitMessage(message string) events.TCREvent { + header, event := parseCommitHeaderAndEvents(message) + event.Status = parseCommitStatus(header) + return event +} + +func parseCommitHeaderAndEvents(message string) (string, events.TCREvent) { // First line is the main commit message // Second line is a blank line // The YAML-structured data starts on the third line until we reach a blank line @@ -305,17 +289,20 @@ func parseCommitMessage(message string) (event events.TCREvent) { continue } } + event := events.FromYAML(statsYAML.String()) + return header, event +} - event = events.FromYAML(statsYAML.String()) - switch header { - case messagePassed.toString(true): - event.Status = events.StatusPass - case messageFailed.toString(true): - event.Status = events.StatusFail - default: - event.Status = events.StatusUnknown +func parseCommitStatus(header string) events.CommandStatus { + commitStatus := events.StatusUnknown + if strings.Contains(header, messagePassed.Tag) { + commitStatus = events.StatusPass + } else if strings.Contains(header, messageFailed.Tag) { + commitStatus = events.StatusFail + } else { + commitStatus = events.StatusUnknown } - return event + return commitStatus } func (tcr *TCREngine) setMessageSuffix(suffix string) { diff --git a/src/engine/tcr_test.go b/src/engine/tcr_test.go index e62ef579..b6d5c082 100644 --- a/src/engine/tcr_test.go +++ b/src/engine/tcr_test.go @@ -798,17 +798,53 @@ func Test_tcr_print_log(t *testing.T) { } } +func Test_parse_commit_message_type_tag(t *testing.T) { + testFlags := []struct { + desc string + commitMessage string + expectedStatus events.CommandStatus + }{ + { + desc: "empty commit message", + commitMessage: "", + expectedStatus: events.StatusUnknown, + }, + { + desc: "passing commit", + commitMessage: messagePassed.Tag, + expectedStatus: events.StatusPass, + }, + { + desc: "failing commit", + commitMessage: messageFailed.Tag, + expectedStatus: events.StatusFail, + }, + { + desc: "revert commit are unknown status", + commitMessage: messageReverted.Tag, + expectedStatus: events.StatusUnknown, + }, + { + desc: "non-TCR commit", + commitMessage: "Joe was here!", + expectedStatus: events.StatusUnknown, + }, + } + + for _, tt := range testFlags { + t.Run(tt.desc, func(t *testing.T) { + event := parseCommitMessage(tt.commitMessage) + assert.Equal(t, tt.expectedStatus, event.Status) + }) + } +} + func Test_parse_commit_message(t *testing.T) { testFlags := []struct { desc string commitMessage string expected events.TCREvent }{ - { - desc: "empty commit message", - commitMessage: "", - expected: events.TCREvent{Status: events.StatusUnknown}, - }, { desc: "test-passing commit", commitMessage: passedCommitMessage + "\n" + @@ -1046,55 +1082,3 @@ func Test_count_files(t *testing.T) { }) } } - -func Test_building_commit_messages(t *testing.T) { - tests := []struct { - desc string - commitMessage CommitMessage - withEmoji bool - expected string - }{ - { - "Passing Message", - CommitMessage{ - Emoji: '✅', - Tag: "[TCR - PASSED]", - Description: "tests passing", - }, - true, - passedCommitMessage, - }, { - "Failing Message", - CommitMessage{ - Emoji: '❌', - Tag: "[TCR - FAILED]", - Description: "tests failing", - }, - true, - failedCommitMessage, - }, { - "Reverting Message", - CommitMessage{ - Emoji: '⏪', - Tag: "[TCR - REVERTED]", - Description: "revert changes", - }, - true, - revertedCommitMessage, - }, { - "Passing Message without Emoji", - CommitMessage{ - Emoji: '✅', - Tag: "[TCR - PASSED]", - Description: "tests passing", - }, - false, - "[TCR - PASSED] tests passing", - }, - } - for _, test := range tests { - t.Run(test.desc, func(t *testing.T) { - assert.Equal(t, test.expected, test.commitMessage.toString(test.withEmoji)) - }) - } -}