Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Termux in InlineDelegateByteBuddyMockMaker.java #3158

Merged
merged 2 commits into from Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -96,6 +96,8 @@ dependencies {
implementation libraries.objenesis

testImplementation libraries.assertj
testImplementation libraries.junitJupiterApi
testImplementation libraries.junitJupiterParams
TimvdLippe marked this conversation as resolved.
Show resolved Hide resolved

testUtil sourceSets.test.output

Expand Down
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Expand Up @@ -10,6 +10,7 @@ versions.errorprone = '2.23.0'

libraries.junit4 = 'junit:junit:4.13.2'
libraries.junitJupiterApi = "org.junit.jupiter:junit-jupiter-api:${versions.junitJupiter}"
libraries.junitJupiterParams = "org.junit.jupiter:junit-jupiter-params:${versions.junitJupiter}"
libraries.junitPlatformLauncher = 'org.junit.platform:junit-platform-launcher:1.10.0'
libraries.junitJupiterEngine = "org.junit.jupiter:junit-jupiter-engine:${versions.junitJupiter}"
libraries.junitVintageEngine = "org.junit.vintage:junit-vintage-engine:${versions.junitJupiter}"
Expand Down
Expand Up @@ -207,9 +207,7 @@ class InlineDelegateByteBuddyMockMaker
InlineDelegateByteBuddyMockMaker() {
if (INITIALIZATION_ERROR != null) {
String detail;
if (System.getProperty("java.specification.vendor", "")
.toLowerCase()
.contains("android")) {
if (PlatformUtils.isAndroidPlatform() || PlatformUtils.isProbablyTermuxEnvironment()) {
detail =
"It appears as if you are trying to run this mock maker on Android which does not support the instrumentation API.";
} else {
Expand Down
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;

/**
* Helpers for various platform detection mechanisms related to the ByteBuddy mock maker.
*
* @author Ashley Scopes
*/
final class PlatformUtils {
private PlatformUtils() {
// Static-only class.
}

static boolean isAndroidPlatform() {
return System.getProperty("java.specification.vendor", "")
.toLowerCase()
.contains("android");
}

static boolean isProbablyTermuxEnvironment() {
boolean isLinux = System.getProperty("os.name", "").equalsIgnoreCase("linux");
boolean isInTermuxData =
System.getProperty("java.home", "").toLowerCase().contains("/com.termux/");
return isLinux && isInTermuxData;
}
}
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2023 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;

import java.util.Properties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.parallel.Isolated;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Ashley Scopes
*/
@Isolated("modifies system properties temporarily")
class PlatformUtilsTest {
Properties globalProperties;

@BeforeEach
void setUp() {
globalProperties = new Properties();
globalProperties.putAll(System.getProperties());
System.getProperties().clear();
}

@AfterEach
void tearDown() {
System.getProperties().clear();
System.getProperties().putAll(globalProperties);
}

@CsvSource(
value = {
"java.specification.vendor, expected result",
" android, true",
" AnDrOId, true",
" anythingElse, false",
" , false",
},
useHeadersInDisplayName = true)
@ParameterizedTest
void isAndroidPlatform_returns_expected_value(String vendor, boolean result) {
// Given
setProperty("java.specification.vendor", vendor);

// Then
assertThat(PlatformUtils.isAndroidPlatform()).isEqualTo(result);
}

@CsvSource(
value = {
" os.name, java.home, expected result",
" linux, /data/data/com.termux/whatever, true",
" linux, /foo/bar/com.termux/somedir, true",
" LINUX, /data/data/com.termux/whatever, true",
" Linux, /foo/bar/com.termux/somedir, true",
"Mac OS X 13.0, /foo/bar/com.termux/somedir, false",
" Windows 10, /foo/bar/com.termux/somedir, false",
" OS/2, /foo/bar/com.termux/somedir, false",
" Linux, /usr/share/java, false",
" , , false",
" foo, , false",
" , foo, false",
" linux, , false",
" , /data/data/com.termux/whatever, false",
},
useHeadersInDisplayName = true)
@ParameterizedTest
void isProbablyTermuxEnvironment_returns_expected_value(
String os, String javaHome, boolean result) {
// Given
setProperty("os.name", os);
setProperty("java.home", javaHome);

// Then
assertThat(PlatformUtils.isProbablyTermuxEnvironment()).isEqualTo(result);
}

static void setProperty(String name, String value) {
if (value == null) {
System.clearProperty(name);
} else {
System.setProperty(name, value);
}
}
}