aboutsummaryrefslogtreecommitdiffstats
path: root/test/plugin_xref.mk
blob: 0246e239fd954a6b1fcc82f3383ebf1c7ec153ad (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Xref plugin.

XREF_TARGETS = $(call list_targets,xref)

.PHONY: xref $(XREF_TARGETS)

xref: $(XREF_TARGETS)

xref-check: init

	$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 "Run the Xref plugin"
	$t $(MAKE) -C $(APP) xref $v

	$i "Create a module with an undefined function call"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin again, expect an error"
	$t ! $(MAKE) -C $(APP) xref $v

xref-check-custom: init

	$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 "Run the Xref plugin with undefined_functions"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS=undefined_functions $v

	$i "Create a module with an unused export"
	$t printf "%s\n" \
		"-module(bad1)." \
		"-export([f/0])." \
		"f() -> whereis(user) ! bad_message." \
		> $(APP)/src/bad1.erl

	$i "Run the Xref plugin with exports_not_used, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_CHECKS=exports_not_used $v

	$i "Run the Xref plugin with multiple checks"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="[undefined_function_calls, undefined_functions]" $v

	$i "Create a module with an undefined function call"
	$t printf "%s\n" \
		"-module(bad2)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/src/bad2.erl

	$i "Run the Xref plugin with multiple checks, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_CHECKS="[undefined_function_calls, undefined_functions]" $v

xref-check-informational: init

	$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 "Run the Xref plugin with module_use"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="{module_use, $(APP)_sup}" > $(APP)/output.txt

	$i "Confirm that the module was found"
	$t grep -q "\- $(APP)_app$$" $(APP)/output.txt

xref-query: init

	$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 "Run the Xref plugin with query XC (external calls)"
	$t $(MAKE) -C $(APP) xref q="XC" > $(APP)/output.txt

	$i "Confirm that the supervisor:start_link/3 call was found"
	$t grep -q "\- supervisor:start_link/3 called by $(APP)_sup:start_link/0$$" $(APP)/output.txt

xref-scope-apps: init

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

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

	$i "Create a module with an undefined function call inside my_app"
	$t printf "%s\n" \
		"-module(bad2)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/apps/my_app/src/bad2.erl

	$i "Run the Xref plugin with apps in the scope, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_SCOPE="app apps" $v

xref-scope-deps: init

	$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 "Bootstrap a new OTP library named my_dep inside $(APP)"
	$t mkdir $(APP)/my_dep
	$t cp ../erlang.mk $(APP)/my_dep/
	$t $(MAKE) -C $(APP)/my_dep/ -f erlang.mk bootstrap-lib $v

	$i "Create a module with an undefined function call inside my_dep"
	$t printf "%s\n" \
		"-module(bad2)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/my_dep/src/bad2.erl

	$i "Add my_dep to the list of dependencies"
	$t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = my_dep\ndep_my_dep = cp $(CURDIR)/$(APP)/my_dep/\n"}' $(APP)/Makefile

ifdef LEGACY
	$i "Add my_dep to the applications key in the .app.src file"
	$t perl -ni.bak -e 'print;if ($$.==7) {print "\t\tmy_dep,\n"}' $(APP)/src/$(APP).app.src
endif

	$i "Run the Xref plugin with deps in the scope, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_SCOPE="app deps" $v

xref-scope-otp: init

	$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 "Run the Xref plugin for module use with OTP in the scope"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="{module_use, asn1ct_pretty_format}" \
		XREF_SCOPE="app otp" > $(APP)/output.txt

	$i "Confirm that the asn1ct_pretty_format module use was analysed"
	$t grep -q "\- asn1ct_pretty_format$$" $(APP)/output.txt

xref-extra-apps: init

	$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 "Bootstrap a new OTP library named extra_app inside $(APP)"
	$t mkdir $(APP)/extra_app
	$t cp ../erlang.mk $(APP)/extra_app/
	$t $(MAKE) -C $(APP)/extra_app/ -f erlang.mk bootstrap-lib $v

	$i "Create a module in extra_app with a function call to $(APP)"
	$t printf "%s\n" \
		"-module(extra)." \
		"-export([f/0])." \
		"f() -> $(APP)_sup:init([])." \
		> $(APP)/extra_app/src/extra.erl

	$i "Build extra_app"
	$t $(MAKE) -C $(APP)/extra_app $v

	$i "Run the Xref plugin for application use with the extra app"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="{application_use, $(APP)}" \
		XREF_EXTRA_APP_DIRS="extra_app/" > $(APP)/output.txt

	$i "Confirm that the extra_app application call was found"
	$t grep -q "\- extra_app$$" $(APP)/output.txt

xref-extra-dirs: init

	$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 "Bootstrap a new OTP library named extra_dir inside $(APP)"
	$t mkdir $(APP)/extra_dir
	$t cp ../erlang.mk $(APP)/extra_dir/
	$t $(MAKE) -C $(APP)/extra_dir/ -f erlang.mk bootstrap-lib $v

	$i "Create a module in extra_dir with an undefined function call"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/extra_dir/src/bad.erl

	$i "Build extra_dir"
	$t $(MAKE) -C $(APP)/extra_dir $v

	$i "Run the Xref plugin with the extra dir, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_EXTRA_DIRS="extra_dir/ebin/" $v

xref-ignore-inline-fa: init

	$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 "Create a module with an undefined function call and an inline ignore"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0])." \
		"-ignore_xref([{f,0}])." \
		"f() -> f_module:f_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin, expect success"
	$t $(MAKE) -C $(APP) xref $v

xref-ignore-inline-mfa: init

	$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 "Create a module with undefined function calls and inline ignores"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0, g/0])." \
		"-ignore_xref([{bad,f,0}])." \
		"-ignore_xref({g_module,g_not_exist,0})." \
		"f() -> f_module:f_not_exist()." \
		"g() -> g_module:g_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin, expect success"
	$t $(MAKE) -C $(APP) xref $v

xref-ignore-inline-mod: init

	$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 "Create a module with undefined function calls and inline ignores"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0, g/0])." \
		"-ignore_xref([?MODULE])." \
		"-ignore_xref(g_module)." \
		"f() -> f_module:f_not_exist()." \
		"g() -> g_module:g_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin, expect success"
	$t $(MAKE) -C $(APP) xref $v

xref-ignore-project-wide-mfa: init

	$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 "Create a module with an undefined function call"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin with project-wide ignore, expect success"
	$t $(MAKE) -C $(APP) xref XREF_IGNORE="{bad,f,0}" $v

xref-ignore-project-wide-mod: init

	$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 "Create a module with an undefined function call"
	$t printf "%s\n" \
		"-module(bad)." \
		"-export([f/0])." \
		"f() -> this_module:does_not_exist()." \
		> $(APP)/src/bad.erl

	$i "Run the Xref plugin with project-wide ignore, expect success"
	$t $(MAKE) -C $(APP) xref XREF_IGNORE="[bad]" $v

xref-ignore-callbacks: init

	$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 "Run the Xref plugin for exports_not_used, expect success"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="exports_not_used" $v

	$i "Run the Xref plugin again with explicit ignoring of callbacks, expect success"
	$t $(MAKE) -C $(APP) xref XREF_CHECKS="exports_not_used" XREF_IGNORE_CALLBACKS=1 $v

	$i "Run the Xref plugin again without ignoring callbacks, expect an error"
	$t ! $(MAKE) -C $(APP) xref XREF_CHECKS="exports_not_used" XREF_IGNORE_CALLBACKS=0 $v