From 1fdafb00cf95a070bb7e2bbce9a0371ef53b7423 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Sun, 13 Mar 2022 14:01:20 +0000 Subject: [PATCH] Skip update check if DANGER_SWIFT_NO_UPDATE_CHECK is set --- Sources/Runner/main.swift | 5 +-- Sources/RunnerLib/VersionChecker.swift | 6 ++- .../RunnerLibTests/VersionCheckerTests.swift | 38 ++++++++++++++++++- Tests/RunnerLibTests/XCTestManifests.swift | 2 + 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Sources/Runner/main.swift b/Sources/Runner/main.swift index 6e8daae5..b18ae92e 100644 --- a/Sources/Runner/main.swift +++ b/Sources/Runner/main.swift @@ -20,10 +20,7 @@ private func runCommand(_ command: DangerCommand, logger: Logger) throws { } private func checkForUpdate(logger: Logger) { - // FIXME: Add another env variable to opt out this check - // https://github.com/danger/swift/pull/505#discussion_r799356563 - guard ProcessInfo.processInfo.environment["DEBUG"] == nil else { return } - let versionChecker = VersionChecker(logger: logger) + let versionChecker = VersionChecker(logger: logger, env: ProcessInfo.processInfo.environment) versionChecker.checkForUpdate(current: DangerVersion) } diff --git a/Sources/RunnerLib/VersionChecker.swift b/Sources/RunnerLib/VersionChecker.swift index b42ecac0..d4f129f3 100644 --- a/Sources/RunnerLib/VersionChecker.swift +++ b/Sources/RunnerLib/VersionChecker.swift @@ -6,18 +6,22 @@ import Version public struct VersionChecker { private let shellExecutor: ShellExecuting private let logger: Logger + private let env: [String: String] public init( shellExecutor: ShellExecuting = ShellExecutor(), - logger: Logger + logger: Logger, + env: [String: String] ) { self.shellExecutor = shellExecutor self.logger = logger + self.env = env } } public extension VersionChecker { func checkForUpdate(current currentVersionString: String) { + guard env["DANGER_SWIFT_NO_UPDATE_CHECK"] == nil, env["DEBUG"] == nil else { return } guard let latestVersionString = fetchLatestVersion() else { return } guard let latestVersion = Version(latestVersionString) else { logger.debug("Invalid latestVersionString: (\(latestVersionString)") diff --git a/Tests/RunnerLibTests/VersionCheckerTests.swift b/Tests/RunnerLibTests/VersionCheckerTests.swift index 8a698bcb..889eb59a 100644 --- a/Tests/RunnerLibTests/VersionCheckerTests.swift +++ b/Tests/RunnerLibTests/VersionCheckerTests.swift @@ -27,12 +27,45 @@ final class VersionCheckerTests: XCTestCase { isVerbose: false, isSilent: false, printer: spyPrinter - ) + ), + env: [:] ) versionChecker.checkForUpdate(current: "1.0.0") XCTAssertTrue(spyPrinter.printedMessages.contains { $0.contains("A new release of danger-swift is available") }) } + func testItIgnoresTheCheckIfNoUpdateEnvVariableIsSet() throws { + executor.result = mockResult(tagName: "1.0.1") + + let versionChecker = VersionChecker( + shellExecutor: executor, + logger: Logger( + isVerbose: false, + isSilent: false, + printer: spyPrinter + ), + env: ["DANGER_SWIFT_NO_UPDATE_CHECK": "*"] + ) + versionChecker.checkForUpdate(current: "1.0.0") + XCTAssertFalse(spyPrinter.printedMessages.contains { $0.contains("A new release of danger-swift is available") }) + } + + func testItIgnoresTheCheckIfDebugEnvVariableIsSet() throws { + executor.result = mockResult(tagName: "1.0.1") + + let versionChecker = VersionChecker( + shellExecutor: executor, + logger: Logger( + isVerbose: false, + isSilent: false, + printer: spyPrinter + ), + env: ["DEBUG": "*"] + ) + versionChecker.checkForUpdate(current: "1.0.0") + XCTAssertFalse(spyPrinter.printedMessages.contains { $0.contains("A new release of danger-swift is available") }) + } + func testItNotShowNotificationIfRunningIsLatest() throws { executor.result = mockResult(tagName: "1.0.0") @@ -42,7 +75,8 @@ final class VersionCheckerTests: XCTestCase { isVerbose: false, isSilent: false, printer: spyPrinter - ) + ), + env: [:] ) versionChecker.checkForUpdate(current: "1.0.0") XCTAssertFalse(spyPrinter.printedMessages.contains { $0.contains("A new release of danger-swift is available") }) diff --git a/Tests/RunnerLibTests/XCTestManifests.swift b/Tests/RunnerLibTests/XCTestManifests.swift index 4cf830a0..354f1627 100644 --- a/Tests/RunnerLibTests/XCTestManifests.swift +++ b/Tests/RunnerLibTests/XCTestManifests.swift @@ -129,6 +129,8 @@ // `swift test --generate-linuxmain` // to regenerate. static let __allTests__VersionCheckerTests = [ + ("testItIgnoresTheCheckIfDebugEnvVariableIsSet", testItIgnoresTheCheckIfDebugEnvVariableIsSet), + ("testItIgnoresTheCheckIfNoUpdateEnvVariableIsSet", testItIgnoresTheCheckIfNoUpdateEnvVariableIsSet), ("testItNotShowNotificationIfRunningIsLatest", testItNotShowNotificationIfRunningIsLatest), ("testItShowsNotificationIfNewVersionIsAvailable", testItShowsNotificationIfNewVersionIsAvailable), ]