Skip to content

Commit

Permalink
Factory reload on clean to allow usage of sequences (#95)
Browse files Browse the repository at this point in the history
instead of reloading between every request
  • Loading branch information
grantspeelman authored Jan 3, 2022
1 parent b6a00a7 commit d870d4e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.10.1...main

### Changed
* only reload factories on clean instead of every factory create request [PR 95](https://github.com/shakacode/cypress-on-rails/pull/95)
* alternative command added for get tail of logs [PR 89](https://github.com/shakacode/cypress-on-rails/pull/89) by [ccrockett]

### Tasks
Expand Down
2 changes: 1 addition & 1 deletion lib/cypress_on_rails/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def handle_command(req)
body = JSON.parse(req.body.read)
logger.info "handle_command: #{body}"
commands = Command.from_body(body, configuration)
missing_command = commands.find {|command| !@file.exists?(command.file_path) }
missing_command = commands.find {|command| !@file.exist?(command.file_path) }

if missing_command.nil?
begin
Expand Down
30 changes: 19 additions & 11 deletions lib/cypress_on_rails/smart_factory_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def self.build_list(*args)
instance.build_list(*args)
end

def self.reload
instance.reload
end

# @return [Array]
attr_accessor :factory
attr_accessor :always_reload
Expand All @@ -44,7 +48,7 @@ def initialize(files:, factory:, always_reload: false,
end

def create(*options)
load_files
auto_reload
factory_name = options.shift
if options.last.is_a?(Hash)
args = options.pop
Expand All @@ -55,12 +59,12 @@ def create(*options)
end

def create_list(*args)
load_files
auto_reload
factory.create_list(*args)
end

def build(*options)
load_files
auto_reload
factory_name = options.shift
if options.last.is_a?(Hash)
args = options.pop
Expand All @@ -71,10 +75,19 @@ def build(*options)
end

def build_list(*args)
load_files
auto_reload
factory.build_list(*args)
end

def reload
logger.info 'Loading Factories'
factory.reload
files.each do |file|
logger.debug "-- Loading: #{file}"
@kernel.load(file)
end
end

private

# @param [String,Array] arg
Expand All @@ -92,16 +105,11 @@ def logger
CypressOnRails.configuration.logger
end

def load_files
def auto_reload
current_latest_mtime = files.map{|file| @file_system.mtime(file) }.max
return unless should_reload?(current_latest_mtime)
logger.info 'Loading Factories'
@latest_mtime = current_latest_mtime
factory.reload
files.each do |file|
logger.debug "-- Loading: #{file}"
@kernel.load(file)
end
reload
end

def should_reload?(current_latest_mtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
Post.delete_all if defined?(Post)
end

CypressOnRails::SmartFactoryWrapper.reload

Rails.logger.info "APPCLEANED" # used by log_fail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
factory = FactoryGirl if defined?(FactoryGirl)

CypressOnRails::SmartFactoryWrapper.configure(
always_reload: !Rails.configuration.cache_classes,
always_reload: false,
factory: factory,
files: [
Rails.root.join('spec', 'factories.rb'),
Expand Down
24 changes: 12 additions & 12 deletions spec/cypress_on_rails/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def rack_input(json_value)
context '/__cypress__/command' do
before do
allow(command_executor).to receive(:perform).and_return({ id: 1, title: 'some result' })
allow(file).to receive(:exists?)
allow(file).to receive(:exist?)
env['PATH_INFO'] = '/__cypress__/command'
end

it 'command file exists' do
it 'command file exist' do
allow(command_executor).to receive(:perform).and_return({ id: 1, title: 'some result' })
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exists?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
Expand All @@ -34,9 +34,9 @@ def rack_input(json_value)
end
end

it 'command file exists with options' do
it 'command file exist with options' do
env['rack.input'] = rack_input(name: 'seed', options: ['my_options'])
allow(file).to receive(:exists?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
Expand All @@ -46,11 +46,11 @@ def rack_input(json_value)
end
end

it 'command file does not exists' do
it 'command file does not exist' do
object = BasicObject.new
allow(command_executor).to receive(:perform).and_return(object)
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exists?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
Expand All @@ -62,7 +62,7 @@ def rack_input(json_value)

it 'command result does not respond to to_json' do
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exists?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
Expand All @@ -75,8 +75,8 @@ def rack_input(json_value)
it 'running multiple commands' do
env['rack.input'] = rack_input([{name: 'load_user'},
{name: 'load_sample', options: {'all' => 'true'}}])
allow(file).to receive(:exists?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exists?).with('spec/cypress/app_commands/load_sample.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_sample.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
Expand All @@ -89,8 +89,8 @@ def rack_input(json_value)

it 'running multiple commands but one missing' do
env['rack.input'] = rack_input([{name: 'load_user'}, {name: 'load_sample'}])
allow(file).to receive(:exists?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exists?).with('spec/cypress/app_commands/load_sample.rb').and_return(false)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_sample.rb').and_return(false)

aggregate_failures do
expect(response).to eq([404, {}, ['could not find command file: spec/cypress/app_commands/load_sample.rb']])
Expand Down
7 changes: 7 additions & 0 deletions spec/cypress_on_rails/smart_factory_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def mtime(filename)
expect(kernel_double).to have_received(:load).with('file2.rb').twice
end

it 'can manually reload' do
subject.reload
expect(factory_double).to have_received(:reload)
expect(kernel_double).to have_received(:load).with('file1.rb')
expect(kernel_double).to have_received(:load).with('file2.rb')
end

context 'files is a string' do
let(:files) { 'file*.rb' }

Expand Down
3 changes: 2 additions & 1 deletion spec/integrations/rails_5_2/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ cd $DIR

echo '-- bundle install'
bundle --version
bundle install --quiet --gemfile="$DIR/Gemfile" --retry 2 --path vendor/bundle
bundle config set --local path 'vendor/bundle'
bundle install --quiet --gemfile="$DIR/Gemfile" --retry 2

echo '-- migration'
bundle exec ./bin/rails db:drop || true
Expand Down

0 comments on commit d870d4e

Please sign in to comment.