Skip to content

Commit 4839648

Browse files
committed
Temporarily switch JavaKitTests target to XCTest
Command-line testing on macOS is broken for the JavaKitTests due to an interaction between the Java Virtual Machine, a helper test process for swift-testing (`swiftpm-testing-helper`), and some macOS security features. Work around the issue by replacing our use of swift-testing with XCTest, whose helper process doesn't seem to have this problem. Only do so for test targets that need to spin up an instance of the JVM, and keep using swift-testing everywhere we can. This makes me very sad, but is better than the current solution of disabling these tests when running from the terminal. Once we've found a solution for the underlying issue, we'll revert this change as get back to swift-testing and its much nicer testing output. Fixes issue #43 without disabling tests.
1 parent e40db8b commit 4839648

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

Tests/JavaKitTests/BasicRuntimeTests.swift

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,68 @@
1515
import JavaKit
1616
import JavaKitNetwork
1717
import JavaKitVM
18-
import Testing
18+
import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43
1919

2020
@MainActor
2121
let jvm = try! JavaVirtualMachine(vmOptions: [])
2222

23-
@Suite
2423
@MainActor
25-
struct BasicRuntimeTests {
26-
@Test("Object management", .disabled(if: isLinux, "Attempts to refcount a null pointer on Linux"))
27-
func javaObjectManagement() throws {
24+
class BasicRuntimeTests: XCTestCase {
25+
func testJavaObjectManagement() throws {
26+
if isLinux {
27+
throw XCTSkip("Attempts to refcount a null pointer on Linux")
28+
}
29+
2830
let sneakyJavaThis: jobject
2931
do {
3032
let object = JavaObject(environment: jvm.environment)
31-
#expect(object.toString().starts(with: "java.lang.Object"))
33+
XCTAssert(object.toString().starts(with: "java.lang.Object"))
3234

3335
// Make sure this object was promoted to a global reference.
34-
#expect(object.javaEnvironment.pointee?.pointee.GetObjectRefType(object.javaEnvironment, object.javaThis) == JNIGlobalRefType)
36+
XCTAssertEqual(object.javaEnvironment.pointee?.pointee.GetObjectRefType(object.javaEnvironment, object.javaThis), JNIGlobalRefType)
3537

3638
// Keep track of the Java object.
3739
sneakyJavaThis = object.javaThis
3840
}
3941

4042
// The reference should now be invalid, because we've deleted the
4143
// global reference.
42-
#expect(jvm.environment.pointee?.pointee.GetObjectRefType(jvm.environment, sneakyJavaThis) == JNIInvalidRefType)
44+
XCTAssertEqual(jvm.environment.pointee?.pointee.GetObjectRefType(jvm.environment, sneakyJavaThis), JNIInvalidRefType)
4345

4446
// 'super' and 'as' don't require allocating a new holder.
4547
let url = try URL("http://swift.org", environment: jvm.environment)
4648
let superURL = url.super
47-
#expect(url.javaHolder === superURL.javaHolder)
49+
XCTAssert(url.javaHolder === superURL.javaHolder)
4850
let urlAgain = superURL.as(URL.self)!
49-
#expect(url.javaHolder === urlAgain.javaHolder)
51+
XCTAssert(url.javaHolder === urlAgain.javaHolder)
5052
}
5153

52-
@Test("Java exceptions", .disabled(if: isLinux, "Attempts to refcount a null pointer on Linux"))
53-
func javaExceptionsInSwift() throws {
54+
func testJavaExceptionsInSwift() throws {
55+
if isLinux {
56+
throw XCTSkip("Attempts to refcount a null pointer on Linux")
57+
}
58+
5459
do {
5560
_ = try URL("bad url", environment: jvm.environment)
5661
} catch {
57-
#expect(String(describing: error) == "no protocol: bad url")
62+
XCTAssert(String(describing: error) == "no protocol: bad url")
5863
}
5964
}
6065

61-
@Test("Static methods", .disabled(if: isMacOS, "Fails on macOS command line"))
62-
func staticMethods() throws {
66+
func testStaticMethods() throws {
67+
if isLinux {
68+
throw XCTSkip("Attempts to refcount a null pointer on Linux")
69+
}
70+
6371
let urlConnectionClass = try JavaClass<URLConnection>(in: jvm.environment)
64-
#expect(urlConnectionClass.getDefaultAllowUserInteraction() == false)
72+
XCTAssert(urlConnectionClass.getDefaultAllowUserInteraction() == false)
6573
}
6674

67-
@Test("Class instance lookup")
68-
func classInstanceLookup() throws {
75+
func testClassInstanceLookup() throws {
6976
do {
7077
_ = try JavaClass<Nonexistent>(in: jvm.environment)
7178
} catch {
72-
#expect(String(describing: error) == "org/swift/javakit/Nonexistent")
79+
XCTAssertEqual(String(describing: error), "org/swift/javakit/Nonexistent")
7380
}
7481
}
7582
}
@@ -85,12 +92,3 @@ var isLinux: Bool {
8592
return false
8693
#endif
8794
}
88-
89-
/// Whether we're running on MacOS.
90-
var isMacOS: Bool {
91-
#if os(macOS)
92-
return true
93-
#else
94-
return false
95-
#endif
96-
}

0 commit comments

Comments
 (0)