From 1b19c089c7986ec0239794f2d62483d05f482522 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 19 May 2024 13:51:12 -0600 Subject: [PATCH] Fix spec namespaces There were some not needed `require_relative` calls if the `spec` folder were ordered according to the `lib` folder. So, I removed them and organized the test suite accordingly the classes at library --- CHANGELOG.md | 1 + spec/bundle_report_spec.rb | 77 --------------------------- spec/deprecation_tracker_spec.rb | 6 ++- spec/next_rails/bundle_report_spec.rb | 76 ++++++++++++++++++++++++-- spec/next_rails/gem_info_spec.rb | 45 ++++++++++++++-- spec/next_rails_spec.rb | 4 ++ spec/ten_years_rails/gem_info_spec.rb | 36 ------------- 7 files changed, 123 insertions(+), 122 deletions(-) delete mode 100644 spec/bundle_report_spec.rb delete mode 100644 spec/ten_years_rails/gem_info_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f8eef1..c3ef8ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # main [(unreleased)](https://github.com/fastruby/next_rails/compare/v1.3.0...main) * Your changes/patches go here. +- [CHORE: Use next_rails namespace on spec tests.](https://github.com/fastruby/next_rails/pull/117) # v1.3.0 / 2023-06-16 [(commits)](https://github.com/fastruby/next_rails/compare/v1.2.4...v1.3.0) diff --git a/spec/bundle_report_spec.rb b/spec/bundle_report_spec.rb deleted file mode 100644 index 0882764..0000000 --- a/spec/bundle_report_spec.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -require 'date' -require 'tempfile' -require_relative 'spec_helper' -require_relative '../lib/next_rails/bundle_report' - -RSpec.describe NextRails::BundleReport do - describe '.outdated' do - let(:mock_version) { Struct.new(:version, :age) } - let(:mock_gem) { Struct.new(:name, :version, :age, :latest_version, :up_to_date?, :created_at, :sourced_from_git?) } - let(:format_str) { '%b %e, %Y' } - let(:alpha_date) { Date.parse('2022-01-01') } - let(:alpha_age) { alpha_date.strftime(format_str) } - let(:bravo_date) { Date.parse('2022-02-02') } - let(:bravo_age) { bravo_date.strftime(format_str) } - let(:charlie_date) { Date.parse('2022-03-03') } - let(:charlie_age) { charlie_date.strftime(format_str) } - - before do - allow(NextRails::GemInfo).to receive(:all).and_return( - [ - mock_gem.new('alpha', '0.0.1', alpha_age, mock_version.new('0.0.2', bravo_age), false, alpha_date, false), - mock_gem.new('bravo', '0.2.0', bravo_age, mock_version.new('0.2.2', charlie_age), false, bravo_date, true) - ] - ) - end - - context 'when writing human-readable output' do - #subject { described_class.outdated } - - it 'invokes $stdout.puts properly', :aggregate_failures do - allow($stdout) - .to receive(:puts) - .with("#{'alpha 0.0.1'.bold.white}: released #{alpha_age} (latest version, 0.0.2, released #{bravo_age})\n") - allow($stdout) - .to receive(:puts) - .with("#{'bravo 0.2.0'.bold.white}: released #{bravo_age} (latest version, 0.2.2, released #{charlie_age})\n") - allow($stdout).to receive(:puts).with('') - allow($stdout).to receive(:puts).with(<<-EO_MULTLINE_STRING) - #{'1'.yellow} gems are sourced from git - #{'2'.red} of the 2 gems are out-of-date (100%) - EO_MULTLINE_STRING - end - end - - context 'when writing JSON output' do - it 'JSON is correctly formatted' do - gems = NextRails::GemInfo.all - out_of_date_gems = gems.reject(&:up_to_date?).sort_by(&:created_at) - sourced_from_git = gems.select(&:sourced_from_git?) - - expect(NextRails::BundleReport.build_json(out_of_date_gems, gems.count, sourced_from_git.count)).to eq( - { - outdated_gems: [ - { name: 'alpha', installed_version: '0.0.1', installed_age: alpha_age, latest_version: '0.0.2', - latest_age: bravo_age }, - { name: 'bravo', installed_version: '0.2.0', installed_age: bravo_age, latest_version: '0.2.2', - latest_age: charlie_age } - ], - sourced_from_git_count: sourced_from_git.count, - total_gem_count: gems.count - } - ) - end - end - end - - describe ".compatibility" do - describe "output" do - it "returns ERB generated output" do - output = NextRails::BundleReport.erb_output({}, [], 7.0) - expect(output).to match "gems incompatible with Rails 7.0" - end - end - end -end diff --git a/spec/deprecation_tracker_spec.rb b/spec/deprecation_tracker_spec.rb index e017c0c..a3fda3a 100644 --- a/spec/deprecation_tracker_spec.rb +++ b/spec/deprecation_tracker_spec.rb @@ -1,5 +1,9 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "date" require "tempfile" -require_relative "spec_helper" require_relative "../lib/deprecation_tracker" RSpec::Matchers.define_negated_matcher :not_raise_error, :raise_error diff --git a/spec/next_rails/bundle_report_spec.rb b/spec/next_rails/bundle_report_spec.rb index 07b26b5..a04d6f0 100644 --- a/spec/next_rails/bundle_report_spec.rb +++ b/spec/next_rails/bundle_report_spec.rb @@ -1,7 +1,77 @@ -require_relative ".././spec_helper" -require_relative "../../lib/next_rails/bundle_report" +# frozen_string_literal: true + +require "spec_helper" RSpec.describe NextRails::BundleReport do + describe '.outdated' do + let(:mock_version) { Struct.new(:version, :age) } + let(:mock_gem) { Struct.new(:name, :version, :age, :latest_version, :up_to_date?, :created_at, :sourced_from_git?) } + let(:format_str) { '%b %e, %Y' } + let(:alpha_date) { Date.parse('2022-01-01') } + let(:alpha_age) { alpha_date.strftime(format_str) } + let(:bravo_date) { Date.parse('2022-02-02') } + let(:bravo_age) { bravo_date.strftime(format_str) } + let(:charlie_date) { Date.parse('2022-03-03') } + let(:charlie_age) { charlie_date.strftime(format_str) } + + before do + allow(NextRails::GemInfo).to receive(:all).and_return( + [ + mock_gem.new('alpha', '0.0.1', alpha_age, mock_version.new('0.0.2', bravo_age), false, alpha_date, false), + mock_gem.new('bravo', '0.2.0', bravo_age, mock_version.new('0.2.2', charlie_age), false, bravo_date, true) + ] + ) + end + + context 'when writing human-readable output' do + #subject { described_class.outdated } + + it 'invokes $stdout.puts properly', :aggregate_failures do + allow($stdout) + .to receive(:puts) + .with("#{'alpha 0.0.1'.bold.white}: released #{alpha_age} (latest version, 0.0.2, released #{bravo_age})\n") + allow($stdout) + .to receive(:puts) + .with("#{'bravo 0.2.0'.bold.white}: released #{bravo_age} (latest version, 0.2.2, released #{charlie_age})\n") + allow($stdout).to receive(:puts).with('') + allow($stdout).to receive(:puts).with(<<-EO_MULTLINE_STRING) + #{'1'.yellow} gems are sourced from git + #{'2'.red} of the 2 gems are out-of-date (100%) + EO_MULTLINE_STRING + end + end + + context 'when writing JSON output' do + it 'JSON is correctly formatted' do + gems = NextRails::GemInfo.all + out_of_date_gems = gems.reject(&:up_to_date?).sort_by(&:created_at) + sourced_from_git = gems.select(&:sourced_from_git?) + + expect(NextRails::BundleReport.build_json(out_of_date_gems, gems.count, sourced_from_git.count)).to eq( + { + outdated_gems: [ + { name: 'alpha', installed_version: '0.0.1', installed_age: alpha_age, latest_version: '0.0.2', + latest_age: bravo_age }, + { name: 'bravo', installed_version: '0.2.0', installed_age: bravo_age, latest_version: '0.2.2', + latest_age: charlie_age } + ], + sourced_from_git_count: sourced_from_git.count, + total_gem_count: gems.count + } + ) + end + end + end + + describe ".compatibility" do + describe "output" do + it "returns ERB generated output" do + output = NextRails::BundleReport.erb_output({}, [], 7.0) + expect(output).to match "gems incompatible with Rails 7.0" + end + end + end + describe "#compatible_ruby_version" do context "when rails_version is a valid one" do it "returns the correct ruby version" do @@ -18,7 +88,7 @@ expect(ruby_version).to eq(">= 2.7.0") end end - + context "when rails_version is an invalid one" do it "returns nil for ruby version" do rails_version = { rails_version: "0.0.0" } diff --git a/spec/next_rails/gem_info_spec.rb b/spec/next_rails/gem_info_spec.rb index 169d96e..043a741 100644 --- a/spec/next_rails/gem_info_spec.rb +++ b/spec/next_rails/gem_info_spec.rb @@ -1,10 +1,46 @@ -require_relative ".././spec_helper" -require_relative "../../lib/next_rails/gem_info" +# frozen_string_literal: true + +require "spec_helper" + +require "timecop" RSpec.describe NextRails::GemInfo do + let(:release_date) { Time.utc(2019, 7, 6, 0, 0, 0) } + let(:now) { Time.utc(2019, 7, 6, 12, 0, 0) } + let(:spec) do + Gem::Specification.new do |s| + s.date = release_date + s.version = "1.0.0" + end + end + + subject { NextRails::GemInfo.new(spec) } + + describe "#age" do + around do |example| + Timecop.travel(now) do + example.run + end + end + + let(:result) { now.strftime("%b %e, %Y") } + + it "returns a date" do + expect(subject.age).to eq(result) + end + end + + describe "#up_to_date?" do + it "is up to date" do + allow(Gem).to receive(:latest_spec_for).and_return(spec) + expect(subject.up_to_date?).to be_truthy + end + end + describe "#state" do let(:mock_gem) { Struct.new(:name, :version, :runtime_dependencies) } let(:mocked_dependency) { Struct.new(:name, :requirement) } + it "returns :incompatible if gem specifies a rails dependency but no compatible version is found" do # set up a mock gem with with a rails dependency that is unsatisfied by the version given mocked_dependency_requirement = double("requirement") @@ -36,11 +72,11 @@ expect(gem_info.state(rails_version)).to eq(:no_new_version) end - end describe "#find_latest_compatible" do let(:mock_gem) { Struct.new(:name, :version) } + it "sets latest_compatible_version to NullGem if no specs are found" do gem = mock_gem.new('gem_name', "0.0.1") @@ -53,6 +89,5 @@ gem_info.find_latest_compatible expect(gem_info.latest_compatible_version).to be_a(NextRails::GemInfo::NullGemInfo) end - end -end \ No newline at end of file +end diff --git a/spec/next_rails_spec.rb b/spec/next_rails_spec.rb index 9a6ca3f..34c0d17 100644 --- a/spec/next_rails_spec.rb +++ b/spec/next_rails_spec.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +require "spec_helper" + require "fileutils" RSpec.describe NextRails do diff --git a/spec/ten_years_rails/gem_info_spec.rb b/spec/ten_years_rails/gem_info_spec.rb deleted file mode 100644 index 77970d0..0000000 --- a/spec/ten_years_rails/gem_info_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require "spec_helper" -require "timecop" - -RSpec.describe NextRails::GemInfo do - let(:release_date) { Time.utc(2019, 7, 6, 0, 0, 0) } - let(:now) { Time.utc(2019, 7, 6, 12, 0, 0) } - let(:spec) do - Gem::Specification.new do |s| - s.date = release_date - s.version = "1.0.0" - end - end - - subject { NextRails::GemInfo.new(spec) } - - describe "#age" do - around do |example| - Timecop.travel(now) do - example.run - end - end - - let(:result) { now.strftime("%b %e, %Y") } - - it "returns a date" do - expect(subject.age).to eq(result) - end - end - - describe "#up_to_date?" do - it "is up to date" do - allow(Gem).to receive(:latest_spec_for).and_return(spec) - expect(subject.up_to_date?).to be_truthy - end - end -end