Skip to content

Commit 53ffff0

Browse files
authored
[Android] Enable more code and tests (#871)
* [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 ce79f53 commit 53ffff0

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
@@ -956,6 +956,8 @@ extension _FileManagerImpl {
956956
#if os(WASI)
957957
// WASI does not support extended attributes
958958
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
959+
#elseif canImport(Android)
960+
// Android doesn't allow setting this for normal apps, so just skip it.
959961
#else
960962
try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false)
961963
#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
@@ -450,7 +450,7 @@ extension _ProcessInfo {
450450
var siInfo = SYSTEM_INFO()
451451
GetSystemInfo(&siInfo)
452452
return Int(siInfo.dwNumberOfProcessors)
453-
#elseif os(Linux) || os(FreeBSD)
453+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
454454
return Int(sysconf(Int32(_SC_NPROCESSORS_CONF)))
455455
#else
456456
return 1
@@ -467,7 +467,7 @@ extension _ProcessInfo {
467467
return 0
468468
}
469469
return Int(count)
470-
#elseif os(Linux) || os(FreeBSD)
470+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
471471
#if os(Linux)
472472
if let fsCount = Self.fsCoreCount() {
473473
return fsCount
@@ -561,7 +561,7 @@ extension _ProcessInfo {
561561
return 0
562562
}
563563
return totalMemoryKB * 1024
564-
#elseif os(Linux) || os(FreeBSD)
564+
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
565565
var memory = sysconf(Int32(_SC_PHYS_PAGES))
566566
memory *= sysconf(Int32(_SC_PAGESIZE))
567567
return UInt64(memory)

Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ struct TimeZoneCache : Sendable, ~Copyable {
158158
}
159159
}
160160

161-
#if os(Linux) && !os(WASI)
161+
#if os(Linux)
162162
// Try localtime
163163
tzset()
164164
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
@@ -337,8 +341,13 @@ final class FileManagerTests : XCTestCase {
337341
XCTAssertTrue($0.delegateCaptures.isEmpty)
338342
try $0.linkItem(atPath: "foo", toPath: "bar")
339343
XCTAssertEqual($0.delegateCaptures.shouldLink, [.init("foo", "bar")])
344+
#if os(Android) // Hard links are not normally allowed on Android.
345+
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterLinkError, [.init("foo", "bar", code: .fileWriteNoPermission)])
346+
XCTAssertFalse($0.fileExists(atPath: "bar"))
347+
#else
340348
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterLinkError, [])
341349
XCTAssertTrue($0.fileExists(atPath: "bar"))
350+
#endif
342351
}
343352

344353
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 = "swift-foundationPackageTests.xctest"
176176
#else
177177
let targetName = "xctest"

0 commit comments

Comments
 (0)