On Wed, Feb 23, 2022 at 1:47 PM Gergely Brautigam <skarlso...@gmail.com>
wrote:

> Hi!
>
> The best way to do it is, to have these files in a folder next to the test
> called `testdata` and then when the test starts, simply copy them into a
> temp location. And then make your program work with an environment property
> or setting which can configure the path in which is loads these files from
> and set that environment property for the test to the Temp folder you just
> copied your files to.
>
> And don't forget to defer os.RemoveAll(tmp) :)
>
You don't need this if you use t.TempDir
<https://pkg.go.dev/testing#T.TempDir> to create the tempdir. :)

>
> On Wednesday, 23 February 2022 at 16:30:06 UTC+1 david....@gmail.com
> wrote:
>
>> On Wed, Feb 23, 2022 at 8:53 AM Leam Hall <leam...@gmail.com> wrote:
>>
>>> My program uses data and template files located relative to the binary.
>>> Since go test creates the binary under test in /tmp/go-build....., there
>>> are no data or template files. How do I either specify what directory to
>>> build in so I can create the files, or get the tmp directory name before
>>> the tests are run so I can write files to it?
>>>
>>
>> When go test runs tests, they run with the current working directory
>> inside the package's directory, so relative paths should work.
>>
>> Another option that you might want to consider is using //go:embed
>> directives to embed those files in your binary. (package doc:
>> https://pkg.go.dev/embed)
>> This way you never need to resolve anything relative to the binary again.
>>
>> Note: unless you use mlock(2) embedding in the binary shouldn't bloat
>> memory usage, since the data will only be paged into memory at first-access.
>>
>>>
>>> Thanks!
>>>
>>> Leam
>>>
>>> --
>>> Site Automation Engineer   (reuel.net/resume)
>>> Scribe: The Domici War     (domiciwar.net)
>>> General Ne'er-do-well      (github.com/LeamHall)
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golang-nuts...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/7d8cf3dc-14ab-315f-ce5a-9e3cca877e17%40gmail.com
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/8ffeae9e-ce1d-4a7d-af01-b24a1d13c7cdn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/8ffeae9e-ce1d-4a7d-af01-b24a1d13c7cdn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CANrC0BjvgCD%3D7GrhSsV1xHuZjGj6wHso_-qpCWBRbEj8MW-%3DgA%40mail.gmail.com.

Reply via email to