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

test: add unit tests for engine exec #180

Merged
merged 11 commits into from
Aug 8, 2022
Merged

test: add unit tests for engine exec #180

merged 11 commits into from
Aug 8, 2022

Conversation

shay2025
Copy link
Contributor

Description

This pull request introduces the unit tests for engine exec and some extra mocks related to the pull request data.
During the development of the unit tests, it was encountered an error in the CollectError function where it wasn't being taken into account the *github.ErrorResponse error type and this would cause an nil pointer dereference because the Error() method would be applied to this object which doesn't support such thing and so we need to manually get the error string in this case.

Related issue

Closes #176

Type of change

Bug fix (non-breaking change which fixes an issue)

Improvements (non-breaking change without functionality)

How was this tested?

Ran task test cmd.

Checklist

  • I have performed a self-review of my code
  • I have ran task check -f and have no issues

@reviewpad-bot reviewpad-bot added large large pull request run-build Label that controls when build should be executed labels Jul 27, 2022
@reviewpad-bot reviewpad-bot added the ask Pull request requires a code review before merge label Aug 1, 2022
@shay2025 shay2025 marked this pull request as draft August 1, 2022 10:02
@shay2025 shay2025 marked this pull request as ready for review August 1, 2022 10:24
@shay2025 shay2025 force-pushed the test/engine-exec branch 2 times, most recently from d050eca to ef91b7c Compare August 1, 2022 10:29
Copy link
Member

@ferreiratiago ferreiratiago left a comment

Choose a reason for hiding this comment

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

I would suggest to refactor the test and have into consideration table testing. Here's a starting point:

func TestEval_Labels() {
	"when label has no name"
	"when label exists"
	"when label does not exist"
	"when label is invalid"
}

func TestEval_Groups() {
	"when group is valid"
	"when group is invalid"
}

func TestEval_Rules() {
	"when rule is valid"
	"when rule is invalid"
}

func TestEval_Workflows() {
	"when workflow is invalid" // e.g. rule is invalid
	"when no workflow is activated"
	"when one workflow is activated"
	"when multiple workflows are activated"
	"when workflow should always run"
	"when workflow is activated and has extra actions"
}

You can check hasUnaddressedReviewThreads_test.go to have an idea.

Also, I would suggest to have a different reviewpad.yml file for each test case in testdata/exec. For instance:

  • reviewpad_with_unmaned_label.yml
  • reviewpad_with_valid_label.yml (this can be used for "when label does not exist" and "when label exists")
  • ...
  • reviewpad_with_one_worfklow
  • reviewpad_with_multiple_worfklows

WDYT?

Copy link
Member

@ferreiratiago ferreiratiago left a comment

Choose a reason for hiding this comment

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

Please have a look at the comments.

engine/exec_test.go Outdated Show resolved Hide resolved
engine/exec_test.go Outdated Show resolved Hide resolved
engine/exec_test.go Outdated Show resolved Hide resolved
engine/exec_test.go Outdated Show resolved Hide resolved
engine/exec_test.go Outdated Show resolved Hide resolved
engine/mocks.go Outdated Show resolved Hide resolved
engine/mocks.go Outdated Show resolved Hide resolved
engine/mocks.go Outdated Show resolved Hide resolved
Comment on lines 14 to 35
func LoadReviewpadFile(filepath string) ([]byte, error) {
reviewpadFileData, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}

return reviewpadFileData, nil
}

func ParseReviewpadFile(data []byte) (*engine.ReviewpadFile, error) {
reviewpadFile := &engine.ReviewpadFile{}
err := yaml.Unmarshal(data, &reviewpadFile)
if err != nil {
return nil, err
}

// At the end of loading all imports from the file, its imports are reset to []engine.PadImport{}.
// However, the parsing of the wanted reviewpad file, sets the imports to []engine.PadImport(nil).
reviewpadFile.Imports = []engine.PadImport{}

return reviewpadFile, nil
}
Copy link
Member

Choose a reason for hiding this comment

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

Please review these functions.

engine/mocks.go Outdated Show resolved Hide resolved
@shay2025 shay2025 requested a review from ferreiratiago August 8, 2022 16:01
@shay2025 shay2025 merged commit 434ed34 into main Aug 8, 2022
@shay2025 shay2025 deleted the test/engine-exec branch August 8, 2022 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ask Pull request requires a code review before merge large large pull request run-build Label that controls when build should be executed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test Eval function in engine exec
3 participants