8000 Fix for help when $HOME is / by duglin · Pull Request #10625 · moby/moby · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix for help when $HOME is / #10625

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

Merged
merged 1 commit into from
Feb 20, 2015
Merged

Fix for help when $HOME is / #10625

merged 1 commit into from
Feb 20, 2015

Conversation

duglin
Copy link
Contributor
@duglin duglin commented Feb 6, 2015

@estesp noticed that when $HOME is / the ~ substitutions messes up
because it tries to replace all paths that start with "/" with "~".
This fixes it so that it will only replace it when $HOME isn't "/".

This is a follow-on/fix to: #10547

Signed-off-by: Doug Davis dug@us.ibm.com

@tianon
< 8000 div class="timeline-comment-actions flex-shrink-0 d-flex flex-items-center">
Copy link
Member
tianon commented Feb 6, 2015

Is it really such a good idea for us to be substituting ~ in these help paths? ~ is not expanded by Go, but by the shell, so it might not always work depending on how a user is plugging it in after copy/pasting it from the help text to change "one little thing" about it, for example.

@jessfraz
Copy link
Contributor
jessfraz commented Feb 6, 2015

this should use the new homedir package? no?

@phemmer
Copy link
Contributor
phemmer commented Feb 6, 2015

@tianon I had that same thought when I made the comment here, but didn't mention at the time.
I think the ideal solution is if these fields weren't quoted. Then the copy/paste issue would be solved. But I didn't think the effort to handle removing quotes on these few fields was worth it.

@tianon
Copy link
Member
tianon commented Feb 6, 2015

Do users not usually recognize their own homedir? I know any time I see /home/tianon, I recognize it for what it is... 😄

@phemmer
Copy link
Contributor
phemmer commented Feb 6, 2015

@tianon see the linked comment. The issue is the width required to show the home directory.

@duglin
Copy link
Contributor Author
duglin commented Feb 6, 2015

If people think this was being "too smart for our own good" I'm ok with removing it. But as @phemmer said, this was to help make sure we fit within 80 chars.

@jfrazelle I switched it to use the new pkg just in case we don't rip it out.

@tiborvass tiborvass added this to the 1.5.1 milestone Feb 7, 2015
"os/exec"
"runtime"
"strings"
"testing"
"unicode"

"github.com/docker/docker/pkg/homedir"
)

