From 012460d285b90e23d82ad7e5763cde98a0eda075 Mon Sep 17 00:00:00 2001 From: Zakir Date: Wed, 10 Jul 2024 20:13:43 +0300 Subject: [PATCH] Run RSpec tests on rails and non-rails environments (#19) --- Gemfile | 1 + Rakefile | 4 +- .../{ => non_rails}/uber_task/current_spec.rb | 0 spec/{ => non_rails}/uber_task/error_spec.rb | 0 .../uber_task/event_handled_spec.rb | 0 spec/{ => non_rails}/uber_task/event_spec.rb | 0 .../uber_task}/exception_spec.rb | 0 .../non_rails/uber_task/internal/path_spec.rb | 26 +++++++++ .../uber_task/internal_spec.rb | 0 .../uber_task/location_spec.rb | 0 spec/{ => non_rails}/uber_task/logger_spec.rb | 18 ++---- .../uber_task/retry_info_spec.rb | 0 .../uber_task/retry_task_spec.rb | 0 spec/{ => non_rails}/uber_task/run_spec.rb | 0 spec/{ => non_rails}/uber_task/skip_spec.rb | 0 .../uber_task/skip_task_spec.rb | 0 spec/on_rails/support/tasks/rails.rake | 25 +++++++++ spec/on_rails/uber_task/internal/path_spec.rb | 28 ++++++++++ spec/on_rails/uber_task/logger_spec.rb | 17 ++++++ spec/rspec_settings.rb | 23 ++++++++ spec/uber_task/internal/path_spec.rb | 56 ------------------- 21 files changed, 127 insertions(+), 71 deletions(-) rename spec/{ => non_rails}/uber_task/current_spec.rb (100%) rename spec/{ => non_rails}/uber_task/error_spec.rb (100%) rename spec/{ => non_rails}/uber_task/event_handled_spec.rb (100%) rename spec/{ => non_rails}/uber_task/event_spec.rb (100%) rename spec/{ => non_rails/uber_task}/exception_spec.rb (100%) create mode 100644 spec/non_rails/uber_task/internal/path_spec.rb rename spec/{ => non_rails}/uber_task/internal_spec.rb (100%) rename spec/{ => non_rails}/uber_task/location_spec.rb (100%) rename spec/{ => non_rails}/uber_task/logger_spec.rb (53%) rename spec/{ => non_rails}/uber_task/retry_info_spec.rb (100%) rename spec/{ => non_rails}/uber_task/retry_task_spec.rb (100%) rename spec/{ => non_rails}/uber_task/run_spec.rb (100%) rename spec/{ => non_rails}/uber_task/skip_spec.rb (100%) rename spec/{ => non_rails}/uber_task/skip_task_spec.rb (100%) create mode 100644 spec/on_rails/support/tasks/rails.rake create mode 100644 spec/on_rails/uber_task/internal/path_spec.rb create mode 100644 spec/on_rails/uber_task/logger_spec.rb delete mode 100644 spec/uber_task/internal/path_spec.rb diff --git a/Gemfile b/Gemfile index 07e5742..560cc00 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ group :development do end group :test do + gem 'rails', '~> 7.1.1' gem 'rspec', '~> 3.2' gem 'simplecov', '~> 0.21' end diff --git a/Rakefile b/Rakefile index 4f957ff..ce06502 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,7 @@ require 'bundler/gem_tasks' require 'rubocop/rake_task' require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) RuboCop::RakeTask.new +RSpec::Core::RakeTask.new(:spec) -task default: %i[spec rubocop] +task default: %i[rubocop spec] diff --git a/spec/uber_task/current_spec.rb b/spec/non_rails/uber_task/current_spec.rb similarity index 100% rename from spec/uber_task/current_spec.rb rename to spec/non_rails/uber_task/current_spec.rb diff --git a/spec/uber_task/error_spec.rb b/spec/non_rails/uber_task/error_spec.rb similarity index 100% rename from spec/uber_task/error_spec.rb rename to spec/non_rails/uber_task/error_spec.rb diff --git a/spec/uber_task/event_handled_spec.rb b/spec/non_rails/uber_task/event_handled_spec.rb similarity index 100% rename from spec/uber_task/event_handled_spec.rb rename to spec/non_rails/uber_task/event_handled_spec.rb diff --git a/spec/uber_task/event_spec.rb b/spec/non_rails/uber_task/event_spec.rb similarity index 100% rename from spec/uber_task/event_spec.rb rename to spec/non_rails/uber_task/event_spec.rb diff --git a/spec/exception_spec.rb b/spec/non_rails/uber_task/exception_spec.rb similarity index 100% rename from spec/exception_spec.rb rename to spec/non_rails/uber_task/exception_spec.rb diff --git a/spec/non_rails/uber_task/internal/path_spec.rb b/spec/non_rails/uber_task/internal/path_spec.rb new file mode 100644 index 0000000..dc1a66c --- /dev/null +++ b/spec/non_rails/uber_task/internal/path_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'pathname' + +describe UberTask::Internal::Path do + describe '.shorten' do + let(:paths) do + [ + '/foo/bar/baz/gems/bar', + '/foo/bar/rubygems/baz', + ] + end + + it 'shortens paths' do + shortened = paths.map do |path| + described_class.shorten(path) + end + expect(shortened).to eq( + %w[ + [GEM]/bar + [GEM]/baz + ], + ) + end + end +end diff --git a/spec/uber_task/internal_spec.rb b/spec/non_rails/uber_task/internal_spec.rb similarity index 100% rename from spec/uber_task/internal_spec.rb rename to spec/non_rails/uber_task/internal_spec.rb diff --git a/spec/uber_task/location_spec.rb b/spec/non_rails/uber_task/location_spec.rb similarity index 100% rename from spec/uber_task/location_spec.rb rename to spec/non_rails/uber_task/location_spec.rb diff --git a/spec/uber_task/logger_spec.rb b/spec/non_rails/uber_task/logger_spec.rb similarity index 53% rename from spec/uber_task/logger_spec.rb rename to spec/non_rails/uber_task/logger_spec.rb index 14f87dc..7d9e548 100644 --- a/spec/uber_task/logger_spec.rb +++ b/spec/non_rails/uber_task/logger_spec.rb @@ -6,20 +6,12 @@ end describe '.logger' do - it 'memoizes variable' do - initialized_logger = described_class.logger + subject(:logger) { described_class.logger } - expect(described_class.logger.object_id).to eq( - initialized_logger.object_id, - ) - end - - context 'when Rails logger is not defined' do - it 'uses stdout logger by default' do - expect do - described_class.logger.info('some message') - end.to output(/some message/).to_stdout - end + it 'uses stdout logger by default' do + expect do + logger.info('some message') + end.to output(/some message/).to_stdout end end diff --git a/spec/uber_task/retry_info_spec.rb b/spec/non_rails/uber_task/retry_info_spec.rb similarity index 100% rename from spec/uber_task/retry_info_spec.rb rename to spec/non_rails/uber_task/retry_info_spec.rb diff --git a/spec/uber_task/retry_task_spec.rb b/spec/non_rails/uber_task/retry_task_spec.rb similarity index 100% rename from spec/uber_task/retry_task_spec.rb rename to spec/non_rails/uber_task/retry_task_spec.rb diff --git a/spec/uber_task/run_spec.rb b/spec/non_rails/uber_task/run_spec.rb similarity index 100% rename from spec/uber_task/run_spec.rb rename to spec/non_rails/uber_task/run_spec.rb diff --git a/spec/uber_task/skip_spec.rb b/spec/non_rails/uber_task/skip_spec.rb similarity index 100% rename from spec/uber_task/skip_spec.rb rename to spec/non_rails/uber_task/skip_spec.rb diff --git a/spec/uber_task/skip_task_spec.rb b/spec/non_rails/uber_task/skip_task_spec.rb similarity index 100% rename from spec/uber_task/skip_task_spec.rb rename to spec/non_rails/uber_task/skip_task_spec.rb diff --git a/spec/on_rails/support/tasks/rails.rake b/spec/on_rails/support/tasks/rails.rake new file mode 100644 index 0000000..13b5f0c --- /dev/null +++ b/spec/on_rails/support/tasks/rails.rake @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +DUMMY_RAILS_APP_PATH = 'tmp/test/dummy/' + +namespace :rails do + desc 'Generate dummy rails application' + task :generate_dummy_app, [:app_path] do |_t, args| + app_path = args.fetch(:app_path, DUMMY_RAILS_APP_PATH) + + if Dir.exist?(app_path) + puts "Using Rails application at #{app_path} for tests..." + next + end + + puts "Generating dummy Rails application at #{app_path}..." + system( + "rails new #{app_path} " \ + '--skip-git --skip-asset-pipeline --skip-action-cable ' \ + '--skip-action-mailer --skip-action-mailbox --skip-action-text ' \ + '--skip-active-record --skip-active-job --skip-active-storage ' \ + '--skip-javascript --skip-hotwire --skip-jbuilder ' \ + '--skip-test --skip-system-test --skip-bootsnap ', + ) + end +end diff --git a/spec/on_rails/uber_task/internal/path_spec.rb b/spec/on_rails/uber_task/internal/path_spec.rb new file mode 100644 index 0000000..ddddfdc --- /dev/null +++ b/spec/on_rails/uber_task/internal/path_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'pathname' + +describe UberTask::Internal::Path do + describe '.shorten' do + let(:paths) do + [ + Rails.root.join('abc').to_s, + '/foo/bar/baz/gems/bar', + '/foo/bar/rubygems/baz', + ] + end + + it 'shortens paths' do + shortened = paths.map do |path| + described_class.shorten(path) + end + expect(shortened).to eq( + %w[ + [PROJECT]/abc + [GEM]/bar + [GEM]/baz + ], + ) + end + end +end diff --git a/spec/on_rails/uber_task/logger_spec.rb b/spec/on_rails/uber_task/logger_spec.rb new file mode 100644 index 0000000..f4d12d9 --- /dev/null +++ b/spec/on_rails/uber_task/logger_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +describe UberTask do + before do + described_class.instance_variable_set(:@logger, nil) + end + + describe '.logger' do + subject(:logger) { described_class.logger } + + it 'uses Rails logger by default' do + expect(Rails.logger).to receive(:info).with('some message') + + logger.info('some message') + end + end +end diff --git a/spec/rspec_settings.rb b/spec/rspec_settings.rb index 031370b..77b2d31 100644 --- a/spec/rspec_settings.rb +++ b/spec/rspec_settings.rb @@ -3,6 +3,7 @@ require 'simplecov' SimpleCov.start do add_filter '/spec/' + add_filter %r{^/tmp/} end require 'uber_task' @@ -12,4 +13,26 @@ config.expect_with :rspec do |c| c.syntax = :expect end + + config.define_derived_metadata(file_path: %r{spec/on_rails}) do |metadata| + metadata[:on_rails] = true + end + + config.before(:example, :on_rails) do + next if defined?(Rails) + + require 'rake' + load File.expand_path('on_rails/support/tasks/rails.rake', __dir__) + + rails_app_path = File.expand_path('../tmp/test/dummy/', __dir__) + Rake::Task['rails:generate_dummy_app'].invoke(rails_app_path) + + require File.expand_path( + File.join(rails_app_path, '/config/environment.rb'), + __dir__, + ) + + ENV['RAILS_ROOT'] = rails_app_path + raise 'Failed to load Rails application' unless defined?(Rails) + end end diff --git a/spec/uber_task/internal/path_spec.rb b/spec/uber_task/internal/path_spec.rb deleted file mode 100644 index dd95db7..0000000 --- a/spec/uber_task/internal/path_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -# frozen_string_literal: true - -require 'pathname' - -describe UberTask::Internal::Path do - describe '.shorten' do - context 'normal' do - let(:paths) do - [ - '/foo/bar/baz/gems/bar', - '/foo/bar/rubygems/baz', - ] - end - - it 'shortens paths' do - shortened = paths.map do |path| - described_class.shorten(path) - end - expect(shortened).to eq( - %w[ - [GEM]/bar - [GEM]/baz - ], - ) - end - end - - context 'in rails' do - let(:rails_root) { Pathname.new(Dir.pwd) } - let(:paths) do - [ - rails_root.join('abc').to_s, - '/foo/bar/baz/gems/bar', - '/foo/bar/rubygems/baz', - ] - end - - before do - stub_const('Rails', double(root: rails_root)) - end - - it 'shortens paths' do - shortened = paths.map do |path| - described_class.shorten(path) - end - expect(shortened).to eq( - %w[ - [PROJECT]/abc - [GEM]/bar - [GEM]/baz - ], - ) - end - end - end -end