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

Use DI for the CommandExecutor #81

Merged
merged 2 commits into from
Aug 19, 2022
Merged

Use DI for the CommandExecutor #81

merged 2 commits into from
Aug 19, 2022

Conversation

rtorrero
Copy link
Contributor

Switch gatherers that use the CommandExecutor to do it using dependency injection

@rtorrero rtorrero force-pushed the command-executor-di branch from 7b7f19d to 61dbdd8 Compare August 18, 2022 09:13
@CDimonaco
Copy link
Member

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?

@fabriziosestito
Copy link
Member

@rtorrero nice! all good , green after addressing @CDimonaco comment

Copy link
Member

@nelsonkopliku nelsonkopliku left a 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")
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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 :/)

Copy link
Member

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

Copy link
Contributor Author

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.

@rtorrero rtorrero force-pushed the command-executor-di branch from 27598e6 to 133ccc0 Compare August 18, 2022 14:17
@rtorrero rtorrero requested a review from CDimonaco August 18, 2022 14:17
@CDimonaco
Copy link
Member

CDimonaco commented Aug 18, 2022

@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

@rtorrero rtorrero force-pushed the command-executor-di branch from 133ccc0 to f3202c9 Compare August 18, 2022 14:42
@rtorrero rtorrero merged commit 09406ed into main Aug 19, 2022
@stefanotorresi stefanotorresi deleted the command-executor-di branch October 3, 2022 11:07
@nelsonkopliku nelsonkopliku added the enhancement New feature or request label Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

4 participants