mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-01 22:37:51 +08:00
The way we create the kubo binary for coverage is very hacky.
It uses the testing tool. In order to simulate a Kubo binary,
we need to supress all the output that would otherwise be printed
by 'go test'.
So far, we were setting os.Stdout and os.Stderr as a read-only
/dev/null file descriptor. This is causing issues with the new
versions of Go:
error generating coverage report: write /dev/null: bad file descriptor
exit status 2
Updating it to a Read-Write file descriptor solves the problem.
I did not try looking into what is causing this issue now. There have
been some updates to the 'go test' tool in Go 1.20 and it is likely
that some error is now being checked for that hasn't been checked
before. Writing to a read-only file descriptor always failed. But
the error was just supressed somehow.
31 lines
672 B
Go
31 lines
672 B
Go
//go:build testrunmain
|
|
// +build testrunmain
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// this abuses go so much that I felt dirty writing this code
|
|
// but it is the only way to do it without writing custom compiler that would
|
|
// be a clone of go-build with go-test.
|
|
func TestRunMain(t *testing.T) {
|
|
args := flag.Args()
|
|
os.Args = append([]string{os.Args[0]}, args...)
|
|
ret := mainRet()
|
|
|
|
p := os.Getenv("IPFS_COVER_RET_FILE")
|
|
if len(p) != 0 {
|
|
os.WriteFile(p, []byte(fmt.Sprintf("%d\n", ret)), 0o777)
|
|
}
|
|
|
|
// close outputs so go testing doesn't print anything
|
|
null, _ := os.OpenFile(os.DevNull, os.O_RDWR, 0755)
|
|
os.Stderr = null
|
|
os.Stdout = null
|
|
}
|