-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version check if newer CLI available (#30)
- Loading branch information
Showing
6 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'json' | ||
|
||
module EmergeCLI | ||
module Utils | ||
class VersionCheck | ||
def initialize(network: EmergeCLI::Network.new) | ||
@network = network | ||
end | ||
|
||
def check_version | ||
Sync do | ||
response = @network.get( | ||
path: 'https://rubygems.org/api/v1/gems/emerge.json', | ||
headers: {} | ||
) | ||
latest_version = JSON.parse(response.read).fetch('version') | ||
current_version = EmergeCLI::VERSION | ||
|
||
if Gem::Version.new(latest_version) > Gem::Version.new(current_version) | ||
Logger.warn "A new version of emerge-cli is available (#{latest_version})" | ||
Logger.warn "You are currently using version #{current_version}" | ||
Logger.warn "To update, run: gem update emerge\n" | ||
end | ||
end | ||
rescue KeyError | ||
Logger.error 'Failed to parse version from RubyGems API response' | ||
rescue StandardError => e | ||
Logger.error "Failed to check for updates: #{e.message}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'test_helper' | ||
|
||
module EmergeCLI | ||
module Utils | ||
class VersionCheckTest < Minitest::Test | ||
def setup | ||
@network = FakeNetwork.new | ||
@version_check = VersionCheck.new(network: @network) | ||
end | ||
|
||
def test_warns_when_newer_version_available | ||
@network = FakeNetwork.new( | ||
'https://rubygems.org/api/v1/gems/emerge.json' => '{"version": "999.0.0"}' | ||
) | ||
@version_check = VersionCheck.new(network: @network) | ||
|
||
Logger.stub :warn, lambda { |msg| | ||
@captured_warnings ||= [] | ||
@captured_warnings << msg | ||
} do | ||
@version_check.check_version | ||
end | ||
|
||
assert_equal 3, @captured_warnings.length | ||
assert_match(/A new version of emerge-cli is available \(999.0.0\)/, @captured_warnings[0]) | ||
assert_match(/You are currently using version #{EmergeCLI::VERSION}/, @captured_warnings[1]) | ||
assert_match(/To update, run: gem update emerge/, @captured_warnings[2]) | ||
end | ||
|
||
def test_silent_when_current_version | ||
@network = FakeNetwork.new( | ||
'https://rubygems.org/api/v1/gems/emerge.json' => "{\"version\": \"#{EmergeCLI::VERSION}\"}" | ||
) | ||
@version_check = VersionCheck.new(network: @network) | ||
|
||
Logger.stub :warn, ->(_msg) { flunk 'Should not warn when version is current' } do | ||
@version_check.check_version | ||
end | ||
end | ||
|
||
def test_logs_error_when_version_key_missing | ||
@network = FakeNetwork.new( | ||
'https://rubygems.org/api/v1/gems/emerge.json' => '{}' | ||
) | ||
@version_check = VersionCheck.new(network: @network) | ||
|
||
Logger.stub :error, ->(msg) { @error_message = msg } do | ||
@version_check.check_version | ||
end | ||
|
||
assert_equal 'Failed to parse version from RubyGems API response', @error_message | ||
end | ||
end | ||
end | ||
end |