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 finding the nested config when a single file path is passed #3379

Merged
merged 3 commits into from
Nov 8, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
[ZevEisenberg](https://github.com/ZevEisenberg)
[#3033](https://github.com/realm/SwiftLint/issues/3033)

* Fix finding a nested config when a single file path is passed.
[Seth Friedman](https://github.com/sethfri)

## 0.40.3: Greased Up Drum Bearings

#### Breaking
Expand Down
21 changes: 3 additions & 18 deletions Source/SwiftLintFramework/Extensions/Configuration+Merging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ extension Configuration {
}

private func configuration(forPath path: String) -> Configuration {
if path == rootDirectory {
let rootConfigurationDirectory = configurationPath?.bridge().deletingLastPathComponent
// We're linting a file in the same directory as the root configuration we've already loaded
if path == rootConfigurationDirectory {
return self
}

Expand Down Expand Up @@ -49,23 +51,6 @@ 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
}

if isDirectoryObjC.boolValue {
return rootPath
} else {
return rootPath.bridge().deletingLastPathComponent
}
}

private struct HashableRule: Hashable {
fileprivate let rule: Rule

Expand Down
10 changes: 8 additions & 2 deletions Source/swiftlint/Extensions/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ extension Configuration {

var groupedFiles = [Configuration: [SwiftLintFile]]()
for file in files {
// Files whose configuration specifies they should be excluded will be skipped
let fileConfiguration = configuration(for: file)
// If config was specified as a command line argument, always use it as an override. Otherwise, look for
// configs as normal, merging as necessary
let fileConfiguration = configurationSpecified() ? self : configuration(for: file)
let fileConfigurationRootPath = (fileConfiguration.rootPath ?? "").bridge()
// Files whose configuration specifies they should be excluded will be skipped
let shouldSkip = fileConfiguration.excluded.contains { excludedRelativePath in
let excludedPath = fileConfigurationRootPath.appendingPathComponent(excludedRelativePath)
let filePathComponents = file.path?.bridge().pathComponents ?? []
Expand Down Expand Up @@ -269,6 +271,10 @@ private func isConfigOptional() -> Bool {
return !CommandLine.arguments.contains("--config")
}

private func configurationSpecified() -> Bool {
return CommandLine.arguments.contains("--config")
}

private struct DuplicateCollector {
var all = Set<String>()
var duplicates = Set<String>()
Expand Down
3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.18.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 1.0.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

@testable import SwiftLintFrameworkTests
Expand Down Expand Up @@ -214,6 +214,7 @@ extension ConfigurationTests {
("testLevel2", testLevel2),
("testLevel3", testLevel3),
("testNestedConfigurationWithCustomRootPath", testNestedConfigurationWithCustomRootPath),
("testNestedConfigurationForOnePathPassedIn", testNestedConfigurationForOnePathPassedIn),
("testMergedWarningThreshold", testMergedWarningThreshold),
("testNestedOnlyRules", testNestedOnlyRules),
("testNestedConfigurationsWithCustomRulesMerge", testNestedConfigurationsWithCustomRulesMerge),
Expand Down
8 changes: 8 additions & 0 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests+Nested.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ extension ConfigurationTests {
XCTAssertEqual(projectMockConfig0.merge(with: projectMockConfig3).rootPath, projectMockConfig3.rootPath)
}

func testNestedConfigurationForOnePathPassedIn() {
let config = Configuration(path: projectMockYAML0, rootPath: projectMockSwift3)
XCTAssertEqual(
config.configuration(for: SwiftLintFile(path: projectMockSwift3)!),
config.merge(with: projectMockConfig3)
)
}

func testMergedWarningThreshold() {
func configuration(forWarningThreshold warningThreshold: Int?) -> Configuration {
return Configuration(warningThreshold: warningThreshold,
Expand Down