From 5bd9ac77fca185945b350647a86084dc22a8f898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sun, 5 Sep 2021 20:33:28 +0200 Subject: [PATCH] Re-enable uncommented test code for Checkers --- Sources/TestSupport/TestLogger.swift | 6 + .../FileContentsCheckerTests.swift | 311 +++++++++--------- .../CheckersTests/FilePathsCheckerTests.swift | 129 ++++---- Tests/CheckersTests/FilesSearchTests.swift | 112 +++---- Tests/CheckersTests/LintTests.swift | 242 +++++++------- 5 files changed, 393 insertions(+), 407 deletions(-) diff --git a/Sources/TestSupport/TestLogger.swift b/Sources/TestSupport/TestLogger.swift index 31cd545..1d12b57 100644 --- a/Sources/TestSupport/TestLogger.swift +++ b/Sources/TestSupport/TestLogger.swift @@ -3,6 +3,7 @@ import Core public final class TestLogger: Loggable { public var loggedMessages: [String] + public var exitStatusCode: Int32? public init() { loggedMessages = [] @@ -20,4 +21,9 @@ public final class TestLogger: Loggable { ) } } + + public func exit(fail: Bool) -> Never { + exitStatusCode = fail ? EXIT_FAILURE : EXIT_SUCCESS + fatalError() + } } diff --git a/Tests/CheckersTests/FileContentsCheckerTests.swift b/Tests/CheckersTests/FileContentsCheckerTests.swift index 9de12f1..592cf9d 100644 --- a/Tests/CheckersTests/FileContentsCheckerTests.swift +++ b/Tests/CheckersTests/FileContentsCheckerTests.swift @@ -72,164 +72,155 @@ final class FileContentsCheckerTests: XCTestCase { } } - // func testSkipHere() { - // let temporaryFiles: [TemporaryFile] = [ - // (subpath: "Sources/Hello.swift", contents: "// AnyLint.skipHere: OtherRule, Whitespacing\n\n\nlet x=5\nvar y=10"), - // (subpath: "Sources/World.swift", contents: "\n\n// AnyLint.skipHere: OtherRule, Whitespacing\nlet x=5\nvar y=10"), - // ( - // subpath: "Sources/Foo.swift", contents: "\n\n\nlet x=5\nvar y=10 // AnyLint.skipHere: OtherRule, Whitespacing\n" - // ), - // (subpath: "Sources/Bar.swift", contents: "\n\n\nlet x=5\nvar y=10\n// AnyLint.skipHere: OtherRule, Whitespacing"), - // ] - // - // withTemporaryFiles(temporaryFiles) { filePathsToCheck in - // let checkInfo = CheckInfo( - // id: "Whitespacing", - // hint: "Always add a single whitespace around '='.", - // severity: .warning - // ) - // let violations = try FileContentsChecker( - // checkInfo: checkInfo, - // regex: #"(let|var) \w+=\w+"#, - // filePathsToCheck: filePathsToCheck, - // autoCorrectReplacement: nil, - // repeatIfAutoCorrected: false - // ) - // .performCheck() - // - // XCTAssertEqual(violations.count, 6) - // - // XCTAssertEqual(violations[0].checkInfo, checkInfo) - // XCTAssertEqual(violations[0].filePath, "\(tempDir)/Sources/Hello.swift") - // XCTAssertEqual(violations[0].locationInfo!.line, 4) - // XCTAssertEqual(violations[0].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[1].checkInfo, checkInfo) - // XCTAssertEqual(violations[1].filePath, "\(tempDir)/Sources/Hello.swift") - // XCTAssertEqual(violations[1].locationInfo!.line, 5) - // XCTAssertEqual(violations[1].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[2].checkInfo, checkInfo) - // XCTAssertEqual(violations[2].filePath, "\(tempDir)/Sources/World.swift") - // XCTAssertEqual(violations[2].locationInfo!.line, 5) - // XCTAssertEqual(violations[2].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[3].checkInfo, checkInfo) - // XCTAssertEqual(violations[3].filePath, "\(tempDir)/Sources/Foo.swift") - // XCTAssertEqual(violations[3].locationInfo!.line, 4) - // XCTAssertEqual(violations[3].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[4].checkInfo, checkInfo) - // XCTAssertEqual(violations[4].filePath, "\(tempDir)/Sources/Bar.swift") - // XCTAssertEqual(violations[4].locationInfo!.line, 4) - // XCTAssertEqual(violations[4].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[5].checkInfo, checkInfo) - // XCTAssertEqual(violations[5].filePath, "\(tempDir)/Sources/Bar.swift") - // XCTAssertEqual(violations[5].locationInfo!.line, 5) - // XCTAssertEqual(violations[5].locationInfo!.charInLine, 1) - // } - // } - // - // func testSkipIfEqualsToAutocorrectReplacement() { - // let temporaryFiles: [TemporaryFile] = [ - // (subpath: "Sources/Hello.swift", contents: "let x = 5\nvar y = 10"), - // (subpath: "Sources/World.swift", contents: "let x =5\nvar y= 10"), - // ] - // - // withTemporaryFiles(temporaryFiles) { filePathsToCheck in - // let checkInfo = CheckInfo( - // id: "Whitespacing", - // hint: "Always add a single whitespace around '='.", - // severity: .warning - // ) - // let violations = try FileContentsChecker( - // checkInfo: checkInfo, - // regex: #"(let|var) (\w+)\s*=\s*(\w+)"#, - // filePathsToCheck: filePathsToCheck, - // autoCorrectReplacement: "$1 $2 = $3", - // repeatIfAutoCorrected: false - // ) - // .performCheck() - // - // XCTAssertEqual(violations.count, 2) - // - // XCTAssertEqual(violations[0].checkInfo, checkInfo) - // XCTAssertEqual(violations[0].filePath, "\(tempDir)/Sources/World.swift") - // XCTAssertEqual(violations[0].locationInfo!.line, 1) - // XCTAssertEqual(violations[0].locationInfo!.charInLine, 1) - // - // XCTAssertEqual(violations[1].checkInfo, checkInfo) - // XCTAssertEqual(violations[1].filePath, "\(tempDir)/Sources/World.swift") - // XCTAssertEqual(violations[1].locationInfo!.line, 2) - // XCTAssertEqual(violations[1].locationInfo!.charInLine, 1) - // } - // } - // - // func testRepeatIfAutoCorrected() { - // let temporaryFiles: [TemporaryFile] = [ - // (subpath: "Sources/Hello.swift", contents: "let x = 500\nvar y = 10000"), - // (subpath: "Sources/World.swift", contents: "let x = 50000000\nvar y = 100000000000000"), - // ] - // - // withTemporaryFiles(temporaryFiles) { filePathsToCheck in - // let checkInfo = CheckInfo( - // id: "LongNumbers", - // hint: "Format long numbers with `_` after each triple of digits from the right.", - // severity: .warning - // ) - // let violations = try FileContentsChecker( - // checkInfo: checkInfo, - // regex: #"(? FilePathsChecker { - // FilePathsChecker( - // checkInfo: sayHelloCheck(), - // regex: #".*Hello\.swift"#, - // filePathsToCheck: filePathsToCheck, - // autoCorrectReplacement: nil, - // violateIfNoMatchesFound: true - // ) - // } - // - // private func sayHelloCheck() -> CheckInfo { - // CheckInfo(id: "say_hello", hint: "Should always say hello.", severity: .info) - // } - // - // private func noWorldChecker(filePathsToCheck: [String]) -> FilePathsChecker { - // FilePathsChecker( - // checkInfo: noWorldCheck(), - // regex: #".*World\.swift"#, - // filePathsToCheck: filePathsToCheck, - // autoCorrectReplacement: nil, - // violateIfNoMatchesFound: false - // ) - // } - // - // private func noWorldCheck() -> CheckInfo { - // CheckInfo(id: "no_world", hint: "Do not include the global world, be more specific instead.", severity: .error) - // } + private func sayHelloChecker(filePathsToCheck: [String]) -> FilePathsChecker { + FilePathsChecker( + id: "say_hello", + hint: "Should always say hello.", + severity: .info, + regex: try! Regex(#".*Hello\.swift"#), + filePathsToCheck: filePathsToCheck, + autoCorrectReplacement: nil, + violateIfNoMatchesFound: true + ) + } + + private func noWorldChecker(filePathsToCheck: [String]) -> FilePathsChecker { + FilePathsChecker( + id: "no_world", + hint: "Do not include the global world, be more specific instead.", + severity: .error, + regex: try! Regex(#".*World\.swift"#), + filePathsToCheck: filePathsToCheck, + autoCorrectReplacement: nil, + violateIfNoMatchesFound: false + ) + } } diff --git a/Tests/CheckersTests/FilesSearchTests.swift b/Tests/CheckersTests/FilesSearchTests.swift index 3c93c6e..4f373e7 100644 --- a/Tests/CheckersTests/FilesSearchTests.swift +++ b/Tests/CheckersTests/FilesSearchTests.swift @@ -1,67 +1,59 @@ @testable import Checkers import XCTest +import Core final class FilesSearchTests: XCTestCase { - func testSample() { - XCTAssertTrue(true) // TODO: [cg_2021-07-31] not yet implemented + func testAllFilesWithinPath() { + withTemporaryFiles( + [ + (subpath: "Sources/Hello.swift", contents: ""), + (subpath: "Sources/World.swift", contents: ""), + (subpath: "Sources/.hidden_file", contents: ""), + (subpath: "Sources/.hidden_dir/unhidden_file", contents: ""), + ] + ) { _ in + let includeFilterFilePaths = FilesSearch.shared + .allFiles( + within: FileManager.default.currentDirectoryPath, + includeFilters: [try Regex("\(tempDir)/.*")], + excludeFilters: [] + ) + .sorted() + XCTAssertEqual(includeFilterFilePaths, ["\(tempDir)/Sources/Hello.swift", "\(tempDir)/Sources/World.swift"]) + + let excludeFilterFilePaths = FilesSearch.shared.allFiles( + within: FileManager.default.currentDirectoryPath, + includeFilters: [try Regex("\(tempDir)/.*")], + excludeFilters: [try Regex("World")] + ) + XCTAssertEqual(excludeFilterFilePaths, ["\(tempDir)/Sources/Hello.swift"]) + } } - // override func setUp() { - // log = Logger(outputType: .test) - // TestHelper.shared.reset() - // } - // - // func testAllFilesWithinPath() { - // withTemporaryFiles( - // [ - // (subpath: "Sources/Hello.swift", contents: ""), - // (subpath: "Sources/World.swift", contents: ""), - // (subpath: "Sources/.hidden_file", contents: ""), - // (subpath: "Sources/.hidden_dir/unhidden_file", contents: ""), - // ] - // ) { _ in - // let includeFilterFilePaths = FilesSearch.shared - // .allFiles( - // within: FileManager.default.currentDirectoryPath, - // includeFilters: [try Regex("\(tempDir)/.*")], - // excludeFilters: [] - // ) - // .sorted() - // XCTAssertEqual(includeFilterFilePaths, ["\(tempDir)/Sources/Hello.swift", "\(tempDir)/Sources/World.swift"]) - // - // let excludeFilterFilePaths = FilesSearch.shared.allFiles( - // within: FileManager.default.currentDirectoryPath, - // includeFilters: [try Regex("\(tempDir)/.*")], - // excludeFilters: ["World"] - // ) - // XCTAssertEqual(excludeFilterFilePaths, ["\(tempDir)/Sources/Hello.swift"]) - // } - // } - // - // func testPerformanceOfSameSearchOptions() { - // let swiftSourcesFilePaths = (0...800) - // .map { (subpath: "Sources/Foo\($0).swift", contents: "Lorem ipsum\ndolor sit amet\n") } - // let testsFilePaths = (0...400).map { (subpath: "Tests/Foo\($0).swift", contents: "Lorem ipsum\ndolor sit amet\n") } - // let storyboardSourcesFilePaths = (0...300) - // .map { (subpath: "Sources/Foo\($0).storyboard", contents: "Lorem ipsum\ndolor sit amet\n") } - // - // withTemporaryFiles(swiftSourcesFilePaths + testsFilePaths + storyboardSourcesFilePaths) { _ in - // let fileSearchCode: () -> [String] = { - // FilesSearch.shared.allFiles( - // within: FileManager.default.currentDirectoryPath, - // includeFilters: [try! Regex(#"\#(self.tempDir)/Sources/Foo.*"#)], - // excludeFilters: [try! Regex(#"\#(self.tempDir)/.*\.storyboard"#)] - // ) - // } - // - // // first run - // XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) - // - // measure { - // // subsequent runs (should be much faster) - // XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) - // XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) - // } - // } - // } + func testPerformanceOfSameSearchOptions() { + let swiftSourcesFilePaths = (0...800) + .map { (subpath: "Sources/Foo\($0).swift", contents: "Lorem ipsum\ndolor sit amet\n") } + let testsFilePaths = (0...400).map { (subpath: "Tests/Foo\($0).swift", contents: "Lorem ipsum\ndolor sit amet\n") } + let storyboardSourcesFilePaths = (0...300) + .map { (subpath: "Sources/Foo\($0).storyboard", contents: "Lorem ipsum\ndolor sit amet\n") } + + withTemporaryFiles(swiftSourcesFilePaths + testsFilePaths + storyboardSourcesFilePaths) { _ in + let fileSearchCode: () -> [String] = { + FilesSearch.shared.allFiles( + within: FileManager.default.currentDirectoryPath, + includeFilters: [try! Regex(#"\#(self.tempDir)/Sources/Foo.*"#)], + excludeFilters: [try! Regex(#"\#(self.tempDir)/.*\.storyboard"#)] + ) + } + + // first run + XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) + + measure { + // subsequent runs (should be much faster) + XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) + XCTAssertEqual(Set(fileSearchCode()), Set(swiftSourcesFilePaths.map { "\(tempDir)/\($0.subpath)" })) + } + } + } } diff --git a/Tests/CheckersTests/LintTests.swift b/Tests/CheckersTests/LintTests.swift index bb9bc1b..e99f942 100644 --- a/Tests/CheckersTests/LintTests.swift +++ b/Tests/CheckersTests/LintTests.swift @@ -1,121 +1,135 @@ @testable import Checkers import XCTest +import Core +import TestSupport final class LintTests: XCTestCase { - func testSample() { - XCTAssertTrue(true) // TODO: [cg_2021-07-31] not yet implemented + var testLogger: TestLogger = .init() + + override func setUp() { + testLogger = TestLogger() + log = testLogger + } + + func testValidateRegexMatchesForEach() { + XCTAssertNil(testLogger.exitStatusCode) + + let regex = try! Regex(#"foo[0-9]?bar"#) + let checkInfo = CheckInfo(id: "foo_bar", hint: "do bar", severity: .warning) + + Lint.validate( + regex: regex, + matchesForEach: ["foo1bar", "foobar", "myfoo4barbeque"], + checkInfo: checkInfo + ) + XCTAssertNil(testLogger.exitStatusCode) + + // TODO: [cg_2021-09-05] Swift / XCTest doesn't have a way to test for functions returning `Never` + // Lint.validate( + // regex: regex, + // matchesForEach: ["foo1bar", "FooBar", "myfoo4barbeque"], + // checkInfo: checkInfo + // ) + // XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE) + } + + func testValidateRegexDoesNotMatchAny() { + XCTAssertNil(testLogger.exitStatusCode) + + let regex = try! Regex(#"foo[0-9]?bar"#) + let checkInfo = CheckInfo(id: "foo_bar", hint: "do bar", severity: .warning) + + Lint.validate( + regex: regex, + doesNotMatchAny: ["fooLbar", "FooBar", "myfoo40barbeque"], + checkInfo: checkInfo + ) + XCTAssertNil(testLogger.exitStatusCode) + + // TODO: [cg_2021-09-05] Swift / XCTest doesn't have a way to test for functions returning `Never` + // Lint.validate( + // regex: regex, + // doesNotMatchAny: ["fooLbar", "foobar", "myfoo40barbeque"], + // checkInfo: checkInfo + // ) + // XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE) + } + + func testValidateAutocorrectsAllExamplesWithAnonymousGroups() { + XCTAssertNil(testLogger.exitStatusCode) + + let anonymousCaptureRegex = try? Regex(#"([^\.]+)(\.)([^\.]+)(\.)([^\.]+)"#) + + Lint.validateAutocorrectsAll( + checkInfo: CheckInfo(id: "id", hint: "hint"), + examples: [ + AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), + AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), + ], + regex: anonymousCaptureRegex!, + autocorrectReplacement: "$5$2$3$4$1" + ) + + XCTAssertNil(testLogger.exitStatusCode) + + // TODO: [cg_2021-09-05] Swift / XCTest doesn't have a way to test for functions returning `Never` + // Lint.validateAutocorrectsAll( + // checkInfo: CheckInfo(id: "id", hint: "hint"), + // examples: [ + // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), + // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), + // ], + // regex: anonymousCaptureRegex!, + // autocorrectReplacement: "$4$1$2$3$0" + // ) + // + // XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE) + } + + func testValidateAutocorrectsAllExamplesWithNamedGroups() { + XCTAssertNil(testLogger.exitStatusCode) + + let namedCaptureRegex = try! Regex(#"([^\.]+)\.([^\.]+)\.([^\.]+)"#) + + Lint.validateAutocorrectsAll( + checkInfo: CheckInfo(id: "id", hint: "hint"), + examples: [ + AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), + AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), + ], + regex: namedCaptureRegex, + autocorrectReplacement: "$3.$2.$1" + ) + + XCTAssertNil(testLogger.exitStatusCode) + + // TODO: [cg_2021-09-05] Swift / XCTest doesn't have a way to test for functions returning `Never` + // Lint.validateAutocorrectsAll( + // checkInfo: CheckInfo(id: "id", hint: "hint"), + // examples: [ + // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), + // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), + // ], + // regex: namedCaptureRegex, + // autocorrectReplacement: "$sfx$sep1$cnt$sep2$pref" + // ) + // + // XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE) } - // override func setUp() { - // log = Logger(outputType: .test) - // TestHelper.shared.reset() - // } - // - // func testValidateRegexMatchesForEach() { - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // let regex: Regex = #"foo[0-9]?bar"# - // let checkInfo = CheckInfo(id: "foo_bar", hint: "do bar", severity: .warning) - // - // Lint.validate( - // regex: regex, - // matchesForEach: ["foo1bar", "foobar", "myfoo4barbeque"], - // checkInfo: checkInfo - // ) - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // Lint.validate( - // regex: regex, - // matchesForEach: ["foo1bar", "FooBar", "myfoo4barbeque"], - // checkInfo: checkInfo - // ) - // XCTAssertEqual(TestHelper.shared.exitStatus, .failure) - // } - // - // func testValidateRegexDoesNotMatchAny() { - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // let regex: Regex = #"foo[0-9]?bar"# - // let checkInfo = CheckInfo(id: "foo_bar", hint: "do bar", severity: .warning) - // - // Lint.validate( - // regex: regex, - // doesNotMatchAny: ["fooLbar", "FooBar", "myfoo40barbeque"], - // checkInfo: checkInfo - // ) - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // Lint.validate( - // regex: regex, - // doesNotMatchAny: ["fooLbar", "foobar", "myfoo40barbeque"], - // checkInfo: checkInfo - // ) - // XCTAssertEqual(TestHelper.shared.exitStatus, .failure) - // } - // - // func testValidateAutocorrectsAllExamplesWithAnonymousGroups() { - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // let anonymousCaptureRegex = try? Regex(#"([^\.]+)(\.)([^\.]+)(\.)([^\.]+)"#) - // - // Lint.validateAutocorrectsAll( - // checkInfo: CheckInfo(id: "id", hint: "hint"), - // examples: [ - // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), - // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), - // ], - // regex: anonymousCaptureRegex!, - // autocorrectReplacement: "$5$2$3$4$1" - // ) - // - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // Lint.validateAutocorrectsAll( - // checkInfo: CheckInfo(id: "id", hint: "hint"), - // examples: [ - // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), - // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), - // ], - // regex: anonymousCaptureRegex!, - // autocorrectReplacement: "$4$1$2$3$0" - // ) - // - // XCTAssertEqual(TestHelper.shared.exitStatus, .failure) - // } - // - // func testValidateAutocorrectsAllExamplesWithNamedGroups() { - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // let namedCaptureRegex: Regex = [ - // "prefix": #"[^\.]+"#, - // "separator1": #"\."#, - // "content": #"[^\.]+"#, - // "separator2": #"\."#, - // "suffix": #"[^\.]+"#, - // ] - // - // Lint.validateAutocorrectsAll( - // checkInfo: CheckInfo(id: "id", hint: "hint"), - // examples: [ - // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), - // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), - // ], - // regex: namedCaptureRegex, - // autocorrectReplacement: "$suffix$separator1$content$separator2$prefix" - // ) - // - // XCTAssertNil(TestHelper.shared.exitStatus) - // - // Lint.validateAutocorrectsAll( - // checkInfo: CheckInfo(id: "id", hint: "hint"), - // examples: [ - // AutoCorrection(before: "prefix.content.suffix", after: "suffix.content.prefix"), - // AutoCorrection(before: "forums.swift.org", after: "org.swift.forums"), - // ], - // regex: namedCaptureRegex, - // autocorrectReplacement: "$sfx$sep1$cnt$sep2$pref" - // ) - // - // XCTAssertEqual(TestHelper.shared.exitStatus, .failure) - // } + func testRunCustomScript() { + // TODO: [cg_2021-09-05] not yet implemented + } + + func testValidateParameterCombinations() { + // TODO: [cg_2021-09-05] not yet implemented + } + + func testCheckFileContents() { + // TODO: [cg_2021-09-05] not yet implemented + } + + func testCheckFilePaths() { + // TODO: [cg_2021-09-05] not yet implemented + } }