go icon indicating copy to clipboard operation
go copied to clipboard

cmd/go: it would be great if testScript.cmdExists could check for empty files

Open bep opened this issue 1 year ago • 3 comments

See https://github.com/golang/go/blob/0c4db1e347dc51589e5289388305b02108ca0aa1/src/cmd/go/script_test.go#L816

I'm using this via https://github.com/rogpeppe/go-internal/tree/master/testscript

And I do understand that one reason why you're having this in an internal package is that you don't want bug reports about it, but:

  • The testscript package is incredibly useful.
  • I had some tests passing that shouldn't – where the files were empty.
  • Maybe this could discover some issues in this repo, too (I tried to add this check and run the tests, but I ... failed to run the tests on my MacBook)
  • Maybe add a flag e.g. -zerosize for the test cases where the file should be empty.

/cc @rogpeppe

bep avatar Aug 06 '22 16:08 bep

Perhaps instead of exists filename you could write grep '.' filename.

ianlancetaylor avatar Aug 06 '22 19:08 ianlancetaylor

Perhaps instead of exists filename you could write grep '.' filename.

Yea, maybe, or I could write my own exists command (which currently do not allow me to use the negate keyword), but the thing is:

  • exists already have some extra check flags in there (-readonly, -exec).
  • an empty file would, in my head, in most situations be a red flag I would want a test to shout about.

bep avatar Aug 06 '22 19:08 bep

Would you like to send a patch for this?

thanm avatar Aug 08 '22 16:08 thanm

cc @bcmills @mvdan

seankhliao avatar Aug 09 '22 14:08 seankhliao

@thanm I may when I find time, but my first attempt running the tests inside cmd/go was a big failure, so I need to look into that first.

bep avatar Aug 09 '22 16:08 bep

I agree with @ianlancetaylor: you can use grep for this. ! grep . stdout is a common way to test that a command didn't print to stdout, for example. And the exists command isn't called notempty, after all.

mvdan avatar Aug 09 '22 16:08 mvdan

That would mean doing this for every file:

# Check that it exists an is executable
check -exec filename
# Check that it's not empty (which I assume means reading the entire file)
grep . filename
# ... next file

And the exists command isn't called notempty, after all.

Well, it's not called readonly, either.

This is not a big deal for me, but I would suggest a lower barrier for proposals for non-breaking changes in internal test APIs. If, by adding these 2 lines, you discover a fatal bug in File.Sync() on some platform, it would be well worth it.

bep avatar Aug 09 '22 16:08 bep

What I mean is that the default shouldn't change to check for non-empty files. The "readonly" check is opt-in via a flag, for example. Maybe -notempty?

mvdan avatar Aug 09 '22 17:08 mvdan

I've been taking some steps to properly extract the cmd/go script engine (https://go.dev/cl/419875) in order to reuse it for #27494. The approach I've been taking allows the registration of arbitrary commands, and also allows the internal commands to be extended by replacing their implementation.

With that approach, you could modify your own script's exists command to support whatever extra options you want. 🙂

At any rate, I don't think we should make this change in cmd/go itself until / unless we need to use it in a cmd/go test. (The script engine is intentionally internal to cmd/go for the time being. Perhaps at some point we can file a proposal to move it to x/tools or x/exp or someplace like that.)

bcmills avatar Aug 23 '22 15:08 bcmills