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

x/mobile/bind: TestGenGo failing due to changes in gofmt output #34619

Closed
bcmills opened this issue Sep 30, 2019 · 6 comments
Closed

x/mobile/bind: TestGenGo failing due to changes in gofmt output #34619

bcmills opened this issue Sep 30, 2019 · 6 comments
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsFix The path to resolution is known, but the work has not been done. Soon This needs to be done soon. (regressions, serious bugs, outages) Testing An issue that has been verified to require only test changes, not just a test failure.
Milestone

Comments

@bcmills
Copy link
Contributor

bcmills commented Sep 30, 2019

The fix for #28082 changed the line-wrapping behavior of gofmt in some cases.

One of those changes happens to affect x/mobile/bind.TestGenGo, which is asserting a particular golden gofmt output (rather than more robustly running both the actual and expected outputs through gofmt before comparing them).

CC @griesemer @eliben @odeke-em @steeve @hyangah

@gopherbot gopherbot added this to the Unreleased milestone Sep 30, 2019
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Sep 30, 2019
@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. Testing An issue that has been verified to require only test changes, not just a test failure. mobile Android, iOS, and x/mobile and removed mobile Android, iOS, and x/mobile labels Sep 30, 2019
@bcmills
Copy link
Contributor Author

bcmills commented Sep 30, 2019

@bcmills bcmills added the Soon This needs to be done soon. (regressions, serious bugs, outages) label Sep 30, 2019
@gopherbot
Copy link

Change https://golang.org/cl/198038 mentions this issue: bind: fix formatting in test, following changes in 'go fmt'

@eliben
Copy link
Member

eliben commented Sep 30, 2019

Rewriting the tests to do this in a more robust way is a bit tricky due to the way they are structured. In particular TestGenGoJavaWrappers arranges to emit multiple packages into the same file, and compares the emitted file to the equivalent .golden file.

For example, https://github.com/golang/mobile/blob/master/bind/testdata/java.go.golden has multiple packages in it. We can't just run this file through the formatter because format.Source chokes on it upon finding a package in an unexpected place.

The test generates some go code without gofmt-ing it first, which includes multiple packages (in genJavaPackages)

@eliben
Copy link
Member

eliben commented Oct 1, 2019

A couple of possible solutions:

  1. Hacky but relatively simple: when reading the golden file, find package main and only reformat that package, leaving the rest of the code unformatted, as today. package main comes at the end of the file, so it's easy.

  2. More robust, but entails a significant restructuring of bind_test: don't generate packages into the same file with the main Go code. Generate them (genJavaPackages) into a separate golden file, that gets compared separately. So instead of a single golden file per test, there will be two - and the two are compared separately. The one where the main Go code is generated will pass through format.Source for stability.

@hyangah
Copy link
Contributor

hyangah commented Oct 1, 2019 via email

@gopherbot
Copy link

Change https://golang.org/cl/198322 mentions this issue: bind: format generated go code before comparing with golden files

@golang golang locked and limited conversation to collaborators Oct 1, 2020
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 10, 2021
bind_test.go compares the generated Go files against golden files
checked in the repository. The bind package formats some of the
generated Go files, so any changes in the go formatter can break
the tests.

This change makes the test more robust by applying formatting based
on the currently used go version. Since a golden file often
includes multiple go files generated by the bind, the `gofmt`
function splits the golden file using the gobindPreamble marker
and then run format.Source for each chunk. In order to ease the
golden file splitting, this CL also moves the gobindPreamble
to the beginning of each generated file consistently.

It turned out bind omits formatting for some go files (generated
for reverse binding). That needs to be fixed but it is a much
bigger fix. Thus, in this CL, we apply the formatting on the
bind's output as well.

This CL also updates the gobindPreamble to follow the style guide
for generated code. https://golang.org/s/generatedcode

Fixes golang/go#34619

Change-Id: Ia2957693154face2848e051ebbb2373e95d79593
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/198322
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 11, 2021
bind_test.go compares the generated Go files against golden files
checked in the repository. The bind package formats some of the
generated Go files, so any changes in the go formatter can break
the tests.

This change makes the test more robust by applying formatting based
on the currently used go version. Since a golden file often
includes multiple go files generated by the bind, the `gofmt`
function splits the golden file using the gobindPreamble marker
and then run format.Source for each chunk. In order to ease the
golden file splitting, this CL also moves the gobindPreamble
to the beginning of each generated file consistently.

It turned out bind omits formatting for some go files (generated
for reverse binding). That needs to be fixed but it is a much
bigger fix. Thus, in this CL, we apply the formatting on the
bind's output as well.

This CL also updates the gobindPreamble to follow the style guide
for generated code. https://golang.org/s/generatedcode

Fixes golang/go#34619

Change-Id: Ia2957693154face2848e051ebbb2373e95d79593
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/198322
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsFix The path to resolution is known, but the work has not been done. Soon This needs to be done soon. (regressions, serious bugs, outages) Testing An issue that has been verified to require only test changes, not just a test failure.
Projects
None yet
Development

No branches or pull requests

4 participants