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

cmd/go: test doesn't compile examples unless an output is provided #16773

Closed
rakyll opened this issue Aug 17, 2016 · 5 comments
Closed

cmd/go: test doesn't compile examples unless an output is provided #16773

rakyll opened this issue Aug 17, 2016 · 5 comments
Milestone

Comments

@rakyll
Copy link
Contributor

rakyll commented Aug 17, 2016

go test doesn't compile and report compilation errors if the example doesn't have an output section. This used to be different in the past and we were able to use examples to provide usage snippets without worrying that they are broken.

The current requirement to have an output section is impractical in complex examples such as:

func ExampleClientCredentialsJSON() {
    key, err := ioutil.ReadFile("/path/to/downloaded-client-secret.json")
    if err != nil {
        log.Fatal(err)
    }

    cfg, err := google.ConfigFromJSON(key, datastore.ScopeDatastore)
    if err != nil {
        log.Fatal(err)
    }

    client, err := datastore.NewClient(
        ctx, "project-id", option.WithTokenSource(cfg.TokenSource(ctx, nil)))
    if err != nil {
        log.Fatal(err)
    }
    // output:
    // something to assert
}

where go test is trying to run the block and fails because it cannot find the downloaded-client-secret.json. Since we are only caring about the integrity of the examples and not willing to verify them, please give us an option to build examples without output assertion.

@okdave
Copy link
Contributor

okdave commented Aug 18, 2016

This looks like a regression that is probably worth fixing in a 1.7.x. The testing package still says that it will run and verify example code, and only that the example may include the expected output.

@cznic
Copy link
Contributor

cznic commented Aug 18, 2016

@okdave
Copy link
Contributor

okdave commented Aug 18, 2016

@rakyl commented that they're not being compiled at all though, which is a regression

@cznic
Copy link
Contributor

cznic commented Aug 18, 2016

I cannot reproduce it. I am on 1.7 stable and I have just checked making one of examples, without an output section, invalid (s//fmt.Println/fmt.Printlnn and deleting the // Output: part) and the result is:

jnml@r550:~/src/github.com/cznic/gc$ head -n 15 ast_test.go 
// CAUTION: Generated by yy - DO NOT EDIT.

// Copyright 2016 The GC Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gc

import (
    "fmt"
)

func ExampleArgument() {
    fmt.Printlnn(exampleAST(2, "package a ; 'b' ( 'c' )"))
}
jnml@r550:~/src/github.com/cznic/gc$ go test
# github.com/cznic/gc
./ast_test.go:14: undefined: fmt.Printlnn
FAIL    github.com/cznic/gc [build failed]
jnml@r550:~/src/github.com/cznic/gc$

@rakyll
Copy link
Contributor Author

rakyll commented Aug 22, 2016

I cannot produce this problem anymore. I was able to produce both against tip and go1.7. There was something sketchy about my env a week ago. I have little idea how it affected the example tests but am going to close this issue to avoid more noise.

@rakyll rakyll closed this as completed Aug 22, 2016
@golang golang locked and limited conversation to collaborators Aug 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants