| 1 | // Only built when collecting coverage via "go test -tags testrunmain". |
| 2 | //go:build testrunmain |
| 3 | |
| 4 | package main_test |
| 5 | |
| 6 | import ( |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/ipfs/kubo/cmd/ipfs/kubo" |
| 13 | ) |
| 14 | |
| 15 | // this abuses go so much that I felt dirty writing this code |
| 16 | // but it is the only way to do it without writing custom compiler that would |
| 17 | // be a clone of go-build with go-test. |
| 18 | func TestRunMain(t *testing.T) { |
| 19 | args := flag.Args() |
| 20 | os.Args = append([]string{os.Args[0]}, args...) |
| 21 | |
| 22 | ret := kubo.Start(kubo.BuildDefaultEnv) |
| 23 | |
| 24 | p := os.Getenv("IPFS_COVER_RET_FILE") |
| 25 | if len(p) != 0 { |
| 26 | os.WriteFile(p, []byte(fmt.Sprintf("%d\n", ret)), 0o777) |
| 27 | } |
| 28 | |
| 29 | // close outputs so go testing doesn't print anything |
| 30 | null, _ := os.OpenFile(os.DevNull, os.O_RDWR, 0755) |
| 31 | os.Stderr = null |
| 32 | os.Stdout = null |
| 33 | } |