rogpeppe / go-internal

Selected Go-internal packages factored out from the standard library
BSD 3-Clause "New" or "Revised" License
825 stars 67 forks source link

testscript: env vars from outside #197

Closed aprice2704 closed 1 year ago

aprice2704 commented 1 year ago

Hey folks! No doubt I am just being stupid, but is there a way to get env vars into a testscript from without? (from a calling sh, for example)? I have tried several variations of echo, cat etc. without luck :( I have taken to prepending an env expression caclulated in sh before running the testscript, but this seems ugly Many TIA Andy

mvdan commented 1 year ago

testscript only passes a few variables to each test by default, for the sake of keeping tests from relying too hard on the host environment. You can always pass along extra variables via the Setup field in https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript#Params and https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript#TestScript.Setenv.

For example, here's a project of mine: https://github.com/burrowers/garble/blob/d0a6faa4e66fc12c97be6b0d63440bd51c7686a9/main_test.go#L72

myitcv commented 1 year ago

With the one slight improvement that you can use https://pkg.go.dev/github.com/rogpeppe/go-internal@v1.9.0/testscript#Env.Setenv to set the env variable.

mvdan commented 1 year ago

Oops, I sent the link for TestScript.Setenv, not Env.Setenv :)

Don't pay too much attention to my code because I wrote it before Env.Setenv existed.

bitfield commented 1 year ago

These blog posts may help:

https://bitfieldconsulting.com/golang/conditions-concurrency

In this article I show how to set environment variables within scripts, and also in the Go test that runs the script.

On the other hand, if you're using the standalone 'testscript' tool, check out:

https://bitfieldconsulting.com/golang/testscript-tool

This shows how to supply environment variables on the command line to 'testscript'.

These are part of a series of five posts about test scripts: https://bitfieldconsulting.com/golang/test-scripts

Also have a look at 'The Power of Go: Tests' which covers this and many other topics in all the delightful detail anyone could ever want, plus a bit more, just because it's fun.

https://bitfieldconsulting.com/books/tests

aprice2704 commented 1 year ago

Wow, thanks for the all the speedy and helpful replies folks!!!! Much appreciated, I'm sure one of these will do the trick! :D

Andrew's Linkedin profile http://ca.linkedin.com/pub/andrew-price/2/45b/aa5 direct email: @.*** voice/SMS Canada: +1 (604) 869-1905

On Fri, Feb 3, 2023 at 7:25 AM John Arundel @.***> wrote:

These blog posts may help:

https://bitfieldconsulting.com/golang/conditions-concurrency

In this article I show how to set environment variables within scripts, and also in the Go test that runs the script.

On the other hand, if you're using the standalone 'testscript' tool, check out:

https://bitfieldconsulting.com/golang/testscript-tool

This shows how to supply environment variables on the command line to 'testscript'.

These are part of a series of five posts about test scripts: https://bitfieldconsulting.com/golang/test-scripts

Also have a look at 'The Power of Go: Tests' which covers this and many other topics in all the delightful detail anyone could ever want, plus a bit more, just because it's fun.

https://bitfieldconsulting.com/books/tests

— Reply to this email directly, view it on GitHub https://github.com/rogpeppe/go-internal/issues/197#issuecomment-1416024589, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABW7EPSUUVD3IRGF47LAFTWVUPPVANCNFSM6AAAAAAUPXFXAM . You are receiving this because you authored the thread.Message ID: @.***>

myitcv commented 1 year ago

I'll close this on the basis there isn't a bug/feature request here.