Skip to content

Commit

Permalink
Fix crash when using --config and --path with a Swift file
Browse files Browse the repository at this point in the history
Fixes #1694
  • Loading branch information
marcelofabri committed Jul 19, 2017
1 parent 8af2334 commit fa886df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
[Jamie Edge](https://github.com/JamieEdge)
[#1586](https://github.com/realm/SwiftLint/issues/1586)

* Fix crash when using `--config` with a relative path and
`--path` with a file.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1694](https://github.com/realm/SwiftLint/issues/1694)

## 0.20.1: More Liquid Fabric Softener

##### Breaking
Expand Down
13 changes: 12 additions & 1 deletion Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public struct Configuration: Equatable {
public init(path: String = Configuration.fileName, rootPath: String? = nil,
optional: Bool = true, quiet: Bool = false, enableAllRules: Bool = false, cachePath: String? = nil) {
let fullPath: String
if let rootPath = rootPath {
if let rootPath = rootPath, rootPath.isDirectory() {
fullPath = path.bridge().absolutePathRepresentation(rootDirectory: rootPath)
} else {
fullPath = path.bridge().absolutePathRepresentation()
Expand Down Expand Up @@ -201,3 +201,14 @@ private func containsDuplicateIdentifiers(_ identifiers: [String]) -> Bool {
}.joined(separator: "\n"))
return true
}

private extension String {
func isDirectory() -> Bool {
var isDir: ObjCBool = false
if FileManager.default.fileExists(atPath: self, isDirectory:&isDir) {
return isDir.boolValue
}

return false
}
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension ConfigurationTests {
static var allTests: [(String, (ConfigurationTests) -> () throws -> Void)] = [
("testInit", testInit),
("testEmptyConfiguration", testEmptyConfiguration),
("testInitWithRelativePathAndRootPath", testInitWithRelativePathAndRootPath),
("testWhitelistRules", testWhitelistRules),
("testWarningThreshold_value", testWarningThreshold_value),
("testWarningThreshold_nil", testWarningThreshold_nil),
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ class ConfigurationTests: XCTestCase {
XCTAssertEqual(reporterFrom(identifier: config.reporter).identifier, "xcode")
}

func testInitWithRelativePathAndRootPath() {
let previousWorkingDir = FileManager.default.currentDirectoryPath
let rootPath = projectMockSwift0
let expectedConfig = projectMockConfig0
FileManager.default.changeCurrentDirectoryPath(projectMockPathLevel0)

let config = Configuration(path: ".swiftlint.yml", rootPath: rootPath,
optional: false, quiet: true)

XCTAssertEqual(config.disabledRules, expectedConfig.disabledRules)
XCTAssertEqual(config.included, expectedConfig.included)
XCTAssertEqual(config.excluded, expectedConfig.excluded)
XCTAssertEqual(config.reporter, expectedConfig.reporter)

FileManager.default.changeCurrentDirectoryPath(previousWorkingDir)
}

func testWhitelistRules() {
let whitelist = ["nesting", "todo"]
let config = Configuration(dict: ["whitelist_rules": whitelist])!
Expand Down

0 comments on commit fa886df

Please sign in to comment.