-
Notifications
You must be signed in to change notification settings - Fork 18.8k
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
Fix for help when $HOME is / #10625
Conversation
Is it really such a good idea for us to be substituting |
this should use the new homedir package? no? |
Do users not usually recognize their own homedir? I know any time I see |
@tianon see the linked comment. The issue is the width required to show the home directory. |
84ba61a
to
ba6b21e
Compare
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. |
"os/exec" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"unicode" | ||
|
||
"github.com/docker/docker/pkg/homedir" | ||
) | ||
|
||
func TestMainHelpWidth(t *testing.T) { |
There was a problem hiding this comment.
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=/
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
I see with master |
is your $HOME set to / ? |
/home/jessie |
|
Right, "normally" it should replace all |
oh i see what you are saying mine works fine because my home is not / LGTM |
0a7e34a
to
40830fc
Compare
|
||
// Don't check when HOME is / | ||
if home == "/" { | ||
home = "" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 /
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
7ae6e00
to
dfec474
Compare
break | ||
for _, home := range homes { | ||
os.Setenv("HOME", home) | ||
scanForHome := false |
There was a problem hiding this comment.
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 != "/"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
dfec474
to
2888493
Compare
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 "". |
any more comments on this one? |
ping @tiborvass |
@duglin when HOME=/, you don't test for replacing |
2888493
to
41ffc49
Compare
41ffc49
to
dc28db8
Compare
@tiborvass rebased - should be good for final review now |
dc28db8
to
fc3546f
Compare
@ahmetalpbalkan I added a homedir.Set() - hope that's ok |
LGTM |
@duglin. It's probably not ok. not all windows apps read it from USERPROFILE. There is also %HOMEDRIVE%%HOMEPATH%. Why do you need this? |
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? |
@ahmetalpbalkan doesn't matter, whatever he "gets" from homedir.Get, is what he's setting with homedir.Set. So nothing changes. |
fc3546f
to
7b8fe6d
Compare
@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. |
@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) { |
There was a problem hiding this comment.
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.
@tianon I don't see the upside. What's wrong with using Honestly a homedir package with Set method doesn't make sense to me semantically. |
7b8fe6d
to
cc9c874
Compare
@@ -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" |
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
LGTM except I left some minor comments. Thanks @duglin. |
cc9c874
to
3272211
Compare
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>
3272211
to
116367e
Compare
LGTM. |
Made some mods per @ahmetalpbalkan's suggestion. |
Fix for help when $HOME is /
@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