Skip to content

Commit

Permalink
Cover missing tests & cleanup unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Sep 11, 2021
1 parent 791b963 commit f778aed
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 113 deletions.
23 changes: 0 additions & 23 deletions Sources/Checkers/CustomScriptsChecker.swift

This file was deleted.

11 changes: 8 additions & 3 deletions Sources/Checkers/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum Lint {
)

if let autoCorrectReplacement = autoCorrectReplacement {
validateAutocorrectsAll(
validateAutocorrectsAllExamples(
check: check,
examples: autoCorrectExamples,
regex: regex,
Expand Down Expand Up @@ -101,7 +101,7 @@ public enum Lint {
)

if let autoCorrectReplacement = autoCorrectReplacement {
validateAutocorrectsAll(
validateAutocorrectsAllExamples(
check: check,
examples: autoCorrectExamples,
regex: regex,
Expand Down Expand Up @@ -140,6 +140,8 @@ public enum Lint {

do {
let output = try shellOut(to: "/bin/bash", arguments: [tempScriptFileUrl.path])

// clean up temporary script file after successful execution
try FileManager.default.removeItem(at: tempScriptFileUrl)

if let jsonString = output.lintResultsJsonString,
Expand All @@ -160,6 +162,9 @@ public enum Lint {
}
}
catch {
// clean up temporary script file after failed execution
try? FileManager.default.removeItem(at: tempScriptFileUrl)

if let shellOutError = error as? ShellOutError, shellOutError.terminationStatus != 0 {
return [
check.severity: [
Expand Down Expand Up @@ -198,7 +203,7 @@ public enum Lint {
}
}

static func validateAutocorrectsAll(
static func validateAutocorrectsAllExamples(
check: Check,
examples: [AutoCorrection],
regex: Regex,
Expand Down
28 changes: 0 additions & 28 deletions Sources/Reporting/CodableLintResults.swift

This file was deleted.

45 changes: 0 additions & 45 deletions Sources/Reporting/CodableOrderedDictionary.swift

This file was deleted.

14 changes: 14 additions & 0 deletions Tests/CheckersTests/Extensions/RegexExtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@ final class RegexExtTests: XCTestCase {
"\n- Sample Text. \n"
)
}

func testReplaceAllCaptures() throws {
let anonymousRefsRegex = try Regex(#"(\w+)\.(\w+)\.(\w+)"#)
XCTAssertEqual(
anonymousRefsRegex.replaceAllCaptures(in: "prefix.content.suffix", with: "$3-$2-$1"),
"suffix-content-prefix"
)

let namedRefsRegex = try Regex(#"(?<prefix>\w+)\.(?<content>\w+)\.(?<suffix>\w+)"#)
XCTAssertEqual(
namedRefsRegex.replaceAllCaptures(in: "prefix.content.suffix", with: "$suffix-$content-$prefix"),
"suffix-content-prefix"
)
}
}
97 changes: 83 additions & 14 deletions Tests/CheckersTests/LintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ final class LintTests: XCTestCase {
func testValidateAutocorrectsAllExamplesWithAnonymousGroups() {
XCTAssertNil(testLogger.exitStatusCode)

let anonymousCaptureRegex = try? Regex(#"([^\.]+)(\.)([^\.]+)(\.)([^\.]+)"#)
let anonymousCaptureRegex = try? Regex(#"([^\.]+)\.([^\.]+)\.([^\.]+)"#)

Lint.validateAutocorrectsAll(
Lint.validateAutocorrectsAllExamples(
check: Check(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"
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(
// Lint.validateAutocorrectsAllExamples(
// check: Check(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"
// autocorrectReplacement: "$2.$1.$0"
// )
//
// XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE)
Expand All @@ -91,29 +91,29 @@ final class LintTests: XCTestCase {
func testValidateAutocorrectsAllExamplesWithNamedGroups() {
XCTAssertNil(testLogger.exitStatusCode)

let namedCaptureRegex = try! Regex(#"([^\.]+)\.([^\.]+)\.([^\.]+)"#)
let namedCaptureRegex = try! Regex(#"(?<prefix>[^\.]+)\.(?<content>[^\.]+)\.(?<suffix>[^\.]+)"#)

Lint.validateAutocorrectsAll(
Lint.validateAutocorrectsAllExamples(
check: Check(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"
autocorrectReplacement: "$suffix.$content.$prefix"
)

XCTAssertNil(testLogger.exitStatusCode)

// TODO: [cg_2021-09-05] Swift / XCTest doesn't have a way to test for functions returning `Never`
// Lint.validateAutocorrectsAll(
// Lint.validateAutocorrectsAllExamples(
// check: Check(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"
// autocorrectReplacement: "$sfx.$cnt.$pref"
// )
//
// XCTAssertEqual(testLogger.exitStatusCode, EXIT_FAILURE)
Expand Down Expand Up @@ -248,11 +248,80 @@ final class LintTests: XCTestCase {
// )
}

func testCheckFileContents() {
// TODO: [cg_2021-09-05] not yet implemented
func testCheckFileContents() throws {
let temporaryFiles: [TemporaryFile] = [
(
subpath: "Sources/Hello.swift",
contents: """
let x = 5
var y = 10
"""
),
(
subpath: "Sources/World.swift",
contents: """
let x=5
var y=10
"""
),
]

withTemporaryFiles(temporaryFiles) { filePathsToCheck in
var fileContents: [String] = try filePathsToCheck.map { try String(contentsOfFile: $0) }

XCTAssertNoDifference(temporaryFiles[0].contents, fileContents[0])
XCTAssertNoDifference(temporaryFiles[1].contents, fileContents[1])

let violations = try Lint.checkFileContents(
check: .init(id: "1", hint: "hint #1"),
regex: .init(#"(let|var) (\w+)=(\w+)"#),
matchingExamples: ["let x=4"],
autoCorrectReplacement: "$1 $2 = $3",
autoCorrectExamples: [.init(before: "let x=4", after: "let x = 4")]
)

fileContents = try filePathsToCheck.map { try String(contentsOfFile: $0) }

XCTAssertEqual(violations.count, 2)
XCTAssertNoDifference(violations.map(\.matchedString), ["let x=5", "var y=10"])
XCTAssertNoDifference(
violations.map(\.location)[0],
Location(filePath: "\(tempDir)/Sources/World.swift", row: 1, column: 1)
)
XCTAssertNoDifference(
violations.map(\.location)[1],
Location(filePath: "\(tempDir)/Sources/World.swift", row: 2, column: 1)
)
XCTAssertNoDifference(fileContents[0], temporaryFiles[0].contents)
XCTAssertNoDifference(
fileContents[1],
"""
let x = 5
var y = 10
"""
)
}
}

func testCheckFilePaths() {
// TODO: [cg_2021-09-05] not yet implemented
func testCheckFilePaths() throws {
let violations = try Lint.checkFilePaths(
check: .init(id: "2", hint: "hint for #2", severity: .warning),
regex: .init(#"README\.md"#),
violateIfNoMatchesFound: true
)

XCTAssertEqual(violations.count, 1)
XCTAssertNoDifference(
violations.map(\.matchedString),
[nil]
)
XCTAssertNoDifference(
violations.map(\.location),
[nil]
)
XCTAssertNoDifference(
violations.map(\.appliedAutoCorrection),
[nil]
)
}
}

0 comments on commit f778aed

Please sign in to comment.