From e81ff2870dd8831fe4263965d0bb1632f499c6d9 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 10 May 2022 01:00:18 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20test=20and=20prepare=20for=20?= =?UTF-8?q?new=20prerelease?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 4 +++ .../Versions/AppUpdaterCheckTest.swift | 21 +++++++++++ phpmon/Domain/App/AppUpdateChecker.swift | 35 ++++++++++--------- 3 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 phpmon-tests/Versions/AppUpdaterCheckTest.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index fa4e1ca..1e68707 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -119,6 +119,7 @@ C464ADB2275A87CA003FCD53 /* DomainListCellProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = C464ADB1275A87CA003FCD53 /* DomainListCellProtocol.swift */; }; C46E206D28299B3800D909D6 /* AppUpdateChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */; }; C46E206E28299B3800D909D6 /* AppUpdateChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */; }; + C46E20702829D27F00D909D6 /* AppUpdaterCheckTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206F2829D27F00D909D6 /* AppUpdaterCheckTest.swift */; }; C46FA23F246C358E00944F05 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46FA23E246C358E00944F05 /* StringExtension.swift */; }; C473319F2470923A009A0597 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = C473319E2470923A009A0597 /* Localizable.strings */; }; C47331A2247093B7009A0597 /* StatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47331A1247093B7009A0597 /* StatusMenu.swift */; }; @@ -340,6 +341,7 @@ C464ADAE275A7A69003FCD53 /* DomainListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListVC.swift; sourceTree = ""; }; C464ADB1275A87CA003FCD53 /* DomainListCellProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListCellProtocol.swift; sourceTree = ""; }; C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUpdateChecker.swift; sourceTree = ""; }; + C46E206F2829D27F00D909D6 /* AppUpdaterCheckTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppUpdaterCheckTest.swift; sourceTree = ""; }; C46FA23E246C358E00944F05 /* StringExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = ""; }; C473319E2470923A009A0597 /* Localizable.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = ""; }; C47331A1247093B7009A0597 /* StatusMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMenu.swift; sourceTree = ""; }; @@ -871,6 +873,7 @@ C48D6C73279CD3E400F26D7E /* PhpVersionNumberTest.swift */, C4B56360276AB0A500F12CCB /* VersionExtractorTest.swift */, C4AF9F7C275454A900D44ED0 /* ValetVersionExtractorTest.swift */, + C46E206F2829D27F00D909D6 /* AppUpdaterCheckTest.swift */, ); path = Versions; sourceTree = ""; @@ -1305,6 +1308,7 @@ 54D9E0B327E4F51E003B9AD9 /* HotKeysController.swift in Sources */, C4B97B79275CF1B5003F3378 /* App+ActivationPolicy.swift in Sources */, C4CE3BBB27B324230086CA49 /* MainMenu+Switcher.swift in Sources */, + C46E20702829D27F00D909D6 /* AppUpdaterCheckTest.swift in Sources */, C4F7809C25D80344000DBC97 /* CommandTest.swift in Sources */, C44CCD4127AFE2FC00CE40E5 /* AlertableError.swift in Sources */, C4D936CA27E3EB6100BD69FE /* PhpHelper.swift in Sources */, diff --git a/phpmon-tests/Versions/AppUpdaterCheckTest.swift b/phpmon-tests/Versions/AppUpdaterCheckTest.swift new file mode 100644 index 0000000..64a2854 --- /dev/null +++ b/phpmon-tests/Versions/AppUpdaterCheckTest.swift @@ -0,0 +1,21 @@ +// +// AppUpdaterCheckTest.swift +// phpmon-tests +// +// Created by Nico Verbruggen on 10/05/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import XCTest + +class AppUpdaterCheckTest: XCTestCase { + + func testCanRetrieveVersionFromCask() { + let caskVersion = AppUpdateChecker.retrieveVersionFromCask() + + let version = VersionExtractor.from(caskVersion) + + XCTAssertNotNil(version) + } + +} diff --git a/phpmon/Domain/App/AppUpdateChecker.swift b/phpmon/Domain/App/AppUpdateChecker.swift index 06991a4..9819ee8 100644 --- a/phpmon/Domain/App/AppUpdateChecker.swift +++ b/phpmon/Domain/App/AppUpdateChecker.swift @@ -19,6 +19,22 @@ class AppUpdateChecker { return App.version.contains("-dev") }() + public static func retrieveVersionFromCask(_ initiatedFromBackground: Bool = true) -> String { + let caskFile = App.version.contains("-dev") + ? Constants.Urls.DevBuildCaskFile.absoluteString + : Constants.Urls.StableBuildCaskFile.absoluteString + + var command = "curl -s" + + if initiatedFromBackground { + command = "curl -s --max-time 5" + } + + return Shell.pipe( + "\(command) '\(caskFile)' | grep version" + ) + } + public static func checkIfNewerVersionIsAvailable(initiatedFromBackground: Bool = true) { if initiatedFromBackground { if !Preferences.isEnabled(.automaticBackgroundUpdateCheck) { @@ -29,22 +45,7 @@ class AppUpdateChecker { Log.info("Automatic updates are enabled, a check will be performed.") } - // Actually check for updates - let caskFile = App.version.contains("-dev") - ? Constants.Urls.DevBuildCaskFile.absoluteString - : Constants.Urls.StableBuildCaskFile.absoluteString - - // We'll find out what the new version is by using `curl` - var command = "curl -s" - - if initiatedFromBackground { - // If running as a background check, should only waste at most 3 secs of time - command = "curl -s --max-time 3" - } - - let versionString = Shell.pipe( - "\(command) '\(caskFile)' | grep version" - ) + let versionString = retrieveVersionFromCask(initiatedFromBackground) guard let onlineVersion = VersionExtractor.from(versionString) else { Log.err("We couldn't check for updates!") @@ -80,7 +81,7 @@ class AppUpdateChecker { Log.info("There is a newer version (\(onlineVersion)) available!") notifyAboutNewerVersion(version: onlineVersion) case .orderedSame: - Log.info("The installed version \(currentVersion) matches the latest release (\(onlineVersion)).") + Log.info("The installed version (\(currentVersion)) matches the latest release (\(onlineVersion)).") if !background { notifyVersionDoesNotNeedUpgrade() }