-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/gem_tasks' | ||
require 'rubocop/rake_task' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
RuboCop::RakeTask.new | ||
task default: %i[rubocop spec] | ||
|
||
task default: %i[spec rubocop] | ||
desc 'Run RuboCop' | ||
task :rubocop do | ||
require 'rubocop/rake_task' | ||
|
||
RuboCop::RakeTask.new | ||
end | ||
|
||
desc 'Run tests' | ||
task :spec do | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
end | ||
|
||
RAILS_DUMMY_DIR = 'tmp/test/dummy/' | ||
|
||
namespace :rails do | ||
desc 'Generate dummy rails application' | ||
task :generate_dummy_app, [:dir] do |_t, args| | ||
dummy_app_dir = args.fetch(:dir, RAILS_DUMMY_DIR) | ||
exit(0) if Dir.exist?(dummy_app_dir) | ||
|
||
system( | ||
"rails new #{dummy_app_dir} " \ | ||
'--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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rake' | ||
|
||
class RailsManager | ||
APP_PATH = '../../tmp/test/dummy/' | ||
|
||
attr_reader :config, :app_path | ||
|
||
def initialize(config, app_path: APP_PATH) | ||
@config = config | ||
@app_path = app_path | ||
end | ||
|
||
def on_rails | ||
load_rails! | ||
yield | ||
unload_rails! | ||
end | ||
|
||
private | ||
|
||
def load_rails! | ||
return if defined?(Rails) | ||
|
||
if config.instance_variable_defined?(:@rails) | ||
Object.const_set(:Rails, config.instance_variable_get(:@rails)) | ||
return | ||
end | ||
|
||
absolute_app_path = File.expand_path(app_path, __dir__) | ||
system("rake rails:generate_dummy_app[#{absolute_app_path}]") | ||
|
||
require File.expand_path( | ||
File.join(absolute_app_path, '/config/environment.rb'), | ||
__dir__, | ||
) | ||
|
||
ENV['RAILS_ROOT'] = absolute_app_path | ||
config.instance_variable_set(:@rails, Object.const_get(:Rails)) | ||
end | ||
|
||
def unload_rails! | ||
ENV.delete('RAILS_ROOT') | ||
Object.send(:remove_const, :Rails) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'tmpdir' | ||
|
||
describe RailsManager do | ||
let(:manager) { described_class.new(config, app_path: app_path) } | ||
|
||
let(:config) { double } | ||
let(:app_path) { File.join(tmp_dir, 'test/dummy') } | ||
|
||
after do | ||
FileUtils.remove_entry(tmp_dir) | ||
end | ||
|
||
describe '#on_rails', skip: 'Conflicts with manager in rspec_settings' do | ||
it 'runs isolated code as if on Rails application' do | ||
### 1st run | ||
# | ||
expect(Dir.exist?(app_path)).to eq false | ||
expect(defined?(Rails)).to be_nil | ||
expect(config.instance_variable_defined?(:@rails)).to eq false | ||
|
||
manager.on_rails do | ||
expect(defined?(Rails)).not_to be_nil | ||
end | ||
|
||
expect(Dir.exist?(app_path)).to eq true | ||
expect(defined?(Rails)).to be_nil | ||
expect(config.instance_variable_defined?(:@rails)).to eq true | ||
|
||
### 2nd run | ||
# | ||
manager.on_rails do | ||
expect(defined?(Rails)).not_to be_nil | ||
expect(Rails).to eq config.instance_variable_get(:@rails) | ||
end | ||
|
||
expect(Dir.exist?(app_path)).to eq true | ||
expect(defined?(Rails)).to be_nil | ||
expect(config.instance_variable_defined?(:@rails)).to eq true | ||
end | ||
end | ||
|
||
def tmp_dir | ||
@tmp_dir ||= Dir.mktmpdir | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters