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

DRY up config in tests. #2162

Merged
merged 4 commits into from
Nov 16, 2020
Merged

Conversation

absoludity
Copy link
Contributor

@absoludity absoludity commented Nov 16, 2020

Description of the change

Just a follow-up of #2156 (comment) (sorry, wasn't very clear with what I meant there), removing the extra code which is repeated in many tests.

Also removes the defers which were used to reset test config, instead creating the test config (functionally) with no state shared between tests.

DBSecretKey: defaultConfig.DBSecretKey,
UserAgentComment: defaultConfig.UserAgentComment,
Crontab: defaultConfig.Crontab,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I'd meant was to "DRY"-up these repeated Config structs given we're only changing two fields.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooook, now I see your point; totally agree with you. Since we already have the default config, there's no need to repeat it in each test. Thanks!

@@ -979,12 +842,10 @@ func Test_newSyncJob(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.config.UserAgentComment != "" {
defaultConfig.UserAgentComment = tt.config.UserAgentComment
defer func() { defaultConfig.UserAgentComment = "" }()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't clear to me why you were modifying defaultConfig at all here, given that you pass tt.config into your function below. But either way, although it's an interesting use of defer to reset the shared state for your test functions, it's a much better idea (IMO) to not have any shared state that requires resetting if you can avoid it (eg. what I did here by having each test construct the Config for each test).

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't know either what was the reason for having this defer here. I just supposed it was the proper way to reset the state and I didn't want to change more things in the previous PR.
However, with this change, it is much clearer now :)

defaultConfig.Crontab = tt.config.Crontab
defer func() { defaultConfig.Crontab = "" }()
}
result := newCronJob(tt.apprepo, tt.config)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See comment below about the defer funcs here.

@absoludity absoludity merged commit 4d32f93 into vmware-tanzu:master Nov 16, 2020
@absoludity absoludity deleted the default-config-test branch November 16, 2020 23:53
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.

None yet

3 participants