From 87922c8aa46c6a8454601d6b89c12f18639e4800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Wed, 7 Mar 2018 00:26:29 +0100 Subject: [PATCH 1/4] Added URL API to Foundation.Process --- Foundation/Process.swift | 28 +++++++++++++++++++++++++++- TestFoundation/TestProcess.swift | 10 +++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Foundation/Process.swift b/Foundation/Process.swift index 849bfd90d1..6fd3746455 100644 --- a/Foundation/Process.swift +++ b/Foundation/Process.swift @@ -164,14 +164,40 @@ open class Process: NSObject { } - // these methods can only be set before a launch + + // These methods can only be set before a launch. + open var launchPath: String? open var arguments: [String]? open var environment: [String : String]? // if not set, use current open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath + open var executableURL: URL? { + get { + guard let launchPath = self.launchPath else { + return nil + } + + return URL(fileURLWithPath: launchPath) + } + set { + self.launchPath = newValue?.path + } + } + + open var currentDirectoryURL: URL { + get { + return URL(fileURLWithPath: self.currentDirectoryPath) + } + set { + self.currentDirectoryPath = newValue.path + } + } + + // standard I/O channels; could be either a FileHandle or a Pipe + open var standardInput: Any? { willSet { precondition(newValue is Pipe || newValue is FileHandle, diff --git a/TestFoundation/TestProcess.swift b/TestFoundation/TestProcess.swift index 8ebc3cef00..60a1af867a 100644 --- a/TestFoundation/TestProcess.swift +++ b/TestFoundation/TestProcess.swift @@ -45,7 +45,11 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + let executablePath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: executablePath) + + XCTAssertEqual(executablePath, process.launchPath) + process.arguments = ["-c", "exit 0"] process.launch() @@ -53,7 +57,7 @@ class TestProcess : XCTestCase { XCTAssertEqual(process.terminationStatus, 0) XCTAssertEqual(process.terminationReason, .exit) } - + func test_exit1() { let process = Process() @@ -277,7 +281,7 @@ class TestProcess : XCTestCase { func test_current_working_directory() { do { let previousWorkingDirectory = FileManager.default.currentDirectoryPath - + // Darwin Foundation requires the full path to the executable (.launchPath) let (output, _) = try runTask(["/bin/bash", "-c", "pwd"], currentDirectoryPath: "/bin") XCTAssertEqual(output.trimmingCharacters(in: .newlines), "/bin") From a984e1143698c1c62642e750081cb64b408f381b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Sun, 11 Mar 2018 18:05:22 +0100 Subject: [PATCH 2/4] Fixed Locale CF/Swift conversions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CFArray._nsObject already contains String elements so bridging them to NSString crashes on Linux. Also, enabled option to retrieve “AppleLanguages” list from preferences plist in Linux. --- CoreFoundation/Locale.subproj/CFLocale.c | 2 +- Foundation/NSLocale.swift | 42 ++++++++++++------------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CoreFoundation/Locale.subproj/CFLocale.c b/CoreFoundation/Locale.subproj/CFLocale.c index e573fd45b6..4a02e6bbd4 100644 --- a/CoreFoundation/Locale.subproj/CFLocale.c +++ b/CoreFoundation/Locale.subproj/CFLocale.c @@ -1100,7 +1100,7 @@ static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesA #endif CFArrayRef CFLocaleCopyPreferredLanguages(void) { -#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX CFArrayRef languagesArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication); CFArrayRef result = _CFLocaleCopyPreferredLanguagesFromPrefs(languagesArray); if (languagesArray) CFRelease(languagesArray); diff --git a/Foundation/NSLocale.swift b/Foundation/NSLocale.swift index f6ba25a9e3..2dcde893ee 100644 --- a/Foundation/NSLocale.swift +++ b/Foundation/NSLocale.swift @@ -84,11 +84,11 @@ open class NSLocale: NSObject, NSCopying, NSSecureCoding { } extension NSLocale { - open class var current: Locale { + open static var current: Locale { return CFLocaleCopyCurrent()._swiftObject } - open class var system: Locale { + open static var system: Locale { return CFLocaleGetSystem()._swiftObject } } @@ -98,7 +98,7 @@ extension NSLocale { return object(forKey: .identifier) as! String } - open class var availableLocaleIdentifiers: [String] { + open static var availableLocaleIdentifiers: [String] { var identifiers = Array() for obj in CFLocaleCopyAvailableLocaleIdentifiers()._nsObject { identifiers.append(obj as! String) @@ -106,47 +106,47 @@ extension NSLocale { return identifiers } - open class var isoLanguageCodes: [String] { + open static var isoLanguageCodes: [String] { var identifiers = Array() for obj in CFLocaleCopyISOLanguageCodes()._nsObject { - identifiers.append((obj as! NSString)._swiftObject) + identifiers.append(obj as! String) } return identifiers } - open class var isoCountryCodes: [String] { + open static var isoCountryCodes: [String] { var identifiers = Array() for obj in CFLocaleCopyISOCountryCodes()._nsObject { - identifiers.append((obj as! NSString)._swiftObject) + identifiers.append(obj as! String) } return identifiers } - open class var isoCurrencyCodes: [String] { + open static var isoCurrencyCodes: [String] { var identifiers = Array() for obj in CFLocaleCopyISOCurrencyCodes()._nsObject { - identifiers.append((obj as! NSString)._swiftObject) + identifiers.append(obj as! String) } return identifiers } - open class var commonISOCurrencyCodes: [String] { + open static var commonISOCurrencyCodes: [String] { var identifiers = Array() for obj in CFLocaleCopyCommonISOCurrencyCodes()._nsObject { - identifiers.append((obj as! NSString)._swiftObject) + identifiers.append(obj as! String) } return identifiers } - open class var preferredLanguages: [String] { + open static var preferredLanguages: [String] { var identifiers = Array() for obj in CFLocaleCopyPreferredLanguages()._nsObject { - identifiers.append((obj as! NSString)._swiftObject) + identifiers.append(obj as! String) } return identifiers } - open class func components(fromLocaleIdentifier string: String) -> [String : String] { + open static func components(fromLocaleIdentifier string: String) -> [String : String] { var comps = Dictionary() let values = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)._nsObject values.enumerateKeysAndObjects(options: []) { (k, v, stop) in @@ -157,27 +157,27 @@ extension NSLocale { return comps } - open class func localeIdentifier(fromComponents dict: [String : String]) -> String { + open static func localeIdentifier(fromComponents dict: [String : String]) -> String { return CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, dict._cfObject)._swiftObject } - open class func canonicalLocaleIdentifier(from string: String) -> String { + open static func canonicalLocaleIdentifier(from string: String) -> String { return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject } - open class func canonicalLanguageIdentifier(from string: String) -> String { + open static func canonicalLanguageIdentifier(from string: String) -> String { return CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject } - open class func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? { + open static func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? { return CFLocaleCreateLocaleIdentifierFromWindowsLocaleCode(kCFAllocatorSystemDefault, lcid)._swiftObject } - open class func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 { + open static func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 { return CFLocaleGetWindowsLocaleCodeFromLocaleIdentifier(localeIdentifier._cfObject) } - open class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection { + open static func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection { let dir = CFLocaleGetLanguageCharacterDirection(isoLangCode._cfObject) #if os(OSX) || os(iOS) return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))! @@ -186,7 +186,7 @@ extension NSLocale { #endif } - open class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection { + open static func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection { let dir = CFLocaleGetLanguageLineDirection(isoLangCode._cfObject) #if os(OSX) || os(iOS) return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))! From ac9f7af0eae73b3da0ed2208a869501d71cd3c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Sun, 11 Mar 2018 18:22:15 +0100 Subject: [PATCH 3/4] Extended Locale tests --- TestFoundation/TestNSLocale.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/TestFoundation/TestNSLocale.swift b/TestFoundation/TestNSLocale.swift index 11398ef416..09e803d639 100644 --- a/TestFoundation/TestNSLocale.swift +++ b/TestFoundation/TestNSLocale.swift @@ -19,14 +19,14 @@ class TestNSLocale : XCTestCase { static var allTests: [(String, (TestNSLocale) -> () throws -> Void)] { return [ ("test_constants", test_constants), - ("test_Identifier", test_Identifier), + ("test_identifier", test_identifier), ("test_copy", test_copy), - ("test_availableIdentifiers", test_availableIdentifiers), + ("test_staticProperties", test_staticProperties), ("test_localeProperties", test_localeProperties), ] } - func test_Identifier() { + func test_identifier() { // Current locale identifier should not be empty // Or things like NumberFormatter spellOut style won't work XCTAssertFalse(Locale.current.identifier.isEmpty) @@ -110,8 +110,13 @@ class TestNSLocale : XCTestCase { XCTAssertTrue(locale == localeCopy) } - func test_availableIdentifiers() { + func test_staticProperties() { XCTAssertNoThrow(Locale.availableIdentifiers) + XCTAssertNoThrow(Locale.preferredLanguages) + XCTAssertNoThrow(Locale.commonISOCurrencyCodes) + XCTAssertNoThrow(Locale.isoCurrencyCodes) + XCTAssertNoThrow(Locale.isoRegionCodes) + XCTAssertNoThrow(Locale.isoLanguageCodes) } func test_localeProperties(){ From 57e88846901e911fec36e1c8cb52430a85df8333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Mon, 12 Mar 2018 21:05:55 +0100 Subject: [PATCH 4/4] Added _CFLocaleCopyPreferredLanguagesFromPrefs to Linux --- CoreFoundation/Locale.subproj/CFLocale.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoreFoundation/Locale.subproj/CFLocale.c b/CoreFoundation/Locale.subproj/CFLocale.c index 4a02e6bbd4..10013909c9 100644 --- a/CoreFoundation/Locale.subproj/CFLocale.c +++ b/CoreFoundation/Locale.subproj/CFLocale.c @@ -1080,7 +1080,7 @@ _CFLocaleCalendarDirection _CFLocaleGetCalendarDirection(void) { #endif } -#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray) { CFMutableArrayRef newArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks); if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {