From af0417bb414032b4f11915e6844b6dc4552b7443 Mon Sep 17 00:00:00 2001 From: Luke Hill Date: Fri, 3 May 2019 10:32:19 +0200 Subject: [PATCH] Add unit tests for new config functionality --- lib/cucumber/glue/registry_and_more.rb | 4 +- spec/cucumber/glue/registry_and_more_spec.rb | 96 +++++++++++++++++--- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/lib/cucumber/glue/registry_and_more.rb b/lib/cucumber/glue/registry_and_more.rb index 66af21ee39..059dde2d7d 100644 --- a/lib/cucumber/glue/registry_and_more.rb +++ b/lib/cucumber/glue/registry_and_more.rb @@ -105,9 +105,7 @@ def load_code_file(code_file) # This will cause self.add_step_definition, self.add_hook, and self.define_parameter_type to be called from Glue::Dsl - load_files_once = @configuration.only_load_files_once || Cucumber.only_load_files_once - - if load_files_once + if Cucumber.only_load_files_once require File.expand_path(code_file) else load File.expand_path(code_file) diff --git a/spec/cucumber/glue/registry_and_more_spec.rb b/spec/cucumber/glue/registry_and_more_spec.rb index 10a30ec31a..5cf7cd0f80 100644 --- a/spec/cucumber/glue/registry_and_more_spec.rb +++ b/spec/cucumber/glue/registry_and_more_spec.rb @@ -29,29 +29,97 @@ def a_file_called(name) end end - it 're-loads the file when called multiple times' do - a_file_called('tmp.rb') do - '$foo = 1' + context 'by default' do + before(:each) { $foo = nil } + + it 're-loads the file when called multiple times' do + a_file_called('tmp.rb') do + '$foo = 1' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 1 + + a_file_called('tmp.rb') do + '$foo = 2' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 2 end - registry.load_code_file('tmp.rb') - expect($foo).to eq 1 + it 'only loads ruby files' do + a_file_called('docs.md') do + '$foo = 1' + end - a_file_called('tmp.rb') do - '$foo = 2' + registry.load_code_file('docs.md') + expect($foo).to be nil end + end - registry.load_code_file('tmp.rb') - expect($foo).to eq 2 + context 'With `only_load_files_once` set to false' do + before(:each) do + allow(Cucumber).to receive(:only_load_files_once).and_return(false) + $foo = nil + end + + it 're-loads the file when called multiple times' do + a_file_called('tmp.rb') do + '$foo = 1' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 1 + + a_file_called('tmp.rb') do + '$foo = 2' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 2 + end + + it 'only loads ruby files' do + a_file_called('docs.md') do + '$foo = 1' + end + + registry.load_code_file('docs.md') + expect($foo).to be nil + end end - it 'only loads ruby files' do - a_file_called('docs.md') do - '$foo = 1' + context 'With `only_load_files_once` set to true' do + before(:each) do + allow(Cucumber).to receive(:only_load_files_once).and_return(true) + $foo = nil + end + + it 'does not re-load the file when called multiple times' do + a_file_called('tmp.rb') do + '$foo = 1' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 1 + + a_file_called('tmp.rb') do + '$foo = 2' + end + + registry.load_code_file('tmp.rb') + expect($foo).to eq 1 end - registry.load_code_file('docs.md') - expect($foo).to be nil + it 'only loads ruby files' do + a_file_called('docs.md') do + '$foo = 1' + end + + registry.load_code_file('docs.md') + expect($foo).to be nil + end end # rubocop:enable Style/GlobalVars end