Skip to content

Commit

Permalink
Ensure LinuxMain is up-to-date using AnyLint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed May 17, 2020
1 parent cd03aaa commit 854ed60
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription
let package = Package(
name: "AnyLint",
products: [
.library(name: "AnyLint", targets: ["AnyLint"]),
.library(name: "AnyLint", targets: ["AnyLint", "Utility"]),
.executable(name: "anylint", targets: ["AnyLintCLI"]),
],
dependencies: [
Expand Down
5 changes: 3 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.17.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.18.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

@testable import AnyLintTests
Expand Down Expand Up @@ -29,7 +29,8 @@ extension FileContentsCheckerTests {
static var allTests: [(String, (FileContentsCheckerTests) -> () throws -> Void)] = [
("testPerformCheck", testPerformCheck),
("testSkipInFile", testSkipInFile),
("testSkipHere", testSkipHere)
("testSkipHere", testSkipHere),
("testSkipIfEqualsToAutocorrectReplacement", testSkipIfEqualsToAutocorrectReplacement)
]
}

Expand Down
46 changes: 46 additions & 0 deletions lint.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/local/bin/swift-sh
import AnyLint // .
import Utility
import ShellOut // @JohnSundell

try Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
// MARK: - Variables
let swiftSourceFiles: Regex = #"Sources/.*\.swift"#
let swiftTestFiles: Regex = #"Tests/.*\.swift"#
let readmeFile: Regex = #"README\.md"#
let changelogFile: Regex = #"^CHANGELOG\.md$"#
let projectName: String = "AnyLint"

// MARK: - Checks
// MARK: Changelog
Expand Down Expand Up @@ -437,6 +440,49 @@ try Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
]
)

// MARK: LinuxMainUpToDate
try Lint.customCheck(checkInfo: "LinuxMainUpToDate: The tests in Tests/LinuxMain.swift should be up-to-date.") { checkInfo in
var violations: [Violation] = []

let linuxMainFilePath = "Tests/LinuxMain.swift"
let linuxMainContentsBeforeRegeneration = try! String(contentsOfFile: linuxMainFilePath)

let sourceryDirPath = ".sourcery"
let testsDirPath = "Tests/\(projectName)Tests"
let stencilFilePath = "\(sourceryDirPath)/LinuxMain.stencil"
let generatedLinuxMainFilePath = "\(sourceryDirPath)/LinuxMain.generated.swift"

let sourceryInstallPath = try? shellOut(to: "which", arguments: ["sourcery"])
guard sourceryInstallPath != nil else {
log.message(
"Skipped custom check \(checkInfo) – requires Sourcery to be installed, download from: https://github.com/krzysztofzablocki/Sourcery",
level: .warning
)
return []
}

try! shellOut(to: "sourcery", arguments: ["--sources", testsDirPath, "--templates", stencilFilePath, "--output", sourceryDirPath])
let linuxMainContentsAfterRegeneration = try! String(contentsOfFile: generatedLinuxMainFilePath)

// move generated file to LinuxMain path to update its contents
try! shellOut(to: "mv", arguments: [generatedLinuxMainFilePath, linuxMainFilePath])

if linuxMainContentsBeforeRegeneration != linuxMainContentsAfterRegeneration {
violations.append(
Violation(
checkInfo: checkInfo,
filePath: linuxMainFilePath,
appliedAutoCorrection: AutoCorrection(
before: linuxMainContentsBeforeRegeneration,
after: linuxMainContentsAfterRegeneration
)
)
)
}

return violations
}

// MARK: Logger
try Lint.checkFileContents(
checkInfo: "Logger: Don't use `print` – use `log.message` instead.",
Expand Down

0 comments on commit 854ed60

Please sign in to comment.