1package main
2
3import (
4	"os"
5	"os/signal"
6	"strings"
7	"syscall"
8	"testing"
9)
10
11// This is an instrumented main, used when running integration tests (sytest) with code coverage.
12// Compile: go test -c -race -cover -covermode=atomic -o monolith.debug -coverpkg "github.com/matrix-org/..." ./cmd/dendrite-monolith-server
13// Run the monolith: ./monolith.debug -test.coverprofile=/somewhere/to/dump/integrationcover.out DEVEL --config dendrite.yaml
14// Generate HTML with coverage: go tool cover -html=/somewhere/where/there/is/integrationcover.out -o cover.html
15// Source: https://dzone.com/articles/measuring-integration-test-coverage-rate-in-pouchc
16func TestMain(_ *testing.T) {
17	var (
18		args []string
19	)
20
21	for _, arg := range os.Args {
22		switch {
23		case strings.HasPrefix(arg, "DEVEL"):
24		case strings.HasPrefix(arg, "-test"):
25		default:
26			args = append(args, arg)
27		}
28	}
29	// only run the tests if there are args to be passed
30	if len(args) <= 1 {
31		return
32	}
33
34	waitCh := make(chan int, 1)
35	os.Args = args
36	go func() {
37		main()
38		close(waitCh)
39	}()
40
41	signalCh := make(chan os.Signal, 1)
42	signal.Notify(signalCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
43
44	select {
45	case <-signalCh:
46		return
47	case <-waitCh:
48		return
49	}
50}
51