aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/rosa_test.go
blob: ce3dad42529ab23db191ccf96524c212a001e274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package rosa_test

import (
	"context"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"testing"

	"hakurei.app/check"
	"hakurei.app/internal/rosa"
	"hakurei.app/message"
	"hakurei.app/pkg"
)

var (
	// skipBuildTest caches whether build tests are skipped.
	skipBuildTest bool
	// buildTestCache is populated by getCache and must not be accessed directly.
	buildTestCache *pkg.Cache
	// buildTestCacheCancel cancels buildTestCache.
	buildTestCacheCancel context.CancelFunc
	// buildTestCacheOnce synchronises access to buildTestCache.
	buildTestCacheOnce sync.Once
)

func TestMain(m *testing.M) {
	code := m.Run()
	if buildTestCache != nil {
		buildTestCacheCancel()
		buildTestCache.Close()
	}
	os.Exit(code)
}

func getCache(t *testing.T) *pkg.Cache {
	t.Helper()
	const env = "ROSA_BUILD_TEST_CACHE_DIR"

	if !testing.Verbose() {
		t.Skip("verbose flag not set")
	}

	buildTestCacheOnce.Do(func() {
		if p, ok := os.LookupEnv(env); !ok {
			skipBuildTest = true
			return
		} else if a, err := check.NewAbs(p); err != nil {
			t.Fatal(err)
		} else {
			var ctx context.Context
			ctx, buildTestCacheCancel = signal.NotifyContext(context.Background(),
				syscall.SIGINT, syscall.SIGTERM)

			msg := message.New(log.New(os.Stderr, "\x1b[2mrosa: \x1b[0m", 0))
			msg.SwapVerbose(true)

			if buildTestCache, err = pkg.Open(ctx, msg, a, &pkg.CacheAttr{
				Flags: pkg.CSuppressInit | pkg.CColourOutput,
			}); err != nil {
				t.Fatal(err)
			}
		}
	})

	if skipBuildTest {
		t.Skip(env + " not set")
	}
	return buildTestCache
}

func TestCureAll(t *testing.T) {
	cache := getCache(t)
	t.Parallel()

	for _, p := range rosa.CollectAll() {
		_, a := rosa.MustLoad(p)
		t.Run(p.String(), func(t *testing.T) {
			t.Parallel()

			if pathname, _, err := cache.Cure(a); err != nil {
				t.Fatal(err)
			} else {
				t.Log(pathname)
			}
		})
	}
}

func BenchmarkStage3(b *testing.B) {
	t := rosa.New().Std()

	llvm := rosa.H("llvm")
	for b.Loop() {
		t.MustLoad(llvm)

		b.StopTimer()
		t.DropCaches("", 0)
		b.StartTimer()
	}
}