Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(validator): enforcing commit message character limit to 50. #25

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

athem-m
Copy link

@athem-m athem-m commented Feb 26, 2025

Description

Implemented checkMessageLength function to enforce the character limit of the commit message to 50.

Changes Include

  • checkMessageLength function in validator.go
  • additional check in ValidateMessage function

This commit fixes #24

Copy link
Member

@Jarmos-san Jarmos-san left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a few tests for the function?

P.S: I know the software is not well tested (yet) but I would like to gradually implement them, so might as well start with this.

@Jarmos-san
Copy link
Member

Can you ensure the status checks are passing and then mark it "ready for review"?

@athem-m
Copy link
Author

athem-m commented Mar 26, 2025

It is passing the CI Test Suite, but failing the Code Linting check because only a maximum of 88 characters are allowed per statement.

@Jarmos-san
Copy link
Member

Yes, so refactor your code to comply with the linting rules? The rules has been enforced in a standardised manner in the CI for a specific reason and I do not see any reason to not follow them.

@athem-m athem-m marked this pull request as ready for review April 6, 2025 06:17
Copy link
Member

@Jarmos-san Jarmos-san left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase the branch and make the requested changes.

Comment on lines +125 to +126
* - l (*string): A pointer to the string representing the length
* of the commit message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter name l is misleading, how about something like msg since it is synonymous to "commit message" which this function should be checking for its length? Update the docs once you have made the necessary changes as well.

@@ -142,6 +163,11 @@ func ValidateMessage(message *parser.Message) (string, error) {
return "", fmt.Errorf("%s", err)
}

// Validate the commit message length
if err := checkMessageLength(&message.Description); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are supposed to check the entire "commit message" and not just the description of the message.

A commit message is structured as - <TYPE>[OPTIONAL SCOPE]: <DESCRIPTION> (see the summary of the Conventional Commits specifications). An example commit message is - "chore(parser): improve parsing performance of the message".

Comment on lines +121 to +159
// TestCheckMessageLength tests the checkMessageLength function.
func TestCheckMessageLength(t *testing.T) {
testCases := []struct {
name string
message string
expectedError string
}{
{
name: "Valid Length",
message: "feat: add a new feature",
expectedError: "",
},
{
name: "Invalid Length",
message: "Add a new feature that makes the application run " +
"faster and more efficiently",
expectedError: fmt.Sprintf(
"commit message exceeds 50 characters, current length: %d",
len("Add a new feature that makes the application run "+
"faster and more efficiently"),
),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := checkMessageLength(&tc.message)
if tc.expectedError == "" {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
} else {
if err == nil || err.Error() != tc.expectedError {
t.Errorf("expected error %q, got %q", tc.expectedError, err)
}
}
})
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excluding this particular, all the other tests are not unnecessary addition in context to the PR. Try to maintain good coding habits and keep the contents of the PR in accordance with the actual context. Everything else can be added little by little in future smaller PRs.

testCases := []struct {
name string
message string
expectedError string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectedError can be of the error interface type instead of the generic string interface type no?

name: "Invalid Length",
message: "Add a new feature that makes the application run " +
"faster and more efficiently",
expectedError: fmt.Sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the expectedError is of the error interface type then you can use the fmt.Errorf() function to define an error message as well.

Comment on lines +153 to +155
if err == nil || err.Error() != tc.expectedError {
t.Errorf("expected error %q, got %q", tc.expectedError, err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional check for the error message can be much simplified if the expectedError is of the error interface type, so refactor it after a thorough evaluation of the aforementioned comments.

@athem-m athem-m marked this pull request as draft April 8, 2025 15:18
@Jarmos-san Jarmos-san added this to the v1.0.0 milestone Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enforce character limit to 50 for a commit message
2 participants