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 9, 2024
1 parent d92f0da commit e5d5231
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 29 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/rspec-shared.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: RSpec Shared

on:
workflow_call:
inputs:
rspec-task:
required: true
type: string

jobs:
rspec:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.2"
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run tests
env:
RAILS_ENV: test
run: bundle exec rake ${{ inputs.rspec-task }}
- name: Upload coverage results
uses: actions/upload-artifact@master
if: always()
with:
name: coverage-report-${{ inputs.rspec-task }}-${{ github.run_id }}-ubuntu-latest-ruby-3.2
path: coverage
35 changes: 12 additions & 23 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@ on:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
rspec:
runs-on: ubuntu-latest
name: RSpec
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.2"
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run tests
env:
RAILS_ENV: test
run: bundle exec rspec --format documentation
- name: Upload coverage results
uses: actions/upload-artifact@master
if: always()
with:
name: coverage-report-${{ github.run_id }}-ubuntu-latest-ruby-3.2
path: coverage
non-rails-rspec:
name: RSpec (non-rails)
uses: ./.github/workflows/rspec-shared.yml
with:
rspec-task: non_rails_spec

rails-rspec:
name: RSpec (on rails)
uses: ./.github/workflows/rspec-shared.yml
with:
rspec-task: rails_spec
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
43 changes: 41 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,46 @@ 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[rubocop spec]

desc 'Run tests on non-rails environment'
task :non_rails_spec do
ENV.delete('RAILS_APP_PATH')

Rake::Task[:spec].invoke
end

DUMMY_RAILS_APP_PATH = 'tmp/test/dummy/'

desc 'Run tests on rails environment'
task :rails_spec do
ENV['RAILS_APP_PATH'] = File.expand_path(DUMMY_RAILS_APP_PATH, __dir__)

Rake::Task['rails:generate_dummy_app'].invoke(ENV.fetch('RAILS_APP_PATH'))
Rake::Task[:spec].invoke
end

namespace :rails do
desc 'Generate dummy rails application'
task :generate_dummy_app, [:app_path] do |_t, args|
app_path = args.fetch(:app_path)

if Dir.exist?(app_path)
puts "Using Rails application at #{app_path} as a dummy..."
next
end

task default: %i[spec rubocop]
puts 'Generating dummy Rails application...'
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
17 changes: 17 additions & 0 deletions spec/rspec_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@
require 'simplecov'
SimpleCov.start do
add_filter '/spec/'
add_filter %r{^/tmp/}
end

require 'uber_task'
require 'colorize'

rails_app_path = ENV.fetch('RAILS_APP_PATH', nil)

if rails_app_path
unless Dir.exist?(rails_app_path)
raise "Rails application at #{rails_app_path} doesn't exit"
end

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

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
Expand Down
4 changes: 2 additions & 2 deletions spec/uber_task/internal/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

context 'in rails' do
let(:rails_root) { Pathname.new(Dir.pwd) }
let(:rails_root) { defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd) }
let(:paths) do
[
rails_root.join('abc').to_s,
Expand All @@ -36,7 +36,7 @@
end

before do
stub_const('Rails', double(root: rails_root))
stub_const('Rails', double(root: rails_root)) unless defined?(Rails)
end

it 'shortens paths' do
Expand Down
8 changes: 6 additions & 2 deletions spec/uber_task/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
)
end

context 'when Rails logger is not defined' do
it 'uses stdout logger by default' do
it 'uses Rails or stdout logger by default' do
if defined?(Rails)
expect(Rails.logger).to receive(:info).with('some message')

described_class.logger.info('some message')
else
expect do
described_class.logger.info('some message')
end.to output(/some message/).to_stdout
Expand Down

0 comments on commit e5d5231

Please sign in to comment.