-
Notifications
You must be signed in to change notification settings - Fork 11
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
Use DI for the CommandExecutor #81
Conversation
7b7f19d
to
61dbdd8
Compare
Good job, just a note, in the tests maybe we can use the constructor function to build the gatherers and pass the mock executor, instead of creating the struct directly and set the field, this way we test the exposed api and we can also give a hint to the other developers about how to use the gatherers properly What do you thing? |
@rtorrero nice! all good , green after addressing @CDimonaco comment |
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 as long as we address what the guys are saying.
Just a note: somewhere else I personally have used two constructors
func NewDefaultPasswordGatherer() *VerifyPasswordGatherer {
return NewVerifyPasswordGatherer(Executor{})
}
func NewVerifyPasswordGatherer(executor CommandExecutor) *VerifyPasswordGatherer {
return &VerifyPasswordGatherer{
executor,
}
}
Not sure if you guys like that (I hope so because the PR was approved and merged 😅 )
Anyway, I don't mind, whichever way.
cibAdminOutput []byte | ||
} | ||
|
||
func TestCibAdminTestSuite(t *testing.T) { | ||
suite.Run(t, new(CibAdminTestSuite)) | ||
} | ||
|
||
func (suite *CibAdminTestSuite) SetupSuite() { | ||
func (suite *CibAdminTestSuite) SetupTest() { | ||
suite.executor = new(mocks.CommandExecutor) | ||
lFile, _ := os.Open("../../../test/fixtures/gatherers/cibadmin.xml") |
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 believe that reading the cibadmin.xml could go in a SetupSuite()
because reading that file once would be enough, while we need to reset the executor on each test because of the .On(...)
prophecies (telling how the method should behave).
Yet, this is just fine as well.
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.
Indeed, nice catch, this should make the tests execute a bit faster
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 don't really get the benefit of the two constructors though :/)
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.
@rtorrero In this case the benefit is to have a much more ergonomic external api, because we don't care about having one instance of the executor among all the gatherers (for now, because really the executor is just a function that runs command), in much more advanced scenarios, where the dependency we want to inject needs to be atomic or at least controlled in all the difference instances, the defaul constructor should be omitted
For example a database client with connection parameters and connection pool, we need to inject the dependency explicitly in order to have a coordination and control among the dependency injected to the dependents, so a default constructor can be counter intuitive and lead to confusion
At the end @rtorrero is up to you, the important thing is that we distinguish this two scenarios when we decide now and in future how to inject the deps
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.
Ok, now I see. Indeed in this case, sharing the CommandExecutor instance for all the gatherers work, but I'd also go for what @nelsonkopliku proposes as I think it's just more generic.
27598e6
to
133ccc0
Compare
@nelsonkopliku For me is fine, the important thing is that we have the injection in place, if we want to give the developers an option to build a gatherer with default dependency, I'm totally with it and can be more ergonomic and simple to use |
133ccc0
to
f3202c9
Compare
Switch gatherers that use the CommandExecutor to do it using dependency injection