Skip to content

Commit

Permalink
Revert "Fix issue that prevented the inclusion of a configuration fil…
Browse files Browse the repository at this point in the history
…e from a parent folder (#3491)"

This reverts commit 2f6fd1e.
  • Loading branch information
jpsim committed Feb 25, 2021
1 parent 8326624 commit b66f7c7
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 77 deletions.
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@

* Fix typos in configuration options for `file_name` rule.
[advantis](https://github.com/advantis)

* Fix issue that prevented the inclusion of a configuration file from a parent folder.
[Frederick Pietschmann](https://github.com/fredpi)
[#3485](https://github.com/realm/SwiftLint/issues/3485)

* Fix violation location and misplaced corrections for some function
references in `explicit_self` rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,7 @@ internal extension Configuration {
dict: firstConfigurationData.configurationDict,
enableAllRules: enableAllRules
)

// Set the config's rootDirectory to rootDirectory (+ adjust included / excluded paths that relate to it).
// firstConfigurationData.rootDirectory may be different from rootDirectory,
// e. g. when ../file.yml is passed as the first config
firstConfiguration.fileGraph = FileGraph(rootDirectory: rootDirectory)
firstConfiguration.makeIncludedAndExcludedPaths(
relativeTo: rootDirectory,
previousBasePath: firstConfigurationData.rootDirectory
)
firstConfiguration.fileGraph = FileGraph(rootDirectory: firstConfigurationData.rootDirectory)

// Build succeeding configurations
return try configurationData.reduce(firstConfiguration) {
Expand Down
25 changes: 6 additions & 19 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public struct Configuration {

// MARK: Public Instance
/// The paths that should be included when linting
public private(set) var includedPaths: [String]
public let includedPaths: [String]

/// The paths that should be excluded when linting
public private(set) var excludedPaths: [String]
public let excludedPaths: [String]

/// The style to use when indenting Swift source code.
public let indentation: IndentationStyle
Expand Down Expand Up @@ -46,8 +46,7 @@ public struct Configuration {

/// The root directory is the directory that included & excluded paths relate to.
/// By default, the root directory is the current working directory,
/// but during some merging algorithms it may be used differently.
/// The rootDirectory also serves as the stopping point when searching for nested configs along the file hierarchy.
/// but in some merging algorithms it is used differently.
public var rootDirectory: String { fileGraph.rootDirectory }

/// The rules mode used for this configuration.
Expand Down Expand Up @@ -191,11 +190,11 @@ public struct Configuration {
let configurationFiles = configurationFiles.isEmpty ? [Configuration.defaultFileName] : configurationFiles
defer { basedOnCustomConfigurationFiles = hasCustomConfigurationFiles }

let currentWorkingDirectory = FileManager.default.currentDirectoryPath.bridge().absolutePathStandardized()
let rootDirectory = FileManager.default.currentDirectoryPath.bridge().absolutePathStandardized()
let rulesMode: RulesMode = enableAllRules ? .allEnabled : .default(disabled: [], optIn: [])

// Try obtaining cached config
let cacheIdentifier = "\(currentWorkingDirectory) - \(configurationFiles)"
let cacheIdentifier = "\(rootDirectory) - \(configurationFiles)"
if let cachedConfig = Configuration.getCached(forIdentifier: cacheIdentifier) {
self.init(copying: cachedConfig)
return
Expand All @@ -205,7 +204,7 @@ public struct Configuration {
do {
var fileGraph = FileGraph(
commandLineChildConfigs: configurationFiles,
rootDirectory: currentWorkingDirectory,
rootDirectory: rootDirectory,
ignoreParentAndChildConfigs: ignoreParentAndChildConfigs
)
let resultingConfiguration = try fileGraph.resultingConfiguration(enableAllRules: enableAllRules)
Expand Down Expand Up @@ -238,20 +237,8 @@ public struct Configuration {
}
}
}

// MARK: - Methods: Internal
mutating func makeIncludedAndExcludedPaths(relativeTo newBasePath: String, previousBasePath: String) {
includedPaths = includedPaths.map {
$0.bridge().absolutePathRepresentation(rootDirectory: previousBasePath).path(relativeTo: newBasePath)
}

excludedPaths = excludedPaths.map {
$0.bridge().absolutePathRepresentation(rootDirectory: previousBasePath).path(relativeTo: newBasePath)
}
}
}

// MARK: - FileGraphInitializationResult
private enum FileGraphInitializationResult {
case initialImplicitFileNotFound
case error(message: String)
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal extension ConfigurationTests {
// MARK: YAML File Paths
enum Yml {
static var _0: String { Dir.level0.stringByAppendingPathComponent(Configuration.defaultFileName) }
static var _0Custom: String { Dir.level0.stringByAppendingPathComponent("custom.yml") }
static var _0CustomPath: String { Dir.level0.stringByAppendingPathComponent("custom.yml") }
static var _0CustomRules: String { Dir.level0.stringByAppendingPathComponent("custom_rules.yml") }
static var _2: String { Dir.level2.stringByAppendingPathComponent(Configuration.defaultFileName) }
static var _2CustomRules: String { Dir.level2.stringByAppendingPathComponent("custom_rules.yml") }
Expand All @@ -59,7 +59,7 @@ internal extension ConfigurationTests {
// MARK: Configurations
enum Config {
static var _0: Configuration { Configuration(configurationFiles: []) }
static var _0Custom: Configuration { Configuration(configurationFiles: [Yml._0Custom]) }
static var _0CustomPath: Configuration { Configuration(configurationFiles: [Yml._0CustomPath]) }
static var _0CustomRules: Configuration { Configuration(configurationFiles: [Yml._0CustomRules]) }
static var _2: Configuration { Configuration(configurationFiles: [Yml._2]) }
static var _2CustomRules: Configuration { Configuration(configurationFiles: [Yml._2CustomRules]) }
Expand Down
40 changes: 2 additions & 38 deletions Tests/SwiftLintFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import XCTest

private let optInRules = primaryRuleList.list.filter({ $0.1.init() is OptInRule }).map({ $0.0 })

// swiftlint:disable file_length type_body_length

class ConfigurationTests: XCTestCase {
// MARK: Setup & Teardown
private var previousWorkingDir: String!
Expand Down Expand Up @@ -170,40 +168,6 @@ class ConfigurationTests: XCTestCase {
)
}

func testIncludedExcludedRelativeLocationLevel1() {
FileManager.default.changeCurrentDirectoryPath(Mock.Dir.level1)

// The included path "File.swift" should be put relative to the configuration file
// (~> Resources/ProjectMock/File.swift) and not relative to the path where
// SwiftLint is run from (~> Resources/ProjectMock/Level1/File.swift)
let configuration = Configuration(configurationFiles: ["../custom_included_excluded.yml"])
let actualIncludedPath = configuration.includedPaths.first!.bridge()
.absolutePathRepresentation(rootDirectory: configuration.rootDirectory)
let desiredIncludedPath = "File1.swift".absolutePathRepresentation(rootDirectory: Mock.Dir.level0)
let actualExcludedPath = configuration.excludedPaths.first!.bridge()
.absolutePathRepresentation(rootDirectory: configuration.rootDirectory)
let desiredExcludedPath = "File2.swift".absolutePathRepresentation(rootDirectory: Mock.Dir.level0)

XCTAssertEqual(actualIncludedPath, desiredIncludedPath)
XCTAssertEqual(actualExcludedPath, desiredExcludedPath)
}

func testIncludedExcludedRelativeLocationLevel0() {
// Same as testIncludedPathRelatedToConfigurationFileLocationLevel1(),
// but run from the directory the config file resides in
FileManager.default.changeCurrentDirectoryPath(Mock.Dir.level0)
let configuration = Configuration(configurationFiles: ["custom_included_excluded.yml"])
let actualIncludedPath = configuration.includedPaths.first!.bridge()
.absolutePathRepresentation(rootDirectory: configuration.rootDirectory)
let desiredIncludedPath = "File1.swift".absolutePathRepresentation(rootDirectory: Mock.Dir.level0)
let actualExcludedPath = configuration.excludedPaths.first!.bridge()
.absolutePathRepresentation(rootDirectory: configuration.rootDirectory)
let desiredExcludedPath = "File2.swift".absolutePathRepresentation(rootDirectory: Mock.Dir.level0)

XCTAssertEqual(actualIncludedPath, desiredIncludedPath)
XCTAssertEqual(actualExcludedPath, desiredExcludedPath)
}

private class TestFileManager: LintableFileManager {
func filesToLint(inPath path: String, rootDirectory: String? = nil) -> [String] {
switch path {
Expand Down Expand Up @@ -295,7 +259,7 @@ class ConfigurationTests: XCTestCase {
func testCustomConfiguration() {
let file = SwiftLintFile(path: Mock.Swift._0)!
XCTAssertNotEqual(Mock.Config._0.configuration(for: file),
Mock.Config._0Custom.configuration(for: file))
Mock.Config._0CustomPath.configuration(for: file))
}

func testConfigurationWithSwiftFileAsRoot() {
Expand All @@ -306,7 +270,7 @@ class ConfigurationTests: XCTestCase {
}

func testConfigurationWithSwiftFileAsRootAndCustomConfiguration() {
let configuration = Mock.Config._0Custom
let configuration = Configuration(configurationFiles: [Mock.Yml._0CustomPath])

let file = SwiftLintFile(path: Mock.Swift._0)!
XCTAssertEqual(configuration.configuration(for: file), configuration)
Expand Down

This file was deleted.

0 comments on commit b66f7c7

Please sign in to comment.