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

Remove use of run.SetPrepareCmd in tests #2858

Merged
merged 12 commits into from Jan 28, 2021

Conversation

dpromanko
Copy link
Contributor

Follow up to #2801 to remove the use of run.SetPrepareCmd in tests in favor of run.Stub.

I see that run.SetPrepareCmd is called from run.Stub so I wasn't sure if the intention was to move that to be used privately by run.Stub after it was no longer being called by tests. With some direction I can do some additional work there, or if a maintainer wants to pick that piece up that is fine as well.

The other thing with this PR I was unsure of is the few places that were testing failure paths and expecting no commands to be executed, for example TestPRView_web_noResultsForBranch in pkg/cmd/pr/view/view_test.go. Is there an intuitive way to test for the absence of commands with run.Stub?

@dpromanko
Copy link
Contributor Author

Oh and one other thing I was unsure of the purpose of it was this line when checking browser calls url := strings.ReplaceAll(args[len(args)-1], "^", ""). I copied this usage from here. I'm not sure what case that replace is meant to handle.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

This is great. Thanks for addressing this!

Unfortunately, I've spotted a number of translation errors. Do you have time to take another pass over the stubs indicated in my comments?

For asserting that no commands have been ran, simply initialize the stubber as normal and make sure its teardown(t) function gets invoked. That will fail the test if no commands were registered with the stubber, but if gh tried to shell out to something.

defer cmdTeardown(t)

cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "")
cs.Register(`git show-ref --verify -- refs/heads/feature`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This stub used to error out with exit code 1 previously.


cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "")
cs.Register(`git show-ref --verify -- refs/heads/feature`, 0, "")
cs.Register(`git checkout feature`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This stub used to be git checkout -b feature --no-track origin/feature

Comment on lines -135 to -136
assert.Equal(t, "git config branch.feature.remote origin", strings.Join(ranCommands[2], " "))
assert.Equal(t, "git config branch.feature.merge refs/heads/feature", strings.Join(ranCommands[3], " "))
Copy link
Contributor

Choose a reason for hiding this comment

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

These used to be stubbed but are no longer present after the rewrite. Can you confirm that this is no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No these are needed. This was a mistake on my part of changing test behavior by missing the non-zero exit code. This has been corrected.

defer cmdTeardown(t)

cs.Register(`git fetch origin \+refs/heads/feature:refs/remotes/origin/feature`, 0, "")
cs.Register(`git show-ref --verify -- refs/heads/feature`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous stub used to fail with exit status 1

defer cmdTeardown(t)

cs.Register(`git fetch robot-fork \+refs/heads/feature:refs/remotes/robot-fork/feature`, 0, "")
cs.Register(`git show-ref --verify -- refs/heads/feature`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This stub used to fail with exit status 1

defer cmdTeardown(t)

cs.Register(`git fetch origin refs/pull/123/head:feature`, 0, "")
cs.Register(`git config branch\.feature\.merge`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This stub used to print the following contents as output: refs/heads/feature\n

defer cmdTeardown(t)

cs.Register(`git fetch origin refs/pull/123/head`, 0, "")
cs.Register(`git config branch\.feature\.merge`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This stub used to print the following contents as output: refs/heads/feature\n

defer cmdTeardown(t)

cs.Register(`git fetch origin refs/pull/123/head:feature`, 0, "")
cs.Register(`git config branch\.feature\.merge`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This used to exit with error status 1

cs, cmdTeardown := run.Stub()
defer cmdTeardown(t)

cs.Register(``, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

An empty pattern will match anything. Please provide something about the command as pattern. Maybe we should have Register() panic if an empty pattern was passed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been corrected. I did not mean to leave that empty pattern in there. It seems reasonable to have Register() panic if an empty pattern is passed to enforce test quality. Did you want me to make that change in this PR as well? Or would you like a separate PR for that?

defer cmdTeardown(t)

cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
cs.Register(`git rev-parse --show-toplevel`, 0, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this command exit with nonzero status, since we're emulating not being in a git repo?

@mislav
Copy link
Contributor

mislav commented Jan 27, 2021

Oh and one other thing I was unsure of the purpose of it was this line when checking browser calls url := strings.ReplaceAll(args[len(args)-1], "^", "").

You did well. It's a messy workaround, but it compensates for URLs on Windows being escaped differently. Basically, any URL that contains & in the query string needs to have the ReplaceAll line, otherwise the test will fail on Windows. You are free to clean it up from assertions where the URL will never contain a &, but you can also leave it in. We will probably develop a more idiomatic approach to asserting that the browser has been invoked.

@vilmibm vilmibm added this to Backlog 🗒 in The GitHub CLI via automation Jan 27, 2021
@vilmibm vilmibm moved this from Backlog 🗒 to Needs review 🤔 in The GitHub CLI Jan 27, 2021
@dpromanko
Copy link
Contributor Author

@mislav thank you for the thorough review! I'll take another pass through this and should hopefully have it cleaned up by the end of the week.

@dpromanko dpromanko force-pushed the dpromanko/remove-set-cmd-prepare branch from 6664234 to 2964895 Compare January 27, 2021 23:46
@dpromanko
Copy link
Contributor Author

@mislav I think I have it all cleaned up now. Sorry for the sloppy mistakes changing test behavior, I got a little eager with some copy/paste in those scenarios where it was actually stubbing non-zero exit codes and responses.

The only two tests that still seem slightly off are TestPRCheckout_urlArg_differentBase and TestPRCheckout_branchArg. Both of these tests previously had stubbed non-zero exit code for git show-ref --verify -- refs/heads/feature, however I do not think this was needed. I pulled that case out of the old test implementation locally and the tests still pass, so I think that may have been copy/pasted before but not actually needed.

@dpromanko dpromanko requested a review from mislav January 28, 2021 00:00
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

It looks great. Thank you for the hard work!!

The GitHub CLI automation moved this from Needs review 🤔 to Needs to be merged 🎉 Jan 28, 2021
@mislav mislav merged commit d771bef into cli:trunk Jan 28, 2021
The GitHub CLI automation moved this from Needs to be merged 🎉 to Pending Release 🥚 Jan 28, 2021
@dpromanko dpromanko deleted the dpromanko/remove-set-cmd-prepare branch January 28, 2021 22:03
@github-actions github-actions bot moved this from Pending Release 🥚 to Done 💤 in The GitHub CLI Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
The GitHub CLI
  
Done 💤
Development

Successfully merging this pull request may close these issues.

None yet

2 participants