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 configuration issue when using custom paths #1644

Merged
merged 1 commit into from
Jul 1, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1630](https://github.com/realm/SwiftLint/issues/1630)

* Use the directory's .swiftlint.yml when `--path` is used.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1631](https://github.com/realm/SwiftLint/issues/1631)

## 0.20.0: Liquid Fabric Softener

##### Breaking
Expand Down
15 changes: 7 additions & 8 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ 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 = path.bridge().absolutePathRepresentation()
let fullPath: String
if let rootPath = rootPath {
fullPath = path.bridge().absolutePathRepresentation(rootDirectory: rootPath)
} else {
fullPath = path.bridge().absolutePathRepresentation()
}

let fail = { (msg: String) in
queuedPrintError("\(fullPath):\(msg)")
fatalError("Could not read configuration file at path '\(fullPath)'")
Expand Down Expand Up @@ -186,13 +192,6 @@ public struct Configuration: Equatable {
setCached(atPath: fullPath)
}

public init(commandLinePath: String, rootPath: String? = nil, quiet: Bool = false, enableAllRules: Bool = false,
cachePath: String? = nil) {
self.init(path: commandLinePath, rootPath: rootPath?.absolutePathStandardized(),
optional: !CommandLine.arguments.contains("--config"), quiet: quiet, enableAllRules: enableAllRules,
cachePath: cachePath)
}

public func lintablePaths(inPath path: String, fileManager: LintableFileManager = FileManager.default) -> [String] {
// If path is a Swift file, skip filtering with excluded/included paths
if path.bridge().isSwiftFile() && path.isFile {
Expand Down
4 changes: 2 additions & 2 deletions Source/swiftlint/Commands/RulesCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct RulesCommand: CommandProtocol {
return .success()
}

let configuration = Configuration(commandLinePath: options.configurationFile)
let configuration = Configuration(options: options)
let rules = ruleList(for: options, configuration: configuration)

print(TextTable(ruleList: rules, configuration: configuration).render())
Expand Down Expand Up @@ -71,7 +71,7 @@ struct RulesCommand: CommandProtocol {

struct RulesOptions: OptionsProtocol {
fileprivate let ruleID: String?
fileprivate let configurationFile: String
let configurationFile: String
fileprivate let filterEnabled: Bool

static func create(_ configurationFile: String) -> (_ ruleID: String) -> (_ filterEnabled: Bool) -> RulesOptions {
Expand Down
16 changes: 13 additions & 3 deletions Source/swiftlint/Extensions/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ extension Configuration {

init(options: LintOptions) {
let cachePath = options.cachePath.isEmpty ? nil : options.cachePath
self.init(commandLinePath: options.configurationFile, rootPath: options.path, quiet: options.quiet,
let optional = !CommandLine.arguments.contains("--config")
self.init(path: options.configurationFile, rootPath: options.path.absolutePathStandardized(),
optional: optional, quiet: options.quiet,
enableAllRules: options.enableAllRules, cachePath: cachePath)
}

Expand All @@ -137,7 +139,15 @@ extension Configuration {

init(options: AutoCorrectOptions) {
let cachePath = options.cachePath.isEmpty ? nil : options.cachePath
self.init(commandLinePath: options.configurationFile, rootPath: options.path,
quiet: options.quiet, cachePath: cachePath)
let optional = !CommandLine.arguments.contains("--config")
self.init(path: options.configurationFile, rootPath: options.path.absolutePathStandardized(),
optional: optional, quiet: options.quiet, cachePath: cachePath)
}

// MARK: Rules command

init(options: RulesOptions) {
let optional = !CommandLine.arguments.contains("--config")
self.init(path: options.configurationFile, optional: optional)
}
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extension ConfigurationTests {
("testLevel1", testLevel1),
("testLevel2", testLevel2),
("testLevel3", testLevel3),
("testNestedConfigurationWithCustomRootPath", testNestedConfigurationWithCustomRootPath),
("testCustomConfiguration", testCustomConfiguration),
("testConfiguresCorrectlyFromDict", testConfiguresCorrectlyFromDict),
("testConfigureFallsBackCorrectly", testConfigureFallsBackCorrectly),
Expand Down
17 changes: 13 additions & 4 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ class ConfigurationTests: XCTestCase {
}

private var projectMockConfig0CustomPath: Configuration {
var configuration = Configuration(path: projectMockYAML0CustomPath, optional: false, quiet: true)
configuration.rootPath = projectMockPathLevel0
return configuration
return Configuration(path: projectMockYAML0CustomPath, rootPath: projectMockPathLevel0,
optional: false, quiet: true)
}

fileprivate var projectMockConfig2: Configuration {
return Configuration(path: projectMockYAML2, optional: false, quiet: true)
}

fileprivate var projectMockConfig3: Configuration {
return Configuration(path: Configuration.fileName, rootPath: projectMockPathLevel3,
optional: false, quiet: true)
}

func testIsEqualTo() {
XCTAssertEqual(projectMockConfig0, projectMockConfig0)
}
Expand Down Expand Up @@ -194,7 +198,12 @@ class ConfigurationTests: XCTestCase {

func testLevel3() {
XCTAssertEqual(projectMockConfig0.configuration(for: File(path: projectMockSwift3)!),
projectMockConfig0.merge(with: projectMockConfig2))
projectMockConfig0.merge(with: projectMockConfig3))
}

func testNestedConfigurationWithCustomRootPath() {
XCTAssertEqual(projectMockConfig3,
projectMockConfig0.merge(with: projectMockConfig3))
}

// MARK: - Testing Custom Configuration File
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
disabled_rules:
- force_try
- todo
included:
- "everything on level 3"
excluded:
- "the place where i committed many coding sins"
line_length: 10000000000
reporter: "json"