aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Android/testbed
diff options
context:
space:
mode:
Diffstat (limited to 'Android/testbed')
-rw-r--r--Android/testbed/app/build.gradle.kts20
-rw-r--r--Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt8
-rw-r--r--Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt27
-rw-r--r--Android/testbed/app/src/main/python/android_testbed_main.py (renamed from Android/testbed/app/src/main/python/main.py)20
-rw-r--r--Android/testbed/build.gradle.kts2
-rw-r--r--Android/testbed/gradle/wrapper/gradle-wrapper.properties2
6 files changed, 63 insertions, 16 deletions
diff --git a/Android/testbed/app/build.gradle.kts b/Android/testbed/app/build.gradle.kts
index c627cb1b0e0..92cffd61f86 100644
--- a/Android/testbed/app/build.gradle.kts
+++ b/Android/testbed/app/build.gradle.kts
@@ -85,7 +85,7 @@ android {
minSdk = androidEnvFile.useLines {
for (line in it) {
- """api_level:=(\d+)""".toRegex().find(line)?.let {
+ """ANDROID_API_LEVEL:=(\d+)""".toRegex().find(line)?.let {
return@useLines it.groupValues[1].toInt()
}
}
@@ -205,11 +205,29 @@ androidComponents.onVariants { variant ->
into("site-packages") {
from("$projectDir/src/main/python")
+
+ val sitePackages = findProperty("python.sitePackages") as String?
+ if (!sitePackages.isNullOrEmpty()) {
+ if (!file(sitePackages).exists()) {
+ throw GradleException("$sitePackages does not exist")
+ }
+ from(sitePackages)
+ }
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
exclude("**/__pycache__")
}
+
+ into("cwd") {
+ val cwd = findProperty("python.cwd") as String?
+ if (!cwd.isNullOrEmpty()) {
+ if (!file(cwd).exists()) {
+ throw GradleException("$cwd does not exist")
+ }
+ from(cwd)
+ }
+ }
}
}
diff --git a/Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt b/Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt
index 0e888ab71d8..94be52dd2dc 100644
--- a/Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt
+++ b/Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt
@@ -17,11 +17,11 @@ class PythonSuite {
fun testPython() {
val start = System.currentTimeMillis()
try {
- val context =
+ val status = PythonTestRunner(
InstrumentationRegistry.getInstrumentation().targetContext
- val args =
- InstrumentationRegistry.getArguments().getString("pythonArgs", "")
- val status = PythonTestRunner(context).run(args)
+ ).run(
+ InstrumentationRegistry.getArguments()
+ )
assertEquals(0, status)
} finally {
// Make sure the process lives long enough for the test script to
diff --git a/Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt b/Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt
index c4bf6cbe83d..ef28948486f 100644
--- a/Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt
+++ b/Android/testbed/app/src/main/java/org/python/testbed/MainActivity.kt
@@ -15,17 +15,29 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
- val status = PythonTestRunner(this).run("-W -uall")
+ val status = PythonTestRunner(this).run("-m", "test", "-W -uall")
findViewById<TextView>(R.id.tvHello).text = "Exit status $status"
}
}
class PythonTestRunner(val context: Context) {
- /** @param args Extra arguments for `python -m test`.
- * @return The Python exit status: zero if the tests passed, nonzero if
- * they failed. */
- fun run(args: String = "") : Int {
+ fun run(instrumentationArgs: Bundle) = run(
+ instrumentationArgs.getString("pythonMode")!!,
+ instrumentationArgs.getString("pythonModule")!!,
+ instrumentationArgs.getString("pythonArgs") ?: "",
+ )
+
+ /** Run Python.
+ *
+ * @param mode Either "-c" or "-m".
+ * @param module Python statements for "-c" mode, or a module name for
+ * "-m" mode.
+ * @param args Arguments to add to sys.argv. Will be parsed by `shlex.split`.
+ * @return The Python exit status: zero on success, nonzero on failure. */
+ fun run(mode: String, module: String, args: String) : Int {
+ Os.setenv("PYTHON_MODE", mode, true)
+ Os.setenv("PYTHON_MODULE", module, true)
Os.setenv("PYTHON_ARGS", args, true)
// Python needs this variable to help it find the temporary directory,
@@ -36,8 +48,9 @@ class PythonTestRunner(val context: Context) {
System.loadLibrary("main_activity")
redirectStdioToLogcat()
- // The main module is in src/main/python/main.py.
- return runPython(pythonHome.toString(), "main")
+ // The main module is in src/main/python. We don't simply call it
+ // "main", as that could clash with third-party test code.
+ return runPython(pythonHome.toString(), "android_testbed_main")
}
private fun extractAssets() : File {
diff --git a/Android/testbed/app/src/main/python/main.py b/Android/testbed/app/src/main/python/android_testbed_main.py
index d6941b14412..31b8e5343a8 100644
--- a/Android/testbed/app/src/main/python/main.py
+++ b/Android/testbed/app/src/main/python/android_testbed_main.py
@@ -26,7 +26,23 @@ import sys
# test_signals in test_threadsignals.py.
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGUSR1])
+mode = os.environ["PYTHON_MODE"]
+module = os.environ["PYTHON_MODULE"]
sys.argv[1:] = shlex.split(os.environ["PYTHON_ARGS"])
-# The test module will call sys.exit to indicate whether the tests passed.
-runpy.run_module("test")
+cwd = f"{sys.prefix}/cwd"
+if not os.path.exists(cwd):
+ # Empty directories are lost in the asset packing/unpacking process.
+ os.mkdir(cwd)
+os.chdir(cwd)
+
+if mode == "-c":
+ # In -c mode, sys.path starts with an empty string, which means whatever the current
+ # working directory is at the moment of each import.
+ sys.path.insert(0, "")
+ exec(module, {})
+elif mode == "-m":
+ sys.path.insert(0, os.getcwd())
+ runpy.run_module(module, run_name="__main__", alter_sys=True)
+else:
+ raise ValueError(f"unknown mode: {mode}")
diff --git a/Android/testbed/build.gradle.kts b/Android/testbed/build.gradle.kts
index 4d1d6f87594..451517b3f1a 100644
--- a/Android/testbed/build.gradle.kts
+++ b/Android/testbed/build.gradle.kts
@@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
- id("com.android.application") version "8.6.1" apply false
+ id("com.android.application") version "8.10.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
diff --git a/Android/testbed/gradle/wrapper/gradle-wrapper.properties b/Android/testbed/gradle/wrapper/gradle-wrapper.properties
index 36529c89642..5d42fbae084 100644
--- a/Android/testbed/gradle/wrapper/gradle-wrapper.properties
+++ b/Android/testbed/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Feb 19 20:29:06 GMT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists