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

Define in-test function at the head of test #4932

Merged
merged 1 commit into from
Jul 2, 2024
Merged

Conversation

Warashi
Copy link
Contributor

@Warashi Warashi commented May 31, 2024

What this PR does / why we need it:

This PR moves the definition of the in-test function to the top of the test.
This makes the in-test function unable to capture variables other than *testing.T.

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?: No

  • How are users affected by this change:
  • Is this breaking change:
  • How to migrate (if breaking change):

Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com>
Copy link

codecov bot commented May 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 29.39%. Comparing base (84b864d) to head (6b81a30).
Report is 48 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4932      +/-   ##
==========================================
+ Coverage   29.35%   29.39%   +0.03%     
==========================================
  Files         322      322              
  Lines       40852    40852              
==========================================
+ Hits        11994    12010      +16     
+ Misses      27897    27882      -15     
+ Partials      961      960       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +191 to +204
getGCAutoDetach := func(ctx context.Context, repo *repo) (bool, error) {
cmd := exec.CommandContext(ctx, repo.gitPath, "config", "--get", "gc.autoDetach")
cmd.Dir = repo.dir
out, err := cmd.CombinedOutput()
if err != nil {
return false, err
}
v, err := strconv.ParseBool(strings.TrimSuffix(string(out), "\n"))
if err != nil {
return false, err
}

return v, nil
}
Copy link
Member

Choose a reason for hiding this comment

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

TBH, I prefer to push the variable/function declaration right before placing it to use, which increases the readability. The closure function outer variable shadowing or reference is a normal thing we have to face if we use it, so I think it's better to capture it in the review process rather than placing it at the head of the function. wdyt?
Happy to hear other opinions on this cc @ffjlabo @t-kikuc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO, there is a trade-off between the readability of the closure and the codes that use the closure.
When I read the closure itself, closures placed at the top has more readability because there is no captured variable.
On the other hand, when I read codes that use the closure, places at just before it is used has more readability.

In this case, it's not so far between the top of the test function and the line it uses because test is 55 lines of code, not so long.
I think it is short enough to switch eye between definition and call.

Copy link
Member

Choose a reason for hiding this comment

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

Okey, this is just a small change, let's choose the win-win option 🙌

@Warashi Warashi changed the title test(git): define in-test function at the head of test Define in-test function at the head of test May 31, 2024
Copy link
Contributor

github-actions bot commented Jul 1, 2024

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added Stale and removed Stale labels Jul 1, 2024
Copy link
Member

@khanhtc1202 khanhtc1202 left a comment

Choose a reason for hiding this comment

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

Here you go 🪨

@khanhtc1202 khanhtc1202 enabled auto-merge (squash) July 2, 2024 02:54
Copy link
Member

@t-kikuc t-kikuc left a comment

Choose a reason for hiding this comment

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

🚀

@khanhtc1202 khanhtc1202 merged commit 6ec2541 into master Jul 2, 2024
16 checks passed
@khanhtc1202 khanhtc1202 deleted the refactor-git-test branch July 2, 2024 03:42
@github-actions github-actions bot mentioned this pull request Jul 4, 2024
khanhtc1202 pushed a commit that referenced this pull request Jul 12, 2024
Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com>
Signed-off-by: khanhtc1202 <khanhtc1202@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants