From 4624a81100099a224f083e5fdba684d869ded7d7 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 10 Feb 2014 16:45:06 +0000 Subject: [PATCH 1/2] Refactor actions specs with shared examples --- spec/producer/core/action_spec.rb | 42 +---------------- spec/producer/core/actions/echo_spec.rb | 2 + .../producer/core/actions/file_writer_spec.rb | 2 + spec/producer/core/actions/mkdir_spec.rb | 2 + .../core/actions/shell_command_spec.rb | 2 + spec/support/shared_action.rb | 45 +++++++++++++++++++ 6 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 spec/support/shared_action.rb diff --git a/spec/producer/core/action_spec.rb b/spec/producer/core/action_spec.rb index fbe780b..884c4ad 100644 --- a/spec/producer/core/action_spec.rb +++ b/spec/producer/core/action_spec.rb @@ -2,46 +2,6 @@ require 'spec_helper' module Producer::Core describe Action do - let(:input) { StringIO.new } - let(:output) { StringIO.new } - let(:env) { Env.new(input: input, output: output) } - let(:arguments) { [:some, :arguments] } - subject(:action) { Action.new(env, *arguments) } - - describe '#env' do - it 'returns the assigned env' do - expect(action.env).to be env - end - end - - describe '#arguments' do - it 'returns the assigned arguments' do - expect(action.arguments).to eq arguments - end - end - - describe '#input' do - it 'returns env input' do - expect(action.input).to be input - end - end - - describe '#output' do - it 'returns env output' do - expect(action.output).to be output - end - end - - describe '#remote' do - it 'returns env remote' do - expect(action.remote).to be action.env.remote - end - end - - describe '#fs' do - it 'returns env remote fs' do - expect(action.fs).to be action.env.remote.fs - end - end + it_behaves_like 'action' end end diff --git a/spec/producer/core/actions/echo_spec.rb b/spec/producer/core/actions/echo_spec.rb index 35c8d3d..f48fded 100644 --- a/spec/producer/core/actions/echo_spec.rb +++ b/spec/producer/core/actions/echo_spec.rb @@ -7,6 +7,8 @@ module Producer::Core let(:text) { 'hello' } subject(:echo) { Echo.new(env, text) } + it_behaves_like 'action' + describe '#apply' do it 'writes the given string to env output with a record separator' do echo.apply diff --git a/spec/producer/core/actions/file_writer_spec.rb b/spec/producer/core/actions/file_writer_spec.rb index 4084910..79b7e19 100644 --- a/spec/producer/core/actions/file_writer_spec.rb +++ b/spec/producer/core/actions/file_writer_spec.rb @@ -8,6 +8,8 @@ module Producer::Core let(:content) { 'some_content' } subject(:writer) { FileWriter.new(env, path, content) } + it_behaves_like 'action' + describe '#apply' do it 'writes content to file on remote filesystem' do expect(writer.fs).to receive(:file_write).with(path, content) diff --git a/spec/producer/core/actions/mkdir_spec.rb b/spec/producer/core/actions/mkdir_spec.rb index 6847dc5..811cc92 100644 --- a/spec/producer/core/actions/mkdir_spec.rb +++ b/spec/producer/core/actions/mkdir_spec.rb @@ -7,6 +7,8 @@ module Producer::Core let(:path) { 'some_path' } subject(:mkdir) { Mkdir.new(env, path) } + it_behaves_like 'action' + describe '#apply' do it 'creates directory on remote filesystem' do expect(mkdir.fs).to receive(:mkdir).with(path) diff --git a/spec/producer/core/actions/shell_command_spec.rb b/spec/producer/core/actions/shell_command_spec.rb index 52fd597..79efe61 100644 --- a/spec/producer/core/actions/shell_command_spec.rb +++ b/spec/producer/core/actions/shell_command_spec.rb @@ -8,6 +8,8 @@ module Producer::Core let(:command) { "echo #{command_args}" } subject(:sh) { ShellCommand.new(env, command) } + it_behaves_like 'action' + describe '#apply' do it 'executes the remote command' do expect(sh.remote).to receive(:execute).with(command) diff --git a/spec/support/shared_action.rb b/spec/support/shared_action.rb new file mode 100644 index 0000000..132169a --- /dev/null +++ b/spec/support/shared_action.rb @@ -0,0 +1,45 @@ +module Producer::Core + shared_examples 'action' do + let(:input) { StringIO.new } + let(:output) { StringIO.new } + let(:env) { Env.new(input: input, output: output) } + let(:arguments) { [:some, :arguments] } + subject(:action) { described_class.new(env, *arguments) } + + describe '#env' do + it 'returns the assigned env' do + expect(action.env).to be env + end + end + + describe '#arguments' do + it 'returns the assigned arguments' do + expect(action.arguments).to eq arguments + end + end + + describe '#input' do + it 'returns env input' do + expect(action.input).to be input + end + end + + describe '#output' do + it 'returns env output' do + expect(action.output).to be output + end + end + + describe '#remote' do + it 'returns env remote' do + expect(action.remote).to be action.env.remote + end + end + + describe '#fs' do + it 'returns env remote fs' do + expect(action.fs).to be action.env.remote.fs + end + end + end +end From 91637ca631849fb5bb090aecef01c7674354f209 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 10 Feb 2014 17:33:48 +0000 Subject: [PATCH 2/2] Refactor tests specs with shared examples --- spec/producer/core/test_spec.rb | 78 +----------------- .../producer/core/tests/file_contains_spec.rb | 4 +- spec/producer/core/tests/has_dir_spec.rb | 4 +- spec/producer/core/tests/has_env_spec.rb | 4 +- spec/producer/core/tests/has_file_spec.rb | 4 +- spec/support/shared_test.rb | 81 +++++++++++++++++++ 6 files changed, 86 insertions(+), 89 deletions(-) create mode 100644 spec/support/shared_test.rb diff --git a/spec/producer/core/test_spec.rb b/spec/producer/core/test_spec.rb index 7d09295..b995df8 100644 --- a/spec/producer/core/test_spec.rb +++ b/spec/producer/core/test_spec.rb @@ -2,82 +2,6 @@ require 'spec_helper' module Producer::Core describe Test do - let(:env) { Env.new } - let(:arguments) { [:some, :arguments] } - subject(:test) { Test.new(env, *arguments) } - - describe '#initialize' do - it 'assigns the env' do - expect(test.env).to be env - end - - it 'assigns the arguments' do - expect(test.arguments).to eq arguments - end - - it 'assigns negated as false by default' do - expect(test).to_not be_negated - end - - context 'when negated option is true' do - subject(:test) { described_class.new(env, *arguments, negated: true) } - - it 'assigns negated as true' do - expect(test).to be_negated - end - end - end - - describe '#remote' do - it 'returns env remote' do - expect(test.remote).to be test.env.remote - end - end - - describe '#fs' do - it 'returns env remote fs' do - expect(test.fs).to be test.env.remote.fs - end - end - - describe '#negated?' do - it 'returns false' do - expect(test.negated?).to be false - end - - context 'when test is negated' do - subject(:test) { described_class.new(env, *arguments, negated: true) } - - it 'returns true' do - expect(test.negated?).to be true - end - end - end - - describe '#pass?' do - it 'returns true when #verify is true' do - allow(test).to receive(:verify) { true } - expect(test.pass?).to be true - end - - it 'returns false when #verify is false' do - allow(test).to receive(:verify) { false } - expect(test.pass?).to be false - end - - context 'when test is negated' do - subject(:test) { described_class.new(env, *arguments, negated: true) } - - it 'returns false when #verify is true' do - allow(test).to receive(:verify) { true } - expect(test.pass?).to be false - end - - it 'returns true when #verify is false' do - allow(test).to receive(:verify) { false } - expect(test.pass?).to be true - end - end - end + it_behaves_like 'test' end end diff --git a/spec/producer/core/tests/file_contains_spec.rb b/spec/producer/core/tests/file_contains_spec.rb index 5d9377c..b19b911 100644 --- a/spec/producer/core/tests/file_contains_spec.rb +++ b/spec/producer/core/tests/file_contains_spec.rb @@ -8,9 +8,7 @@ module Producer::Core let(:content) { 'some_content' } subject(:test) { FileContains.new(env, filepath, content) } - it 'is a kind of test' do - expect(test).to be_a Test - end + it_behaves_like 'test' describe '#verify' do let(:fs) { double 'fs' } diff --git a/spec/producer/core/tests/has_dir_spec.rb b/spec/producer/core/tests/has_dir_spec.rb index 765c17d..8840dce 100644 --- a/spec/producer/core/tests/has_dir_spec.rb +++ b/spec/producer/core/tests/has_dir_spec.rb @@ -7,9 +7,7 @@ module Producer::Core let(:path) { 'some_directory' } subject(:has_dir) { HasDir.new(env, path) } - it 'is a kind of test' do - expect(has_dir).to be_a Test - end + it_behaves_like 'test' describe '#verify', :ssh do before { sftp_story } diff --git a/spec/producer/core/tests/has_env_spec.rb b/spec/producer/core/tests/has_env_spec.rb index 505161a..3085048 100644 --- a/spec/producer/core/tests/has_env_spec.rb +++ b/spec/producer/core/tests/has_env_spec.rb @@ -7,9 +7,7 @@ module Producer::Core let(:variable_name) { :some_variable_name } subject(:has_env) { HasEnv.new(env, variable_name) } - it 'is a kind of test' do - expect(has_env).to be_a Test - end + it_behaves_like 'test' describe '#verify' do let(:environment) { double 'environment' } diff --git a/spec/producer/core/tests/has_file_spec.rb b/spec/producer/core/tests/has_file_spec.rb index 5ec1749..bc8be8e 100644 --- a/spec/producer/core/tests/has_file_spec.rb +++ b/spec/producer/core/tests/has_file_spec.rb @@ -7,9 +7,7 @@ module Producer::Core let(:filepath) { 'some_file' } subject(:has_file) { HasFile.new(env, filepath) } - it 'is a kind of test' do - expect(has_file).to be_a Test - end + it_behaves_like 'test' describe '#verify', :ssh do before { sftp_story } diff --git a/spec/support/shared_test.rb b/spec/support/shared_test.rb new file mode 100644 index 0000000..d46eee6 --- /dev/null +++ b/spec/support/shared_test.rb @@ -0,0 +1,81 @@ +module Producer::Core + shared_examples 'test' do + let(:env) { Env.new } + let(:arguments) { [:some, :arguments] } + subject(:test) { described_class.new(env, *arguments) } + + describe '#initialize' do + it 'assigns the env' do + expect(test.env).to be env + end + + it 'assigns the arguments' do + expect(test.arguments).to eq arguments + end + + it 'assigns negated as false by default' do + expect(test).to_not be_negated + end + + context 'when negated option is true' do + subject(:test) { described_class.new(env, *arguments, negated: true) } + + it 'assigns negated as true' do + expect(test).to be_negated + end + end + end + + describe '#remote' do + it 'returns env remote' do + expect(test.remote).to be test.env.remote + end + end + + describe '#fs' do + it 'returns env remote fs' do + expect(test.fs).to be test.env.remote.fs + end + end + + describe '#negated?' do + it 'returns false' do + expect(test.negated?).to be false + end + + context 'when test is negated' do + subject(:test) { described_class.new(env, *arguments, negated: true) } + + it 'returns true' do + expect(test.negated?).to be true + end + end + end + + describe '#pass?' do + it 'returns true when #verify is true' do + allow(test).to receive(:verify) { true } + expect(test.pass?).to be true + end + + it 'returns false when #verify is false' do + allow(test).to receive(:verify) { false } + expect(test.pass?).to be false + end + + context 'when test is negated' do + subject(:test) { described_class.new(env, *arguments, negated: true) } + + it 'returns false when #verify is true' do + allow(test).to receive(:verify) { true } + expect(test.pass?).to be false + end + + it 'returns true when #verify is false' do + allow(test).to receive(:verify) { false } + expect(test.pass?).to be true + end + end + end + end +end