Skip to content

Commit fdbf3d8

Browse files
committed
Merge branch 'pr/50'
2 parents 0b6479b + fd7d197 commit fdbf3d8

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ extern void _CFDataInit(CFMutableDataRef memory, CFOptionFlags flags, CFIndex ca
228228
extern int32_t _CF_SOCK_STREAM();
229229

230230
extern CFStringRef CFCopySystemVersionString(void);
231+
extern CFDictionaryRef _CFCopySystemVersionDictionary(void);
231232

232233
extern Boolean _CFCalendarInitWithIdentifier(CFCalendarRef calendar, CFStringRef identifier);
233234
extern Boolean _CFCalendarComposeAbsoluteTimeV(CFCalendarRef calendar, /* out */ CFAbsoluteTime *atp, const char *componentDesc, int32_t *vector, int32_t count);

Foundation/NSProcessInfo.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,20 @@ public class NSProcessInfo : NSObject {
9595

9696
public var operatingSystemVersion: NSOperatingSystemVersion {
9797
get {
98-
NSUnimplemented()
98+
let productVersionKey = unsafeBitCast(_kCFSystemVersionProductVersionKey, UnsafePointer<Void>.self)
99+
let productVersion = unsafeBitCast(CFDictionaryGetValue(_CFCopySystemVersionDictionary(), productVersionKey), NSString!.self)
100+
// The following fallback values match Darwin Foundation
101+
let fallbackMajor = -1
102+
let fallbackMinor = 0
103+
let fallbackPatch = 0
104+
if productVersion == nil {
105+
return NSOperatingSystemVersion(majorVersion: fallbackMajor, minorVersion: fallbackMinor, patchVersion: fallbackPatch)
106+
}
107+
let versionComponents = productVersion._swiftObject.characters.split(".").flatMap(String.init).flatMap({ Int($0) })
108+
let majorVersion = versionComponents.dropFirst(0).first ?? fallbackMajor
109+
let minorVersion = versionComponents.dropFirst(1).first ?? fallbackMinor
110+
let patchVersion = versionComponents.dropFirst(2).first ?? fallbackPatch
111+
return NSOperatingSystemVersion(majorVersion: majorVersion, minorVersion: minorVersion, patchVersion: patchVersion)
99112
}
100113
}
101114

TestFoundation/TestNSProcessInfo.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ class TestNSProcessInfo : XCTestCase {
2121

2222
var allTests : [(String, () -> ())] {
2323
return [
24+
("test_operatingSystemVersion", test_operatingSystemVersion ),
2425
("test_processName", test_processName ),
2526
]
2627
}
2728

29+
func test_operatingSystemVersion() {
30+
let processInfo = NSProcessInfo.processInfo()
31+
let versionString = processInfo.operatingSystemVersionString
32+
let version = processInfo.operatingSystemVersion
33+
let reconstructedVersionString = "Version \(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
34+
XCTAssert(versionString.hasPrefix(reconstructedVersionString))
35+
}
36+
2837
func test_processName() {
2938
// Assert that the original process name is "TestFoundation". This test
3039
// will fail if the test target ever gets renamed, so maybe it should

0 commit comments

Comments
 (0)