Skip to content

Commit

Permalink
Run RSpec tests on rails and non-rails environments
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr committed Jul 10, 2024
1 parent 1c3efc4 commit 876fb76
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 71 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions spec/non_rails/uber_task/internal/path_spec.rb
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions spec/on_rails/support/tasks/rails.rake
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions spec/on_rails/uber_task/internal/path_spec.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/on_rails/uber_task/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/rspec_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'simplecov'
SimpleCov.start do
add_filter '/spec/'
add_filter %r{^/tmp/}
end

require 'uber_task'
Expand All @@ -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
56 changes: 0 additions & 56 deletions spec/uber_task/internal/path_spec.rb

This file was deleted.

0 comments on commit 876fb76

Please sign in to comment.