Skip to content

Commit

Permalink
(CAT-1488) - Refactor duplicated shared examples and context
Browse files Browse the repository at this point in the history
This fixes duplicate definition warnings in the acceptance tests.

Signed-off-by: Gavin Didrichsen <[email protected]>
  • Loading branch information
gavindidrichsen committed Oct 5, 2023
1 parent a0d85d2 commit c675464
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 79 deletions.
38 changes: 0 additions & 38 deletions spec/acceptance/remove_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,8 @@
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
Expand Down
41 changes: 0 additions & 41 deletions spec/acceptance/set_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions spec/acceptance/support/a_saved_configuration_file.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/acceptance/support/a_saved_json_configuration_file.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/acceptance/support/with_a_fake_answer_file.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c675464

Please sign in to comment.