-
Notifications
You must be signed in to change notification settings - Fork 10
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
internalize execute command #28
Conversation
Signed-off-by: Kim Tsao <ktsao@redhat.com>
Signed-off-by: Kim Tsao <ktsao@redhat.com>
Signed-off-by: Kim Tsao <ktsao@redhat.com>
Signed-off-by: Kim Tsao <ktsao@redhat.com>
}) | ||
} | ||
|
||
execute = originalExecute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if someone forgets to do this in a future test? Will execute be the test execute in non-test code call hierarchy 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is unnecessary to reset after every test as long as the mock function stays the same. I just did it for the sake of consistency and best practice (in case the mock function does change from test case to test case). In this particular test case, it was calling a mix of the original and mock execute functions, so the results were getting messed up. For non-test code, it should be the original execute since test functions are not exported
pkg/gitops_test.go
Outdated
execute = func(baseDir string, cmd CommandType, args ...string) ([]byte, error) { | ||
var output []byte | ||
var execErr error | ||
output, execErr, executedCmds = mockExecute(outputStack, tt.errors, executedCmds, baseDir, cmd, args...) | ||
return output, execErr | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we're doing this in every test case..
I would just do this as a implementation of an interface once (can replace the Executor interface in gitops.go). So we can 2 implementation:
- original execute
- test execute
this will also solve the issue of not resetting execute at the end of every test 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good suggestion (I don't like duplicate code either) but the limiting factor here is, the parameters to the mockExecute call varies. I can't pass in test arguments like outputStack, tt.errors, executedCmds
to the interface function because that has to stay fixed in order for the override to work, so I have to inline the function declaration in each test in order to get the values of the test arguments that are set outside the scope of the execute function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could also put most of these in a util getter func? Does this work? But I leave it up to you
execute = newTestExecute(outputStack, tt.errors, executedCmds, baseDir, cmd, args...)
func newTestExecute(outputStack, tt.errors, executedCmds, baseDir, cmd, args...) func {
return func(baseDir string, cmd CommandType, args ...string) ([]byte, error) {
var output []byte
var execErr error
output, execErr, executedCmds = mockExecute(outputStack, tt.errors, executedCmds, baseDir, cmd, args...)
return output, execErr
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this does work. It's a lot cleaner so I will implement this. Thanks for the suggestions!
Signed-off-by: Kim Tsao <ktsao@redhat.com>
@@ -2131,3 +2187,29 @@ func getCommitIDFromDotGit(repoPath string) (string, error) { | |||
} | |||
return string(fileBytes), nil | |||
} | |||
|
|||
func mockExecute(outputStack *testutils.OutputStack, errorStack *testutils.ErrorStack, executedCmds *[]testutils.Execution, baseDir string, cmd CommandType, args ...string) ([]byte, error, *[]testutils.Execution) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So each test 'file' we have will have a similar mockExecute and newTestExecute ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So each test 'file' we have will have a similar mockExecute?
Only if you are testing a function that runs a git
or rm
execute command (and you want to avoid live calls). If the file is in the same package and the current mockExecute behaviour is sufficient, you can just call it, otherwise you will need to create your own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: Kim Tsao ktsao@redhat.com
What does this PR do?:
Which issue(s)/story(ies) does this PR fixes:
https://issues.redhat.com/browse/DEVHAS-166
PR acceptance criteria:
Unit/Functional tests
Documentation
Client Impact
How to test changes / Special notes to the reviewer: