blob: 77e173024b5ed3a4bb25a2c2cd30450d9632e061 [file] [log] [blame]
James Lemieux2a2559e2018-01-28 02:56:27 -08001# Defines a target named $(my_target) for running robolectric tests.
2
3# Running the tests is done in two stages: we first generate the test output to
4# $(my_target_output), which is also added to the dist list, and store the
5# return value of running the tests in $(my_target_retval). After that we
6# process the output and return value as part of $(my_target). This is needed
7# to make sure that we can install the test output even if the tests actually
8# fail.
9
10# Files in which to store the output and return value of the tests.
11my_target_xml := $(intermediates)/$(my_filename_stem)-output.xml
12my_target_output := $(intermediates)/$(my_filename_stem)-output.txt
13my_target_retval := $(intermediates)/$(my_filename_stem)-retval.txt
14
Bob Badour9357e3d2022-04-20 13:02:24 -070015ALL_TARGETS.$(my_target_output).META_LIC:=$(module_license_metadata)
16ALL_TARGETS.$(my_target_xml).META_LIC:=$(module_license_metadata)
17
James Lemieux2a2559e2018-01-28 02:56:27 -080018# We should always re-run the tests, even if nothing has changed.
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070019# So until the build system has a dedicated "no cache" option, claim
20# to write a file that is never produced.
21my_target_nocache := $(intermediates)/$(my_filename_stem)-nocache
22
James Lemieux2a2559e2018-01-28 02:56:27 -080023# Private variables.
24$(my_target_output): PRIVATE_MODULE := $(LOCAL_MODULE)
25$(my_target_output): PRIVATE_TESTS := $(my_tests)
26$(my_target_output): PRIVATE_JARS := $(my_jars)
27$(my_target_output): PRIVATE_JAVA_ARGS := $(my_java_args)
Sorin Bascadcc75f12022-02-02 18:39:08 +000028$(my_target_output): PRIVATE_JAVA_PATH := $(ANDROID_JAVA11_HOME)/bin:
James Lemieux2a2559e2018-01-28 02:56:27 -080029$(my_target_output): PRIVATE_ROBOLECTRIC_PATH := $(my_robolectric_path)
30$(my_target_output): PRIVATE_ROBOLECTRIC_SCRIPT_PATH := $(my_robolectric_script_path)
31$(my_target_output): PRIVATE_TARGET_MESSAGE := $(my_target_message)
32$(my_target_output): PRIVATE_TARGET_OUTPUT := $(my_target_output)
33$(my_target_output): PRIVATE_TARGET_RETVAL := $(my_target_retval)
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070034$(my_target_output): PRIVATE_TARGET_NOCACHE := $(my_target_nocache)
James Lemieux2a2559e2018-01-28 02:56:27 -080035$(my_target_output): PRIVATE_TIMEOUT := $(my_timeout)
36$(my_target_output): PRIVATE_XML_OUTPUT_FILE := $(my_target_xml)
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070037$(my_target_output): .KATI_IMPLICIT_OUTPUTS := $(my_target_xml) $(my_target_retval) $(my_target_nocache)
James Lemieux2a2559e2018-01-28 02:56:27 -080038# Runs the Robolectric tests and saves the output and return value.
39$(my_target_output): $(my_jars)
40 @echo "host Robolectric: $(PRIVATE_MODULE)"
41 # Run `touch` to always create the output XML file, so the build doesn't break even if the
42 # runner failed to create the XML output
43 $(hide) touch "$(PRIVATE_XML_OUTPUT_FILE)"
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070044 $(hide) rm -f "$(PRIVATE_TARGET_NOCACHE)"
James Lemieux2a2559e2018-01-28 02:56:27 -080045 $(hide) \
46 PRIVATE_INTERMEDIATES="$(dir $@)" \
47 PRIVATE_JARS="$(PRIVATE_JARS)" \
48 PRIVATE_JAVA_ARGS="$(PRIVATE_JAVA_ARGS)" \
49 PRIVATE_ROBOLECTRIC_PATH="$(PRIVATE_ROBOLECTRIC_PATH)" \
50 PRIVATE_ROBOLECTRIC_SCRIPT_PATH="$(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)" \
51 PRIVATE_RUN_INDIVIDUALLY="$(ROBOTEST_RUN_INDIVIDUALLY)" \
52 PRIVATE_TARGET_MESSAGE="$(PRIVATE_TARGET_MESSAGE)" \
53 PRIVATE_TIMEOUT="$(PRIVATE_TIMEOUT)" \
54 PRIVATE_TESTS="$(PRIVATE_TESTS)" \
55 XML_OUTPUT_FILE="$(PRIVATE_XML_OUTPUT_FILE)" \
56 TEST_WORKSPACE="$(PRIVATE_MODULE)" \
yelinhsieh32f5dcf2020-01-17 18:35:58 +080057 EVENT_FILE_ROBOLECTRIC="$(EVENT_FILE_ROBOLECTRIC)" \
Pete Gillin390294a2019-06-20 17:00:30 +010058 PATH=$(PRIVATE_JAVA_PATH)$${PATH} \
James Lemieux2a2559e2018-01-28 02:56:27 -080059 $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/wrapper.sh \
60 "$(PRIVATE_MODULE)" \
61 "$(PRIVATE_TARGET_OUTPUT)" \
62 "$(PRIVATE_TARGET_RETVAL)" \
63 wrap \
64 $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/robotest.sh
65
James Lemieux2a2559e2018-01-28 02:56:27 -080066# Private variables.
67$(my_target): PRIVATE_MODULE := $(LOCAL_MODULE)
68$(my_target): PRIVATE_TARGET_OUTPUT := $(my_target_output)
69$(my_target): PRIVATE_TARGET_RETVAL := $(my_target_retval)
70$(my_target): PRIVATE_FAILURE_FATAL := $(my_failure_fatal)
71$(my_target): PRIVATE_ROBOLECTRIC_SCRIPT_PATH := $(my_robolectric_script_path)
72# Process the output and the return value of the tests. This will fail if the
73# return value is non-zero.
74$(my_target): $(my_target_output) $(my_target_xml)
75 $(hide) \
76 result=0; \
77 $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/wrapper.sh \
78 "$(PRIVATE_MODULE)" \
79 "$(PRIVATE_TARGET_OUTPUT)" \
80 "$(PRIVATE_TARGET_RETVAL)" \
81 eval \
82 || result=$$?; \
83 if [ "$(strip $(PRIVATE_FAILURE_FATAL))" = true ]; then \
84 exit "$$result"; \
85 fi
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070086 $(hide) touch $@
James Lemieux2a2559e2018-01-28 02:56:27 -080087
88# Add the output of the tests to the dist list, so that we will include it even
89# if the tests fail.
Dan Willemsen6255fe12018-10-18 10:59:05 -070090$(call dist-for-goals, $(my_phony_target), \
James Lemieux2a2559e2018-01-28 02:56:27 -080091 $(my_target_output):robotests/$(LOCAL_MODULE)-$(notdir $(my_target_output)) \
92 $(my_target_xml):robotests/$(LOCAL_MODULE)-$(notdir $(my_target_xml)))
93
94# Clean up local variables.
95my_target_output :=
96my_target_retval :=
97my_target_xml :=
Dan Willemsen31c4a3a2018-06-19 15:55:05 -070098my_target_nocache :=
Maurice Lam90f0b8e2018-02-26 17:20:41 -080099my_filename_stem :=