Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix picking the wrong configuration with —path #1745

Merged
merged 4 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
[Hossam Ghareeb](https://github.com/hossamghareeb)
[#1763](https://github.com/realm/SwiftLint/issues/1763)

* Fix using wrong configuration when using `--path` and when there is
a configuration in a parent directory.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1744](https://github.com/realm/SwiftLint/issues/1744)

## 0.21.0: Vintage Washboard

##### Breaking
Expand Down
26 changes: 25 additions & 1 deletion Source/SwiftLintFramework/Extensions/Configuration+Merging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Configuration {
}

private func configuration(forPath path: String) -> Configuration {
if path == rootPath {
if path == rootDirectory {
return self
}

Expand All @@ -43,6 +43,30 @@ extension Configuration {
return self
}

private var rootDirectory: String? {
guard let rootPath = rootPath else {
return nil
}

var isDirectoryObjC: ObjCBool = false
guard FileManager.default.fileExists(atPath: rootPath, isDirectory: &isDirectoryObjC) else {
return nil
}

let isDirectory: Bool
#if os(Linux)
isDirectory = isDirectoryObjC
#else
isDirectory = isDirectoryObjC.boolValue
#endif

if isDirectory {
return rootPath
} else {
return rootPath.bridge().deletingLastPathComponent
}
}

private struct HashableRule: Hashable {
fileprivate let rule: Rule

Expand Down
12 changes: 6 additions & 6 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public struct Configuration: Equatable {

public static let fileName = ".swiftlint.yml"

public let included: [String] // included
public let excluded: [String] // excluded
public let reporter: String // reporter (xcode, json, csv, checkstyle)
public var warningThreshold: Int? // warning threshold
public var rootPath: String? // the root path to search for nested configurations
public var configurationPath: String? // if successfully loaded from a path
public let included: [String] // included
public let excluded: [String] // excluded
public let reporter: String // reporter (xcode, json, csv, checkstyle)
public let warningThreshold: Int? // warning threshold
public private(set) var rootPath: String? // the root path to search for nested configurations
public private(set) var configurationPath: String? // if successfully loaded from a path
public let cachePath: String?

// MARK: Rules Properties
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public let masterRuleList = RuleList(rules: [
PrivateUnitTestRule.self,
ProhibitedSuperRule.self,
ProtocolPropertyAccessorsOrderRule.self,
SingleTestClassRule.self,
RedundantDiscardableLetRule.self,
RedundantNilCoalescingRule.self,
RedundantOptionalInitializationRule.self,
RedundantStringEnumValueRule.self,
RedundantVoidReturnRule.self,
ReturnArrowWhitespaceRule.self,
ShorthandOperatorRule.self,
SingleTestClassRule.self,
SortedImportsRule.self,
StatementPositionRule.self,
StrictFilePrivateRule.self,
Expand Down
9 changes: 7 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extension ConfigurationTests {
("testIsEqualTo", testIsEqualTo),
("testIsNotEqualTo", testIsNotEqualTo),
("testCustomConfiguration", testCustomConfiguration),
("testConfigurationWithSwiftFileAsRoot", testConfigurationWithSwiftFileAsRoot),
("testConfigurationWithSwiftFileAsRootAndCustomConfiguration", testConfigurationWithSwiftFileAsRootAndCustomConfiguration),
("testConfiguresCorrectlyFromDict", testConfiguresCorrectlyFromDict),
("testConfigureFallsBackCorrectly", testConfigureFallsBackCorrectly),
("testConfiguresCorrectlyFromDeprecatedAlias", testConfiguresCorrectlyFromDeprecatedAlias),
Expand Down Expand Up @@ -388,14 +390,14 @@ extension RulesTests {
("testPrivateUnitTest", testPrivateUnitTest),
("testProhibitedSuper", testProhibitedSuper),
("testProtocolPropertyAccessorsOrder", testProtocolPropertyAccessorsOrder),
("testSingleTestClass", testSingleTestClass),
("testRedundantDiscardableLet", testRedundantDiscardableLet),
("testRedundantNilCoalescing", testRedundantNilCoalescing),
("testRedundantOptionalInitialization", testRedundantOptionalInitialization),
("testRedundantStringEnumValue", testRedundantStringEnumValue),
("testRedundantVoidReturn", testRedundantVoidReturn),
("testReturnArrowWhitespace", testReturnArrowWhitespace),
("testShorthandOperator", testShorthandOperator),
("testSingleTestClass", testSingleTestClass),
("testSortedImports", testSortedImports),
("testStatementPosition", testStatementPosition),
("testStatementPositionUncuddled", testStatementPositionUncuddled),
Expand Down Expand Up @@ -461,7 +463,10 @@ extension UnusedOptionalBindingRuleTests {
extension VerticalWhitespaceRuleTests {
static var allTests: [(String, (VerticalWhitespaceRuleTests) -> () throws -> Void)] = [
("testVerticalWhitespaceWithDefaultConfiguration", testVerticalWhitespaceWithDefaultConfiguration),
("testAttributesWithMaxEmptyLines", testAttributesWithMaxEmptyLines)
("testAttributesWithMaxEmptyLines", testAttributesWithMaxEmptyLines),
("testAutoCorrectionWithMaxEmptyLines", testAutoCorrectionWithMaxEmptyLines),
("testViolationMessageWithMaxEmptyLines", testViolationMessageWithMaxEmptyLines),
("testViolationMessageWithDefaultConfiguration", testViolationMessageWithDefaultConfiguration)
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ extension ConfigurationTests {
}

var projectMockConfig0: Configuration {
var configuration = Configuration(path: projectMockYAML0, optional: false, quiet: true)
configuration.rootPath = projectMockPathLevel0
return configuration
return Configuration(path: projectMockYAML0, rootPath: projectMockPathLevel0,
optional: false, quiet: true)
}

var projectMockConfig0CustomPath: Configuration {
Expand Down
16 changes: 16 additions & 0 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ class ConfigurationTests: XCTestCase {
projectMockConfig0CustomPath.configuration(for: file))
}

func testConfigurationWithSwiftFileAsRoot() {
let configuration = Configuration(path: projectMockYAML0,
rootPath: projectMockSwift0,
optional: false, quiet: true)
let file = File(path: projectMockSwift0)!
XCTAssertEqual(configuration.configuration(for: file), configuration)
}

func testConfigurationWithSwiftFileAsRootAndCustomConfiguration() {
let configuration = Configuration(path: projectMockYAML0CustomPath,
rootPath: projectMockSwift0,
optional: false, quiet: true)
let file = File(path: projectMockSwift0)!
XCTAssertEqual(configuration.configuration(for: file), configuration)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test should pass, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcelofabri,

For starters, Configuration’s Equatable conformance is broken on master, so XCTAssertEqual is unreliable here. (See the comments in #1748 starting with this one). Although that may not be the only issue since there are also failures on macOS.

Copy link
Collaborator Author

@marcelofabri marcelofabri Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens locally on my Mac. I'm pretty sure it's not a bug of comparison, but a consequence of the bug that this PR fixes. However, this PR currently only fixes it for implicit configs.

}

// MARK: - Testing Rules from config dictionary

let testRuleList = RuleList(rules: RuleWithLevelsMock.self)
Expand Down