aboutsummaryrefslogtreecommitdiffstats
path: root/test/plugin_eunit.mk
blob: 900b45ee9d77e32d7d479bff6ed0b80e7d144ae1 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# EUnit plugin.

EUNIT_CASES = all apps-only check erl-opts fun mod priv test-dir tests multiapps-no-root-check-exit-code
EUNIT_TARGETS = $(addprefix eunit-,$(EUNIT_CASES))

.PHONY: eunit $(EUNIT_TARGETS)

eunit: $(EUNIT_TARGETS)

eunit-all: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Check that EUnit detects no tests"
	$t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Build the project cleanly"
	$t $(MAKE) -C $(APP) clean $v
	$t $(MAKE) -C $(APP) $v

	$i "Check that no EUnit test cases were exported"
	$t $(ERL) -pa $(APP)/ebin -eval 'code:load_file($(APP)), false = erlang:function_exported($(APP), ok_test, 0), halt()'

	$i "Check that EUnit runs tests"
	$t $(MAKE) -C $(APP) eunit | grep -q "Test passed."

	$i "Add a failing test to the module"
	$t printf "%s\n" \
		"-ifdef(TEST)." \
		"bad_test() -> throw(fail)." \
		"-endif." >> $(APP)/src/$(APP).erl

	$i "Check that EUnit errors out"
	$t ! $(MAKE) -C $(APP) eunit $v

eunit-apps-only: build clean

	$i "Create a multi application repository with no root application"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t echo "include erlang.mk" > $(APP)/Makefile

	$i "Create a new application named my_app"
	$t $(MAKE) -C $(APP) new-app in=my_app $v

	$i "Create a new library named my_lib"
	$t $(MAKE) -C $(APP) new-lib in=my_lib $v

	$i "Check that EUnit detects no tests"
	$t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."

	$i "Generate a module containing EUnit tests in my_app"
	$t printf "%s\n" \
		"-module(my_app)." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/apps/my_app/src/my_app.erl

	$i "Generate a module containing EUnit tests in my_lib"
	$t printf "%s\n" \
		"-module(my_lib)." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/apps/my_lib/src/my_lib.erl

	$i "Check that EUnit runs tests"
	$t $(MAKE) -C $(APP) eunit | grep -q "Test passed."

	$i "Check that EUnit runs tests"

eunit-multiapps-no-root-check-exit-code: build clean

		$i "Create a multi application repository with no root application"
		$t mkdir $(APP)/
		$t cp ../erlang.mk $(APP)/
		$t echo "include erlang.mk" > $(APP)/Makefile

		$i "Create a new application named my_app1"
		$t $(MAKE) -C $(APP) new-app in=my_app1 $v

		$i "Create a new application named my_app2"
		$t $(MAKE) -C $(APP) new-app in=my_app2 $v

		$i "Create a new library named my_lib"
		$t $(MAKE) -C $(APP) new-lib in=my_lib $v

		$i "Check that EUnit detects no tests"
		$t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."

		$i "Generate a module containing broken EUnit tests in my_app1"
		$t printf "%s\n" \
			"-module(my_app1)." \
			"-ifdef(TEST)." \
			"-include_lib(\"eunit/include/eunit.hrl\")." \
			"bad_test() -> ?assert(0 =:= 1)." \
			"-endif." > $(APP)/apps/my_app1/src/my_app1.erl

		$i "Generate a module containing EUnit good tests in my_app2"
		$t printf "%s\n" \
			"-module(my_app2)." \
			"-ifdef(TEST)." \
			"-include_lib(\"eunit/include/eunit.hrl\")." \
			"ok_test() -> ok." \
			"-endif." > $(APP)/apps/my_app2/src/my_app2.erl

		$i "Generate a module containing EUnit tests in my_lib"
		$t printf "%s\n" \
			"-module(my_lib)." \
			"-ifdef(TEST)." \
			"-include_lib(\"eunit/include/eunit.hrl\")." \
			"ok_test() -> ok." \
			"-endif." > $(APP)/apps/my_lib/src/my_lib.erl

		$i "Check exit code of EUnit for the module with broken test should be non-zero"
		$t ( set +e; $(MAKE) -C $(APP)/apps/my_app1 eunit ; if [ $$? -ne 0 ] ; then \
			echo "Test passed exit-code is non-zero" ; \
		else \
			echo "Test failed exit-code is zero" ; \
			exit 100; \
		fi )

		$i "Check exit code of EUnit for the whole project with broken test should be non-zero"
		$t ( set +e; $(MAKE) -C $(APP) eunit ; if [ $$? -ne 0 ] ; then \
			echo "Test passed exit-code is non-zero" ; \
		else \
			echo "Test failed exit-code is zero" ; \
			exit 100; \
		fi )



eunit-check: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Check that EUnit runs on 'make check'"
	$t $(MAKE) -C $(APP) check | grep -q "Test passed."

eunit-erl-opts: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Set EUNIT_ERL_OPTS in the Makefile"
	$t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Check that EUnit uses EUNIT_ERL_OPTS"
	$t $(MAKE) -C $(APP) eunit | grep -q "hello"

eunit-fun: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"bad_test() -> throw(fail)." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Check that we can run EUnit on a specific test"
	$t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v

eunit-mod: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Generate a module containing failing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP)_fail)." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"bad_test() -> throw(fail)." \
		"-endif." > $(APP)/src/$(APP)_fail.erl

	$i "Check that we can run EUnit on a specific module"
	$t $(MAKE) -C $(APP) eunit t=$(APP) $v

eunit-priv: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Check that EUnit can resolve the priv_dir"
	$t $(MAKE) -C $(APP) tests | grep -q "Test passed."

eunit-test-dir: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Generate a module containing EUnit tests in TEST_DIR"
	$t mkdir $(APP)/test
	$t printf "%s\n" \
		"-module($(APP)_tests)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl

	$i "Check that EUnit runs both tests"
	$t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."

	$i "Check that tests were both run only once"
	$t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -

eunit-tests: build clean

	$i "Bootstrap a new OTP application named $(APP)"
	$t mkdir $(APP)/
	$t cp ../erlang.mk $(APP)/
	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v

	$i "Generate a module containing EUnit tests"
	$t printf "%s\n" \
		"-module($(APP))." \
		"-ifdef(TEST)." \
		"-include_lib(\"eunit/include/eunit.hrl\")." \
		"ok_test() -> ok." \
		"-endif." > $(APP)/src/$(APP).erl

	$i "Check that EUnit runs on 'make tests'"
	$t $(MAKE) -C $(APP) tests | grep -q "Test passed."