简体   繁体   English

Go test 编译成功,但 go build 没有

[英]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 ).我正在尝试在 go(在 Windows 中)中测试一个主包,并且测试似乎被缓存了,尽管我完全删除了缓存( go clean -cache )。

To test this I changed a file ( parseinput.go ), so that it produces an error during compilation (undefined variable).为了测试这一点,我更改了一个文件( parseinput.go ),以便在编译期间产生错误(未定义的变量)。 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 test甚至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).添加一些打印语句后,似乎 test ( go test ) 编译成功inputparser.go (尽管err变量未定义),但构建失败(如上所示)。 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:构建标志由 build、clean、get、install、list、run 和 test 命令共享:

 -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. -cache标志导致 clean 删除整个 go build 缓存。

The -testcache flag causes clean to expire all test results in the go build cache. -testcache标志导致 clean 使 go build 缓存中的所有测试结果过期。

Testing flags测试标志

When ' go test ' runs in package list mode, ' go test ' caches successful package test results to avoid unnecessary repeated running of tests.当' go test '以包列表模式运行时,' go test '缓存成功的包测试结果,以避免不必要的重复运行测试。 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 .显式禁用测试缓存的惯用方法是使用-count=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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