Skip to content

Commit

Permalink
[#809] Parse commit message events on tags rather than full header
Browse files Browse the repository at this point in the history
To support VCS that don't manage emojies
- extract CommitMessage, functions, and tests to their own files
- spot commit message status on string containing tag rather than matching full header
- add test for this new tag matching
- refactoring and code cleanup
  • Loading branch information
philou committed Oct 11, 2024
1 parent 1ace24f commit 2af7bbf
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 90 deletions.
46 changes: 46 additions & 0 deletions src/engine/commit_message.go
Original file line number Diff line number Diff line change
@@ -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
}
80 changes: 80 additions & 0 deletions src/engine/commit_message_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
51 changes: 19 additions & 32 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package engine

import (
"errors"
"fmt"
"github.com/murex/tcr/checker"
"github.com/murex/tcr/events"
"github.com/murex/tcr/filesystem"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
98 changes: 41 additions & 57 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,17 +796,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" +
Expand Down Expand Up @@ -1044,55 +1080,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))
})
}
}
7 changes: 6 additions & 1 deletion src/vcs/fake/vcs_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ func (vf *VCSFake) fakeCommand(cmd Command) (err error) {
// NewVCSFake initializes a fake VCS implementation which does nothing
// apart from emulating errors on VCS operations
func NewVCSFake(settings Settings) *VCSFake {
return &VCSFake{settings: settings, lastCommitSubjects: make([]string, 0), lastCommands: make([]Command, 0), supportsEmojis: true}
return &VCSFake{
settings: settings,
lastCommitSubjects: make([]string, 0),
lastCommands: make([]Command, 0),
supportsEmojis: true,
}
}

// Name returns VCS name
Expand Down

0 comments on commit 2af7bbf

Please sign in to comment.