func TestMainHelpWidth(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@duglin test passes on master is that normal? Even with HOME=/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm, it should fail on master because the check looks for "/" and should Fatal if it sees it.
When you do docker help what do you see?
I get:
-p, --pidfile="~var/run/docker.pid" Path to use for daemon PID file

Copy link
Contributor

Choose a reason for hiding this comment

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

@duglin yeah i get the same thing on master (and this PR fixes it), however this test still passes on master.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tiborvass I'm a little bit confused on this one. If, when you run the tests, $HOME is / then https://github.com/docker/docker/blob/master/integration-cli/docker_cli_help_test.go#L84 should cause your test to fail because at some point it should find a / in your help text. The fact that it doesn't must mean that $HOME isn't really set to /.

However, now I realize that the Contains() in there is looking for too much. Instead of just looking for $HOME it should be looking for "$HOME (ie. $HOME after a quote). That should remove some of the potential false-negative results. However, the check for $HOME being / is still needed because we don't want /some/path to become ~some/path.

@jessfraz
Copy link
Contributor
jessfraz commented Feb 7, 2015

I see -p, --pidfile="/var/run/docker.pid" Path to use for daemon PID file

with master

@duglin
Copy link
Contributor Author
duglin commented Feb 7, 2015

is your $HOME set to / ?

@jessfraz
Copy link
Contributor
jessfraz commented Feb 7, 2015

/home/jessie

@jessfraz
Copy link
Contributor
jessfraz commented Feb 7, 2015
jessie at debian in ~/docker on master
$ echo $HOME
/home/jessie

jessie at debian in ~/docker on master
$ sudo su
root@debian:/home/jessie/docker# echo $HOME
/root
root@debian:/home/jessie/docker# 

@duglin
Copy link
Contributor Author
duglin commented Feb 7, 2015

Right, "normally" it should replace all /home/jessie's or /root's (depending on which env your in) with ~ in your docker help output. Hopefully, your master is doing that. But, when you set $HOME to / then it should get too aggressive and you'll see ~ in other weird spots like ~var/...

@jessfraz
Copy link
Contributor
jessfraz commented Feb 7, 2015

oh i see what you are saying mine works fine because my home is not / LGTM

@duglin duglin force-pushed the FixHelpHomeSlash branch 3 times, most recently from 0a7e34a to 40830fc Compare February 8, 2015 13:54

// Don't check when HOME is /
if home == "/" {
home = ""
Copy link
Contributor

Choose a reason for hiding this comment

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

@duglin when $HOME is / you reset it to empty string here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, when home is "" I skip the check. So, $HOME="/" or being on windows will skip it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@duglin What I'm saying is that, that seems why the test passes on master as well, since it never satisfies the home != "" condition, even on master.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps I misunderstood, but I thought you originally were saying that this test passed for you w/o this PR (on master), which didn't check for / so the if-statement should have generated a failure when $HOME is /.

Copy link
Contributor

Choose a reason for hiding this comment

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

@duglin okay, now I get it, my bad. I was testing your test on top of master, to see if the coverage increased. But the test passes on master because it was a false positive. So if I only checkout the pkg/mflag change from this PR and run the old test from master, I get an error, but only if I set HOME=/.

Maybe we should temporarily set HOME to / in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. Instead of just testing the current env's $HOME I should test / too. I merged the two tests and looped it now so that on non-windows we'll test the current $HOME and /. See what you think.

@duglin duglin force-pushed the FixHelpHomeSlash branch 2 times, most recently from 7ae6e00 to dfec474 Compare February 8, 2015 19:22
break
for _, home := range homes {
os.Setenv("HOME", home)
scanForHome := false
Copy link
Contributor

Choose a reason for hiding this comment

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

scanForHome := runtime.GOOS != "windows" && home != "/"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@estesp
Copy link
Contributor
estesp commented Feb 9, 2015

Since I forgot to comment over the weekend, just wanted to note that the merging of the tests fixes the remaining problem when $HOME == "/" for me, given you had made the change in main help, but not in command help. Now that they are one test, it resolves the bug that command help wasn't conforming "/" to "".

@duglin
Copy link
Contributor Author
duglin commented Feb 10, 2015

any more comments on this one?

@jessfraz
Copy link
Contributor

ping @tiborvass

@tiborvass
Copy link
Contributor

@duglin when HOME=/, you don't test for replacing / by ~ (in --pidfile), which is basically what this PR fixes, no?

@duglin
Copy link
Contributor Author
duglin commented Feb 20, 2015

@tiborvass rebased - should be good for final review now

@duglin
Copy link
Contributor Author
duglin commented Feb 20, 2015

@ahmetalpbalkan I added a homedir.Set() - hope that's ok

@tiborvass
Copy link
Contributor

LGTM

@ahmetb
Copy link
Contributor
ahmetb commented Feb 20, 2015

@duglin. It's probably not ok. not all windows apps read it from USERPROFILE. There is also %HOMEDRIVE%%HOMEPATH%. Why do you need this?

@duglin
Copy link
Contributor Author
duglin commented Feb 20, 2015

In the testcase we set HOME to some value, I need to reset it when done. If on windows we're not setting HOME to a new value but instead setting USERPROFILE then I need to reset that value. no?

@tiborvass
Copy link
Contributor

@ahmetalpbalkan doesn't matter, whatever he "gets" from homedir.Get, is what he's setting with homedir.Set. So nothing changes.

@ahmetb
Copy link
Contributor
ahmetb commented Feb 20, 2015

@tiborvass @duglin that's really hacky. Can you instead write a homedir.GetEnvironmentVariableName() and make homedir.Get() use it? This way you can set the specific cmd.Env key before caling the command and you don't pollute os.Getenv for the next tests.

@tiborvass
Copy link
Contributor

@ahmetalpbalkan I don't understand. What's wrong with doing Set(key, Get(key)) ?

@@ -15,6 +15,14 @@ func Get() string {
return os.Getenv("HOME")
}

func Set(val string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is duplication. exact same if block exist for Get(), consider extracting into EnvironmentKey() method.

@ahmetb
Copy link
Contributor
ahmetb commented Feb 20, 2015

@tianon I don't see the upside. What's wrong with using cmd.Env?

Honestly a homedir package with Set method doesn't make sense to me semantically.

@@ -5,14 +5,20 @@ import (
"runtime"
)

// Return the Env Var Name for the user's home dir. On Linux this is
// "HOME" while on windows its "USERPROFILE"
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be starting with function's name per idiomatic Go.

HomeKey returns the environment name for user's home directory
depending on platform running on.

is probably just fine. By adding more stuff you're risking comments to get out of sync with code. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I'd call this something else. instead of homedir.HomeKey(), homedir.EnvironmentKey() seems like a better fit to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ahmetalpbalkan done for both but used Key() instead per our irc chat.

@ahmetb
Copy link
Contributor
ahmetb commented Feb 20, 2015

LGTM except I left some minor comments. Thanks @duglin.

estesp noticed that when $HOME is / the ~ substitutions messes up
becuase it tries to replace all paths that start with "/" with "~".
This fixes it so that it will only replace it when $HOME isn't "/".

Signed-off-by: Doug Davis <dug@us.ibm.com>
@ahmetb
Copy link
Contributor
ahmetb commented Feb 20, 2015

LGTM. :shipit:

@duglin
Copy link
Contributor Author
duglin commented Feb 20, 2015

Made some mods per @ahmetalpbalkan's suggestion.

tiborvass added a commit that referenced this pull request Feb 20, 2015
@tiborvass tiborvass merged commit fdfa7ee into moby:master Feb 20, 2015
@duglin duglin deleted the FixHelpHomeSlash branch March 6, 2015 04:54
@jessfraz jessfraz modified the milestones: 1.5.1, 1.6.0 Mar 16, 2015
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.

8 participants
0