summaryrefslogtreecommitdiff
path: root/test.myr
blob: 5dd66514f5cda0acc76c584318c1e5f02bd1f302 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std

use "build.use"
use "clean.use"
use "deps.use"
use "opts.use"
use "parse.use"
use "types.use"
use "util.use"
use "subdir.use"

use "config.use"

pkg bld =
	const test	: (p : build# -> void)
;;

const test = {p
	var hasdir, ok, bin

	/* no implicit tests to run */
	ok = true
	hasdir = std.fexists("test")
	if hasdir
		for it in p.targs
			match it
			| `Bin bt:
				if !dotest(p, bt, ok)
					ok = false
				;;
			| `Lib lt:
				if !dotest(p, lt, ok)
					ok = false
				;;
			| _:	/* ignore */
			;;
		;;
	;;
	for `Test t in p.targs
		for s in t.incpath
			if std.sleq(".", s)
				goto founddot
			;;
		;;
		t.incpath = std.slpush(t.incpath, std.sldup("."))

:founddot
		buildbin(p, t, false)
		bin = std.strcat("./", t.name)
		if !runtest(bin)
			ok = false
		;;
		std.slfree(bin)
	;;
	if ok
		std.put("TESTS PASSED\n")
	else
		std.put("TESTS FAILED\n")
		std.exit(1)
	;;
}

const dotest = {p, targ, ok
	var tt, bin ,path, tests

	tests = [][:]
	for s in targ.inputs
		path = std.pathcat("./test", s)
		if std.fexists(path)
			bin = srcswapsuffix(path, "")
			tt = [
				.name = bin,
				.inputs = [path, s][:],
				.install = false,
				.libdeps = targ.libdeps,
				.incpath = targ.incpath,
				.built = false,
			]

			cleantest(p, path)
			buildbin(p, &tt, true)
			tests = std.slpush(tests, bin)
		;;
		std.slfree(path)
	;;

	ok = true
	for t in tests
		if !runtest(t)
			ok = false
		;;
		std.slfree(t)
	;;
	std.slfree(tests)
	-> ok
}

const cleantest = {p, src
	var obj, bin, log, usef

	obj = srcswapsuffix(src, config.Objsuffix)
	log = srcswapsuffix(src, ".log")
	usef = srcswapsuffix(src, ".use")
	bin = srcswapsuffix(src, "")

	std.remove(obj)
	std.remove(usef)
	std.remove(log)
	std.remove(bin)

	std.slfree(obj)
	std.slfree(usef)
	std.slfree(log)
	std.slfree(bin)
}

const runtest = {bin
	var r, log

	std.put("run %s:\t", bin)
	log = std.strcat(bin, ".log")
	match std.spork([bin][:])
	| `std.Fail m:
		std.fatal(1, "unable to run test: %s\n", m)
	| `std.Ok (pid, infd, outfd):
		match std.fslurp(outfd)
		| `std.Ok "":	/* empty output; nothing to log */
		| `std.Ok buf:
			std.blat(log, buf, 0o644)
		| `std.Fail m:
		;;
		std.slfree(log)

		r = false
		match std.wait(pid)
		| `std.Wfailure:	std.put("FAIL\n")
		| `std.Wsignalled:	std.put("CRASH\n")
		| `std.Wsuccess:
			std.put("PASS\n")
			r = true
		| _:	std.put("???\n")
		;;
	;;
	-> r
}