-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor OutdatedGem
; install FactoryBot for improved test coverage
#5
Changes from all commits
7f52545
a1a1207
7be467b
ffb5805
be099b1
64aeb36
f303e6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require: | ||
- rubocop-factory_bot | ||
- rubocop-minitest | ||
- rubocop-packaging | ||
- rubocop-performance | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,17 @@ | |
source "https://rubygems.org" | ||
gemspec | ||
|
||
gem "factory_bot", "~> 6.3.0" | ||
gem "minitest", "~> 5.11" | ||
gem "minitest-snapshots", "~> 1.1" | ||
gem "mocha", "~> 2.4" | ||
gem "observer" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗒️ FactoryBot 6.3 has an undeclared dependency on |
||
gem "rake", "~> 13.0" | ||
|
||
if RUBY_VERSION >= "3.3" | ||
gem "mighty_test", "~> 0.3" | ||
gem "rubocop", "1.64.1" | ||
gem "rubocop-factory_bot", "2.26.1" | ||
gem "rubocop-minitest", "0.35.0" | ||
gem "rubocop-packaging", "0.5.2" | ||
gem "rubocop-performance", "1.21.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ def render_gem(name) | |
|
||
def render | ||
lines = [render_header] | ||
rows.each_key { |name| lines << render_gem(name) } | ||
rows.keys.sort.each { |name| lines << render_gem(name) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗒️ I moved sorting behavior into |
||
lines.join("\n") | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,21 @@ | |
|
||
module BundleUpdateInteractive | ||
class OutdatedGem | ||
attr_reader :current_lockfile_entry, :updated_lockfile_entry, :gemfile_groups | ||
attr_writer :vulnerable | ||
attr_accessor :name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗒️ This refactor removes dependencies on lock file entries (which are deep data structures that in turn depend on internal Bundler classes) in favor of a flat set of simple PORO attributes. |
||
:gemfile_groups, | ||
:git_source_uri, | ||
:current_version, | ||
:current_git_version, | ||
:updated_version, | ||
:updated_git_version | ||
|
||
def initialize(current_lockfile_entry:, updated_lockfile_entry:, gemfile_groups:) | ||
@current_lockfile_entry = current_lockfile_entry | ||
@updated_lockfile_entry = updated_lockfile_entry | ||
@gemfile_groups = gemfile_groups | ||
@changelog_locator = ChangelogLocator.new | ||
attr_writer :rubygems_source, :vulnerable | ||
|
||
def initialize(**attrs) | ||
@vulnerable = nil | ||
end | ||
@changelog_locator = ChangelogLocator.new | ||
|
||
def name | ||
current_lockfile_entry.name | ||
attrs.each { |name, value| public_send(:"#{name}=", value) } | ||
end | ||
|
||
def semver_change | ||
|
@@ -25,13 +27,17 @@ def vulnerable? | |
@vulnerable | ||
end | ||
|
||
def rubygems_source? | ||
@rubygems_source | ||
end | ||
|
||
def changelog_uri | ||
return @changelog_uri if defined?(@changelog_uri) | ||
|
||
@changelog_uri = | ||
if git_version_changed? | ||
"https://github.com/#{github_repo}/compare/#{current_git_version}...#{updated_git_version}" | ||
elsif updated_lockfile_entry.rubygems_source? | ||
elsif rubygems_source? | ||
changelog_locator.find_changelog_uri(name: name, version: updated_version.to_s) | ||
else | ||
begin | ||
|
@@ -42,22 +48,6 @@ def changelog_uri | |
end | ||
end | ||
|
||
def current_version | ||
current_lockfile_entry.version | ||
end | ||
|
||
def updated_version | ||
updated_lockfile_entry.version | ||
end | ||
|
||
def current_git_version | ||
current_lockfile_entry.git_version | ||
end | ||
|
||
def updated_git_version | ||
updated_lockfile_entry.git_version | ||
end | ||
|
||
def git_version_changed? | ||
current_git_version && updated_git_version && current_git_version != updated_git_version | ||
end | ||
|
@@ -69,8 +59,7 @@ def git_version_changed? | |
def github_repo | ||
return nil unless updated_git_version | ||
|
||
updated_lockfile_entry.git_source_uri.to_s[%r{^(?:git@github.com:|https://github.com/)([^/]+/[^/]+?)(:?\.git)?(?:$|/)}i, | ||
1] | ||
git_source_uri.to_s[%r{^(?:git@github.com:|https://github.com/)([^/]+/[^/]+?)(:?\.git)?(?:$|/)}i, 1] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class BundleUpdateInteractive::CLI | ||
class RowTest < Minitest::Test | ||
def test_formatted_gem_name_for_vulnerable_gem_is_red_on_white | ||
outdated_gem = build(:outdated_gem, name: "rails", vulnerable: true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗒️ These tests were super easy to write, because I can now use FactoryBot's |
||
row = Row.new(outdated_gem) | ||
|
||
assert_equal "\e[37;41mrails\e[0m", row.formatted_gem_name | ||
end | ||
|
||
def test_formatted_updated_version_highlights_diff_in_cyan_regardless_of_semver_change | ||
outdated_gem = build( | ||
:outdated_gem, | ||
current_version: "7.0.5", | ||
updated_version: "7.1.2", | ||
current_git_version: "a1a1207", | ||
updated_git_version: "0e5bafe" | ||
) | ||
row = Row.new(outdated_gem) | ||
|
||
assert_equal "7.\e[36m1.2\e[0m \e[36m0e5bafe\e[0m", row.formatted_updated_version | ||
end | ||
|
||
def test_name_and_version_red_if_major_semver_change | ||
outdated_gem = build(:outdated_gem, name: "rails", current_version: "6.1.2", updated_version: "7.0.3") | ||
row = Row.new(outdated_gem) | ||
|
||
assert_equal "\e[31mrails\e[0m", row.formatted_gem_name | ||
assert_equal "\e[31m7.0.3\e[0m", row.formatted_updated_version | ||
end | ||
|
||
def test_name_and_version_yellow_if_minor_semver_change | ||
outdated_gem = build(:outdated_gem, name: "rails", current_version: "7.0.3", updated_version: "7.1.0") | ||
row = Row.new(outdated_gem) | ||
|
||
assert_equal "\e[33mrails\e[0m", row.formatted_gem_name | ||
assert_equal "7.\e[33m1.0\e[0m", row.formatted_updated_version | ||
end | ||
|
||
def test_name_and_version_green_if_patch_semver_change | ||
outdated_gem = build(:outdated_gem, name: "rails", current_version: "7.0.3", updated_version: "7.0.4") | ||
row = Row.new(outdated_gem) | ||
|
||
assert_equal "\e[32mrails\e[0m", row.formatted_gem_name | ||
assert_equal "7.0.\e[32m4\e[0m", row.formatted_updated_version | ||
end | ||
|
||
def test_formatted_gemfile_groups_handles_nil_groups | ||
outdated_gem = build(:outdated_gem, gemfile_groups: nil) | ||
row = Row.new(outdated_gem) | ||
|
||
assert_nil row.formatted_gemfile_groups | ||
end | ||
|
||
def test_formatted_gemfile_groups_returns_comma_separated_symbols | ||
outdated_gem = build(:outdated_gem, gemfile_groups: %i[development test]) | ||
row = Row.new(outdated_gem) | ||
|
||
assert_equal ":development, :test", row.formatted_gemfile_groups | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :outdated_gem, class: "BundleUpdateInteractive::OutdatedGem" do | ||
current_git_version { nil } | ||
current_version { "7.0.3" } | ||
git_source_uri { nil } | ||
name { "rails" } | ||
rubygems_source { true } | ||
updated_git_version { nil } | ||
updated_version { "7.1.0" } | ||
vulnerable { false } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "factory_bot" | ||
|
||
FactoryBot.find_definitions | ||
|
||
class Minitest::Test | ||
include FactoryBot::Syntax::Methods | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗒️ I had to pin at 6.3 because newer versions of FactoryBot do not support Ruby 2.7, which is still part of our CI.