Example tests: access to module testdata files

130 views
Skip to first unread message

Rory Campbell-Lange

unread,
Jan 10, 2025, 3:52:20 AM1/10/25
to golang-nuts
Hi. I have written a small package with an example which runs fine locally, but doesn't work through the web interface of either my local godoc or pkg.go.dev.

https://pkg.go.dev/github.com/rorycl/mailbox...@v0.0.4#example-package

The test attempts to access files in the testdata directories of submodules in the package. I imagine this is not permitted for security reasons.

I'd be grateful for a pointer to the restrictions on web-runnable example tests, and to learn if there is way of providing files to my example programme.

Many thanks,
Rory

Charles Pustejovsky

unread,
Jan 20, 2025, 2:05:27 PM1/20/25
to golang-nuts
It looks like you're code is failing because you're trying to open from

Your operator is opening new Mboxs and the paths you pass in are assuming the main.go is called from that directory.

https://github.com/rorycl/mailboxoperator/blob/main/mbox/mbox.go#L28

I'm not sure how you would resolve this. Since there hasn't been an answer on here, I'd recommend posting the question to the testing channel of the Gopher Slack

Sean Liao

unread,
Jan 20, 2025, 2:21:38 PM1/20/25
to Rory Campbell-Lange, golang-nuts
The playground, which also serves pkg.go.dev examples. runs in an
isolated environment with just the built test/example binary, without
the source code.
Consider embedding any files you need with "embed" and accessing them
through io/fs.FS rather than through os calls.

- sean
> --
> 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 visit https://groups.google.com/d/msgid/golang-nuts/Z4Dfdk3g6ssaeuDk%40campbell-lange.net.

Rory Campbell-Lange

unread,
Jan 21, 2025, 3:59:57 AM1/21/25
to Sean Liao, golang-nuts
That's a really helpful comment. I'll give the embed/fs.FS approach a go -- thank you.
Reply all
Reply to author
Forward
0 new messages