1111
1212DEFAULT_CONFIG_SNIPPET = """ defaultConfig {\n testInstrumentationRunner \" androidx.test.runner.AndroidJUnitRunner\" \n }\n \n """
1313
14- CONFIGURATION_GUARDS = (
15- "configurations.maybeCreate(\" androidTestImplementation\" )\n " ,
16- "configurations.maybeCreate(\" androidTestCompile\" )\n " ,
17- )
18-
1914ANDROID_TEST_DEPENDENCIES = (
2015 "androidx.test:core:1.5.0" ,
2116 "androidx.test.ext:junit:1.1.5" ,
@@ -87,32 +82,17 @@ def ensure_instrumentation_runner(self) -> None:
8782 insert = android_block [0 ] + len ("android {" )
8883 self .content = self .content [:insert ] + DEFAULT_CONFIG_SNIPPET + self .content [insert :]
8984
90- def ensure_configuration_guards (self ) -> None :
91- missing = [snippet for snippet in CONFIGURATION_GUARDS if snippet not in self .content ]
92- if not missing :
93- return
94- insert = 0
95- plugins_block = self .find_block ("plugins" )
96- if plugins_block :
97- insert = plugins_block [1 ] + 1
98- self .content = (
99- self .content [:insert ] + "" .join (missing ) + "\n " + self .content [insert :]
100- )
101-
10285 def ensure_dependencies (self ) -> None :
10386 block = self .find_block ("dependencies" )
10487 if not block :
10588 raise SystemExit ("Unable to locate dependencies block in Gradle file" )
10689 insertion_point = block [1 ] - 1
10790 existing_block = self .content [block [0 ]:block [1 ]]
91+ configuration = self ._select_configuration (existing_block )
10892 for coordinate in ANDROID_TEST_DEPENDENCIES :
10993 if self ._has_dependency (existing_block , coordinate ):
11094 continue
111- line = (
112- f" add(\" androidTestImplementation\" , \" { coordinate } \" )\n "
113- f" add(\" androidTestCompile\" , \" { coordinate } \" )\n "
114- )
115- combined = "" .join (line )
95+ combined = f" { configuration } \" { coordinate } \" \n "
11696 self .content = (
11797 self .content [:insertion_point ]
11898 + combined
@@ -127,14 +107,19 @@ def _has_dependency(block: str, coordinate: str) -> bool:
127107 return bool (
128108 re .search (rf"androidTestImplementation\s+['\"]{ escaped } ['\"]" , block )
129109 or re .search (rf"androidTestCompile\s+['\"]{ escaped } ['\"]" , block )
130- or re .search (rf"add\(\"androidTestImplementation\",\s*['\"]{ escaped } ['\"]\)" , block )
131- or re .search (rf"add\(\"androidTestCompile\",\s*['\"]{ escaped } ['\"]\)" , block )
132110 )
133111
112+ @staticmethod
113+ def _select_configuration (block : str ) -> str :
114+ if "androidTestImplementation" in block :
115+ return "androidTestImplementation"
116+ if "androidTestCompile" in block :
117+ return "androidTestCompile"
118+ return "androidTestImplementation"
119+
134120 def apply (self ) -> None :
135121 self .ensure_test_options ()
136122 self .ensure_instrumentation_runner ()
137- self .ensure_configuration_guards ()
138123 self .ensure_dependencies ()
139124
140125
0 commit comments