简体   繁体   中英

Go test compiles successfully, but go build doesn't

I am trying to test a main package in go (in Windows) and the test seems to be cached, although I delete completely the cache ( go clean -cache ).

To test this I changed a file ( parseinput.go ), so that it produces an error during compilation (undefined variable). The result is, that main package cannot be built:

go\src\xxx\apimonitor> go build
# xxx/apimonitor
.\inputparser.go:15:2: undefined: err
.\inputparser.go:16:19: undefined: err

,but tests still complete successfully ( go test or even go test -a ):

go\src\xxx\apimonitor> go test
PASS
ok      xxx/apimonitor  0.786s

Any clues as to why this keeps happening and why test does not recompile? Any other place where this package might be cached from previous builds?


Update

After adding some print statements, it seems that test ( go test ) compiles successfully inputparser.go (despite that err variable is undefined), but build fails (as depicted above). That's what led me to believe that test was cached. Here is a sample of the source that fails in build:

func parseStoreInput(strArray []string) (inputStoreTransactionHash, error) {
    var parsedIn inputStoreTransactionHash
    if !validateInput(strArray, 1, true) {
        return parsedIn, errors.New("Expecting an escaped JSON string as input")
    }
    err = json.Unmarshal([]byte(strArray[0]), &parsedIn)
    return parsedIn, err
}

Any clues/documentation as to why this might be happening?

Command go

Compile packages and dependencies

The build flags are shared by the build, clean, get, install, list, run, and test commands:

 -a force rebuilding of packages that are already up-to-date. -n print the commands but do not run them. -v print the names of packages as they are compiled. -x print the commands.

Remove object files and cached files

Usage:

 go clean [clean flags] [build flags] [packages]

The -cache flag causes clean to remove the entire go build cache.

The -testcache flag causes clean to expire all test results in the go build cache.

Testing flags

When ' go test ' runs in package list mode, ' go test ' caches successful package test results to avoid unnecessary repeated running of tests. To disable test caching, use any test flag or argument other than the cacheable flags. The idiomatic way to disable test caching explicitly is to use -count=1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM