Skip to content

Commit 1562333

Browse files
authored
[Android] Enable more code and tests (#871) (#923)
* [Android] Enable more code and tests while disabling setting extended file attributes and a test creating a hard link, features not normally allowed on Android. * Remove incorrect WASI check
1 parent 64f9269 commit 1562333

File tree

10 files changed

+24
-13
lines changed

10 files changed

+24
-13
lines changed

Sources/FoundationEssentials/Data/Data+Reading.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import WASILibc
3434
func _fgetxattr(_ fd: Int32, _ name: UnsafePointer<CChar>!, _ value: UnsafeMutableRawPointer!, _ size: Int, _ position: UInt32, _ options: Int32) -> Int {
3535
#if canImport(Darwin)
3636
return fgetxattr(fd, name, value, size, position, options)
37-
#elseif canImport(Glibc) || canImport(Musl)
37+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
3838
return fgetxattr(fd, name, value, size)
3939
#else
4040
return -1
@@ -355,7 +355,7 @@ internal func readBytesFromFile(path inPath: PathOrURL, reportProgress: Bool, ma
355355
let localProgress = (reportProgress && Progress.current() != nil) ? Progress(totalUnitCount: Int64(fileSize)) : nil
356356

357357
if fileSize == 0 {
358-
#if os(Linux)
358+
#if os(Linux) || os(Android)
359359
// Linux has some files that may report a size of 0 but actually have contents
360360
let chunkSize = 1024 * 4
361361
var buffer = malloc(chunkSize)!

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ extension _FileManagerImpl {
955955
#if os(WASI)
956956
// WASI does not support extended attributes
957957
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
958+
#elseif canImport(Android)
959+
// Android doesn't allow setting this for normal apps, so just skip it.
958960
#else
959961
try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false)
960962
#endif

Sources/FoundationEssentials/Platform.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extension Platform {
192192
extension Platform {
193193
@discardableResult
194194
package static func copyCString(dst: UnsafeMutablePointer<CChar>, src: UnsafePointer<CChar>, size: Int) -> Int {
195-
#if canImport(Darwin)
195+
#if canImport(Darwin) || canImport(Android)
196196
return strlcpy(dst, src, size)
197197
#else
198198
// Glibc doesn't support strlcpy
@@ -267,7 +267,7 @@ extension Platform {
267267
return String(cString: buffer.baseAddress!).standardizingPath
268268
#endif
269269
}
270-
#elseif os(Linux)
270+
#elseif os(Linux) || os(Android)
271271
// For Linux, read /proc/self/exe
272272
return try? FileManager.default.destinationOfSymbolicLink(
273273
atPath: "/proc/self/exe").standardizingPath

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ extension _ProcessInfo {
437437
var siInfo = SYSTEM_INFO()
438438
GetSystemInfo(&siInfo)
439439
return Int(siInfo.dwNumberOfProcessors)
440-
#elseif os(Linux) || os(FreeBSD)
440+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
441441
return Int(sysconf(Int32(_SC_NPROCESSORS_CONF)))
442442
#else
443443
return 1
@@ -454,7 +454,7 @@ extension _ProcessInfo {
454454
return 0
455455
}
456456
return Int(count)
457-
#elseif os(Linux) || os(FreeBSD)
457+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
458458
#if os(Linux)
459459
if let fsCount = Self.fsCoreCount() {
460460
return fsCount
@@ -548,7 +548,7 @@ extension _ProcessInfo {
548548
return 0
549549
}
550550
return totalMemoryKB * 1024
551-
#elseif os(Linux) || os(FreeBSD)
551+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
552552
var memory = sysconf(Int32(_SC_PHYS_PAGES))
553553
memory *= sysconf(Int32(_SC_PAGESIZE))
554554
return UInt64(memory)

Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct TimeZoneCache : Sendable {
173173
}
174174
}
175175

176-
#if os(Linux) && !os(WASI)
176+
#if os(Linux)
177177
// Try localtime
178178
tzset()
179179
var t = time(nil)

Tests/FoundationEssentialsTests/DataIOTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class DataIOTests : XCTestCase {
238238
}
239239

240240
func test_zeroSizeFile() throws {
241-
#if !os(Linux)
241+
#if !os(Linux) && !os(Android)
242242
throw XCTSkip("This test is only applicable on Linux")
243243
#else
244244
// Some files in /proc report a file size of 0 bytes via a stat call

Tests/FoundationEssentialsTests/DataTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ extension DataTests {
18371837
}
18381838

18391839
func testEOPNOTSUPP() throws {
1840-
#if !canImport(Darwin) && !os(Linux)
1840+
#if !canImport(Darwin) && !os(Linux) && !os(Android)
18411841
throw XCTSkip("POSIXError.Code is not supported on this platform")
18421842
#else
18431843
// Opening a socket via open(2) on Darwin can result in the EOPNOTSUPP error code

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import TestSupport
2323
@testable import Foundation
2424
#endif
2525

26+
#if canImport(Android)
27+
import Android
28+
#endif
29+
2630
extension FileManager {
2731
fileprivate var delegateCaptures: DelegateCaptures {
2832
(self.delegate as! CapturingFileManagerDelegate).captures
@@ -329,8 +333,13 @@ final class FileManagerTests : XCTestCase {
329333
XCTAssertTrue($0.delegateCaptures.isEmpty)
330334
try $0.linkItem(atPath: "foo", toPath: "bar")
331335
XCTAssertEqual($0.delegateCaptures.shouldLink, [.init("foo", "bar")])
336+
#if os(Android) // Hard links are not normally allowed on Android.
337+
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterLinkError, [.init("foo", "bar", code: .fileWriteNoPermission)])
338+
XCTAssertFalse($0.fileExists(atPath: "bar"))
339+
#else
332340
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterLinkError, [])
333341
XCTAssertTrue($0.fileExists(atPath: "bar"))
342+
#endif
334343
}
335344

336345
try FileManagerPlayground {

Tests/FoundationEssentialsTests/PredicateTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ final class PredicateTests: XCTestCase {
364364
func testRegex_RegexBuilder() throws {
365365
#if !canImport(RegexBuilder)
366366
throw XCTSkip("RegexBuilder is unavavailable on this platform")
367-
#elseif !os(Linux) && !FOUNDATION_FRAMEWORK
367+
#elseif !os(Linux) && !os(Android) && !FOUNDATION_FRAMEWORK
368368
// Disable this test in swift-foundation macOS CI because of incorrect availability annotations in the StringProcessing module
369369
throw XCTSkip("This test is currently disabled on this platform")
370370
#else

Tests/FoundationEssentialsTests/ProcessInfoTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ final class ProcessInfoTests : XCTestCase {
115115
let expectedMinMajorVersion = 2
116116
#endif
117117
XCTAssertGreaterThanOrEqual(version.majorVersion, expectedMinMajorVersion, "Unrealistic major system version")
118-
#elseif os(Windows) || os(Linux)
118+
#elseif os(Windows) || os(Linux) || os(Android)
119119
let minVersion = OperatingSystemVersion(majorVersion: 1, minorVersion: 0, patchVersion: 0)
120120
XCTAssertTrue(ProcessInfo.processInfo.isOperatingSystemAtLeast(minVersion))
121121
#else
@@ -171,7 +171,7 @@ final class ProcessInfoTests : XCTestCase {
171171
func testProcessName() {
172172
#if FOUNDATION_FRAMEWORK
173173
let targetName = "TestHost"
174-
#elseif os(Linux) || os(Windows)
174+
#elseif os(Linux) || os(Windows) || os(Android)
175175
let targetName = "FoundationPreviewPackageTests.xctest"
176176
#else
177177
let targetName = "xctest"

0 commit comments

Comments
 (0)