-
Notifications
You must be signed in to change notification settings - Fork 77
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
testscript: add env convenience functions #96
Conversation
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 a nice idea, thanks, but I think the API could be a little tighter here.
testscript/testscript.go
Outdated
// If the variable is present in the environment the value (which may be empty) | ||
// is returned and the boolean is true. Otherwise the returned value will be | ||
// empty and the boolean will be false. | ||
func (e *Env) LookupEnv(key string) (value string, found bool) { |
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.
I'm wondering about the use case for these methods. In general we're not working with an arbitrary set of environment variables here. I don't really see the use case for LookupEnv
- it didn't even exist in the standard library for ages. How about just providing Getenv
and Setenv
?
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.
testscript/testscript.go
Outdated
// empty and the boolean will be false. | ||
func (e *Env) LookupEnv(key string) (value string, found bool) { | ||
key = envvarname(key) | ||
for _, v := range e.Vars { |
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.
how about iterating in reverse just to make things a bit clearer?
for i := len(e.Vars)-1; i >= 0; i-- {
if pair := strings.SplitN(v, "=", 2); len(pair) == 2 && envvarname(pair[0]) == key {
return pair[1]
}
}
return ""
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.
// Setenv sets the value of the environment variable named by the key. It | ||
// returns an error, if any. | ||
func (e *Env) Setenv(key, value string) error { | ||
if key == "" || strings.IndexByte(key, '=') != -1 { |
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 isn't the os
package. I'd suggest just panicking in this case, which avoids the need to decide what kind of error to return here (I'm not that keen on the syscall
import).
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.
Thanks for the review. All comments addressed and PR updated. |
2141521
to
1137e5e
Compare
Manipulating Env.Vars is not completely trivial: you need to handle last-entry-wins, invalid entries, and key normalization. This commit adds Getenv and Setenv method to Env that function identically to their counterparts in the standard os package.
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.
LGTM, thanks!
Manipulating
Env.Vars
is not completely trivial: you need to handle last-entry-wins, invalid entries, and key normalization.This PR adds
Getenv
,LookupEnv
, andSetenv
method toEnv
that function identically to their counterparts in the standardos
package.Example use: