How to run 'go test' in the parent directory of multiple go modules and return non-zero on error

王林
Release: 2024-02-05 23:48:03
forward
769 people have browsed it

如何在多个go模块的父目录中运行“go test”并在出现错误时返回非零

Question content

Look at this directory structure:

/root
    /hoge
        go.mod
        go.sum
        main.go
        /test
           main_test.go
           /unit
              sub_test.go
    /fuga
        go.mod
        go.sum
        main.go
        /test
           main_test.go
Copy after login

You can run the test using the following code, but it will always return exit code 0 if it fails.

find . -name go.mod -execdir go test ./... \;
Copy after login

Is there a way to return a non-zero value if it fails?


Correct Answer


Use this command:

find . -name go.mod -execdir go test ./... -- {} +
Copy after login

findCommand Manual About -execdir command{}

If any call using the " " form returns a non-zero value as the exit status, find returns a non-zero exit status. If find encounters an error, it sometimes causes an immediate exit, so some pending commands may not run at all.

But go test does not require matching files in the find command. In fact, it will fail like this:

$ go test ./... go.mod
no required module provides package go.mod; to add it:
    go get go.mod
Copy after login

Fails because go.mod is interpreted as a package.

The solution is to add the terminator -- before {} . See Command Line Flag Syntax:

Flag parsing stops before the first non-flag parameter ("-" is a non-flag parameter) or after the terminator "--".

The above is the detailed content of How to run 'go test' in the parent directory of multiple go modules and return non-zero on error. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template