From 854ed60ad093cc4f6d20f12498e345bae73d4075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Mon, 18 May 2020 00:51:16 +0200 Subject: [PATCH] Ensure LinuxMain is up-to-date using AnyLint --- Package.swift | 2 +- Tests/LinuxMain.swift | 5 +++-- lint.swift | 46 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index c0ad908..b266fdf 100644 --- a/Package.swift +++ b/Package.swift @@ -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: [ diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index f25689b..d59ccb2 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -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 @@ -29,7 +29,8 @@ extension FileContentsCheckerTests { static var allTests: [(String, (FileContentsCheckerTests) -> () throws -> Void)] = [ ("testPerformCheck", testPerformCheck), ("testSkipInFile", testSkipInFile), - ("testSkipHere", testSkipHere) + ("testSkipHere", testSkipHere), + ("testSkipIfEqualsToAutocorrectReplacement", testSkipIfEqualsToAutocorrectReplacement) ] } diff --git a/lint.swift b/lint.swift index 11d2132..4265c8a 100755 --- a/lint.swift +++ b/lint.swift @@ -1,5 +1,7 @@ #!/usr/local/bin/swift-sh import AnyLint // . +import Utility +import ShellOut // @JohnSundell try Lint.logSummaryAndExit(arguments: CommandLine.arguments) { // MARK: - Variables @@ -7,6 +9,7 @@ try Lint.logSummaryAndExit(arguments: CommandLine.arguments) { let swiftTestFiles: Regex = #"Tests/.*\.swift"# let readmeFile: Regex = #"README\.md"# let changelogFile: Regex = #"^CHANGELOG\.md$"# + let projectName: String = "AnyLint" // MARK: - Checks // MARK: Changelog @@ -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.",