From ee96de5a945ebf3957c6bdd3c4a74924247b97ae Mon Sep 17 00:00:00 2001 From: Gavin Didrichsen Date: Fri, 17 Nov 2023 10:00:34 +0000 Subject: [PATCH] Squashed commit of the following: commit ddffa6c5fd3d256e02f26f043e83e99b4093e8bb Author: Code Manager Date: Tue Oct 10 13:11:34 2023 +0000 Fix undefined local 'path' Fixed failing tests after refactoring around(:all) into before/after(:all): ``Failure/Error: FileUtils.rm_f(path) NameError: undefined local variable or method `path'...`` commit 10821c54a280dd3a3cfb4a3556bcef409b40d9f2 Author: Gavin Didrichsen Date: Thu Oct 5 21:00:17 2023 +0100 (CAT-1488) - Fix some rubocop warnings Signed-off-by: Gavin Didrichsen commit 831c41da83631ef334d3736b18ff606a327b52f6 Author: Gavin Didrichsen Date: Thu Oct 5 20:34:03 2023 +0100 (CAT-1488) - Split around(:all) into before/after(:all) This fixes duplicate code warnings occurring during the acceptance tests. Signed-off-by: Gavin Didrichsen commit d2cec4bada6775d221ad26a00743de521ec29721 Author: Gavin Didrichsen Date: Thu Oct 5 20:32:51 2023 +0100 (CAT-1488) - Refactor duplicated shared examples and context This fixes duplicate definition warnings in the acceptance tests. Signed-off-by: Gavin Didrichsen --- spec/acceptance/remove_config_spec.rb | 41 ------------------- spec/acceptance/set_config_spec.rb | 41 ------------------- .../support/a_saved_configuration_file.rb | 10 +++++ .../a_saved_json_configuration_file.rb | 11 +++++ .../support/with_a_fake_answer_file.rb | 17 ++++++++ spec/acceptance/test_unit_spec.rb | 8 ++-- 6 files changed, 43 insertions(+), 85 deletions(-) create mode 100644 spec/acceptance/support/a_saved_configuration_file.rb create mode 100644 spec/acceptance/support/a_saved_json_configuration_file.rb create mode 100644 spec/acceptance/support/with_a_fake_answer_file.rb diff --git a/spec/acceptance/remove_config_spec.rb b/spec/acceptance/remove_config_spec.rb index 46da7276f..c69951cef 100644 --- a/spec/acceptance/remove_config_spec.rb +++ b/spec/acceptance/remove_config_spec.rb @@ -4,47 +4,6 @@ describe 'pdk remove config' do include_context 'with a fake TTY' - shared_examples 'a saved configuration file' do |new_content| - it 'saves the setting' do - # Force the command to run if not already - subject.exit_status - expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) - - actual_content = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) - expect(actual_content).to eq(new_content) - end - end - - shared_examples 'a saved JSON configuration file' do |new_json_content| - it 'saves the setting' do - # Force the command to run if not already - subject.exit_status - expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) - - actual_content_raw = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) - actual_json_content = JSON.parse(actual_content_raw) - expect(actual_json_content).to eq(new_json_content) - end - end - - RSpec.shared_context 'with a fake answer file' do |initial_content = nil| - before(:all) do - fake_answer_file = Tempfile.new('mock_answers.json') - unless initial_content.nil? - require 'json' - fake_answer_file.binmode - fake_answer_file.write(JSON.pretty_generate(initial_content)) - end - fake_answer_file.close - ENV['PDK_ANSWER_FILE'] = fake_answer_file.path - end - - after(:all) do - FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here - ENV.delete('PDK_ANSWER_FILE') - end - end - context 'when run outside of a module' do describe command('pdk remove config') do its(:exit_status) { is_expected.not_to eq 0 } diff --git a/spec/acceptance/set_config_spec.rb b/spec/acceptance/set_config_spec.rb index 5b452c142..32d239eba 100644 --- a/spec/acceptance/set_config_spec.rb +++ b/spec/acceptance/set_config_spec.rb @@ -4,47 +4,6 @@ describe 'pdk set config' do include_context 'with a fake TTY' - shared_examples 'a saved configuration file' do |new_content| - it 'saves the setting' do - # Force the command to run if not already - subject.exit_status - expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) - - actual_content = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) - expect(actual_content).to eq(new_content) - end - end - - shared_examples 'a saved JSON configuration file' do |new_json_content| - it 'saves the setting' do - # Force the command to run if not already - subject.exit_status - expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) - - actual_content_raw = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) - actual_json_content = JSON.parse(actual_content_raw) - expect(actual_json_content).to eq(new_json_content) - end - end - - RSpec.shared_context 'with a fake answer file' do |initial_content = nil| - before(:all) do - fake_answer_file = Tempfile.new('mock_answers.json') - unless initial_content.nil? - require 'json' - fake_answer_file.binmode - fake_answer_file.write(JSON.pretty_generate(initial_content)) - end - fake_answer_file.close - ENV['PDK_ANSWER_FILE'] = fake_answer_file.path - end - - after(:all) do - FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here - ENV.delete('PDK_ANSWER_FILE') - end - end - context 'when run outside of a module' do describe command('pdk set config') do its(:exit_status) { is_expected.not_to eq 0 } diff --git a/spec/acceptance/support/a_saved_configuration_file.rb b/spec/acceptance/support/a_saved_configuration_file.rb new file mode 100644 index 000000000..9f215a838 --- /dev/null +++ b/spec/acceptance/support/a_saved_configuration_file.rb @@ -0,0 +1,10 @@ +RSpec.shared_examples 'a saved configuration file' do |new_content| + it 'saves the setting' do + # Force the command to run if not already + subject.exit_status + expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) + + actual_content = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) + expect(actual_content).to eq(new_content) + end +end diff --git a/spec/acceptance/support/a_saved_json_configuration_file.rb b/spec/acceptance/support/a_saved_json_configuration_file.rb new file mode 100644 index 000000000..f5f34be14 --- /dev/null +++ b/spec/acceptance/support/a_saved_json_configuration_file.rb @@ -0,0 +1,11 @@ +RSpec.shared_examples 'a saved JSON configuration file' do |new_json_content| + it 'saves the setting' do + # Force the command to run if not already + subject.exit_status + expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil)) + + actual_content_raw = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read) + actual_json_content = JSON.parse(actual_content_raw) + expect(actual_json_content).to eq(new_json_content) + end +end diff --git a/spec/acceptance/support/with_a_fake_answer_file.rb b/spec/acceptance/support/with_a_fake_answer_file.rb new file mode 100644 index 000000000..478c96a8b --- /dev/null +++ b/spec/acceptance/support/with_a_fake_answer_file.rb @@ -0,0 +1,17 @@ +RSpec.shared_context 'with a fake answer file' do |initial_content = nil| + before(:all) do + fake_answer_file = Tempfile.new('mock_answers.json') + unless initial_content.nil? + require 'json' + fake_answer_file.binmode + fake_answer_file.write(JSON.pretty_generate(initial_content)) + end + fake_answer_file.close + ENV['PDK_ANSWER_FILE'] = fake_answer_file.path + end + + after(:all) do + FileUtils.rm_f(ENV.fetch('PDK_ANSWER_FILE', nil)) # Need actual file calls here + ENV.delete('PDK_ANSWER_FILE') + end +end diff --git a/spec/acceptance/test_unit_spec.rb b/spec/acceptance/test_unit_spec.rb index 97b175dd9..defcd636a 100644 --- a/spec/acceptance/test_unit_spec.rb +++ b/spec/acceptance/test_unit_spec.rb @@ -5,11 +5,13 @@ include_context 'with a fake TTY' shared_context 'with spec file' do |filename, content| - around(:all) do |example| - path = File.join('spec', 'unit', filename) + path = File.join('spec', 'unit', filename) + before(:all) do FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') { |f| f.puts content } - example.run + end + + after(:all) do FileUtils.rm_f(path) end end