Skip to content
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

[WIP] Create a framework to exercise the go test output #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

knz
Copy link
Contributor

@knz knz commented Nov 30, 2019

Prior to this patch, there was no way to exercise the behavior of
-rewrite nor how errors and sub-tests would be reported in the
output of go test -v.

This patch introduces such "reflective" tests, where a test can
compare the output and modified file through running go test
recursively.


This change is Reviewable

@knz knz requested review from tbg and RaduBerinde November 30, 2019 15:03
@knz
Copy link
Contributor Author

knz commented Nov 30, 2019

What do you think? I was missing a way to properly test all the features of the datadriven package. I think this way we can do this.

If you think this is a good idea, I will add unit tests for the features already there - error handling, sub-tests, parsing errors, etc.

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not crazy about running go from the test.. Couldn't we change the infrastructure to take a testing.TB and provide an instrumented implementation for the purposes of testing?

@knz
Copy link
Contributor Author

knz commented Dec 1, 2019

Couldn't we change the infrastructure to take a testing.TB and provide an instrumented implementation for the purposes of testing?

That was my first idea actually, but I found it unsatisfying for two reasons.

The first reason is that it's quite a large amount of work. Not just the interface change, which is minor; the implementation of a mock testing.TB is quite the challenge, as there is a complex interplay between goroutines (one per test), parent-child goroutines (subtests) and the channels used to synchronize (t.Fatal/t.Skip use runtime.Goexit to terminate early). Just the mock would implement 30-40% of the testing package, which is quite a lot.

The second reason (and for me the main reason) is that I really want to test the output produced by failing datadriven tests when ran under go test:

  • the command line flag parsing
  • how the file/lineno reference look like in error messages
  • whether the sub-test names are properly assembled (I just found a bug in this area, PR upcoming)

It looks to me like the amount of refactoring / mock injection needed to test these aspects is unreasonably large compared to the (relative) simplicity of the approach I took here instead.

What do you think?

Prior to this patch, there was no way to exercise the behavior of
`-rewrite` nor how errors and sub-tests would be reported in the
output of `go test -v`.

This patch introduces such "reflective" tests, where a test can
compare the output and modified file through running `go test`
recursively.
@knz knz force-pushed the 20191130-datadriven-reflect-test branch from 4dcde51 to 7cec255 Compare December 1, 2019 09:50
@knz knz mentioned this pull request Dec 1, 2019
@RaduBerinde
Copy link
Member

I was thinking the implementation would still use a *testing.T underneath (the one from our test), you'd just intercept some of the calls we care about (Error,Fatal). I might be missing some subtleties though, I'll leave it to you to decide since you spent more time thinking about this. I don't have strong objections given that it's testing code and not even in the main repo. I'll look more closely at the final change.

@knz
Copy link
Contributor Author

knz commented Dec 2, 2019

you'd just intercept some of the calls we care about (Error,Fatal). I might be missing some subtleties though

I'd like to understand more: how do you mock something that properly casts down to *testing.T in some places, but uses a mock in others?

@RaduBerinde
Copy link
Member

type myT struct { *testing.T }

@knz
Copy link
Contributor Author

knz commented Dec 2, 2019

Oh TIL.

Ok I'll experiment with that.

@RaduBerinde
Copy link
Member

RaduBerinde commented Dec 2, 2019

It wouldn't cast down, but it would implement testing.TB. Hm though I guess that doesn't have everything we need (eg Run). We'd need to define our own interface and that will make the API more complicated. Your solution is probably better.

@knz
Copy link
Contributor Author

knz commented Dec 2, 2019

It doesn't have Run and also implementing Fatal and Skip is annoying (goroutines + defers + weird synchronization)

@tbg tbg removed their request for review June 25, 2020 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants