Skip to content

Commit

Permalink
Fix permissions set/get on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ggruen committed Jan 12, 2018
1 parent 47c2f11 commit 476be2f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Sources/SwiftNetrcCore/SwiftNetrc.swift
Expand Up @@ -19,6 +19,9 @@ public final class SwiftNetrc {

/// .netrc file isn't read-only
case fileGroupOrWorldWritableOrExecutable

/// Couldn't read file permissions
case unableToReadFilePermissions
}

/// The URL to the .netrc file. Defaults to ~/.netrc
Expand Down Expand Up @@ -70,7 +73,10 @@ public final class SwiftNetrc {
let attributes = try FileManager.default.attributesOfItem(atPath: netrcFile.path)
// .netrc must be read and/or write for user only, so 600 or 400 are ok, nothing else.
let okPermissions: Int16 = 0o600
guard (attributes[.posixPermissions] as! Int16 | okPermissions) == okPermissions else {
guard let filePerms = attributes[.posixPermissions] as? NSNumber else {
throw SwiftNetrcError.unableToReadFilePermissions
}
guard (filePerms.int16Value | okPermissions) == okPermissions else {
throw SwiftNetrcError.fileGroupOrWorldWritableOrExecutable
}
let netrc = try String(contentsOf: netrcFile, encoding: .utf8)
Expand Down
6 changes: 6 additions & 0 deletions Tests/LinuxMain.swift
@@ -0,0 +1,6 @@
import XCTest
@testable import SwiftNetrcTests

XCTMain([
testCase(SwiftNetrcTests.allTests)
])
15 changes: 14 additions & 1 deletion Tests/SwiftNetrcTests/SwiftNetrcTests.swift
Expand Up @@ -9,6 +9,19 @@ import XCTest

class SwiftNetrcTests: XCTestCase {

/// For Linux
static var allTests = [
("testRetrievesUsernameAndPassword", testRetrievesUsernameAndPassword),
("testNetrcSetsFilePathOnInit", testNetrcSetsFilePathOnInit ),
("testHandlesTwoMachines", testHandlesTwoMachines ),
("testHandlesPassphrases", testHandlesPassphrases ),
("testThrowsNoMachineSpecified", testThrowsNoMachineSpecified ),
("testThrowsNoValueForToken", testThrowsNoValueForToken ),
("testThrowsOnUnsafeFile", testThrowsOnUnsafeFile ),
("testDoesntThrowWithGoodPermissions", testDoesntThrowWithGoodPermissions ),
("testPerformanceExample", testPerformanceExample ),
]

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of
Expand Down Expand Up @@ -219,7 +232,7 @@ class SwiftNetrcTests: XCTestCase {
/// - Returns: `( callback, pathToFile )`, a tuple in which `callback` is a function to call that will remove
/// the test file and `pathToFile` is the path to the created file
/// - Throws: Errors from `String` or `FileManager`
func writeNetrc(with content: String, to path: String = "/tmp/testnetrc", withPermissions perms: Int16 = 0o600) throws -> (() -> (), String) {
func writeNetrc(with content: String, to path: String = "/tmp/testnetrc", withPermissions perms: NSNumber = 0o600) throws -> (() -> (), String) {
try content.write(toFile: path, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: perms], ofItemAtPath: path)
return ( { try? FileManager.default.removeItem(atPath: path)}, path )
Expand Down
17 changes: 17 additions & 0 deletions sourcery/LinuxMain.stencil
@@ -0,0 +1,17 @@
// sourcery:file:Tests/LinuxMain.swift
import XCTest
{{ argument.testimports }}

{% for type in types.classes|based:"XCTestCase" %}
{% if not type.annotations.disableTests %}extension {{ type.name }} {
static var allTests = [
{% for method in type.methods %}{% if method.parameters.count == 0 and method.shortName|hasPrefix:"test" %} ("{{ method.shortName }}", {{ method.shortName }}),
{% endif %}{% endfor %}]
}

{% endif %}{% endfor %}

XCTMain([
{% for type in types.classes|based:"XCTestCase" %}{% if not type.annotations.disableTests %} testCase({{ type.name }}.allTests),
{% endif %}{% endfor %}])
// sourcery:end

0 comments on commit 476be2f

Please sign in to comment.