Skip to content

Commit

Permalink
OC-4739: OC-4748: Refactor tests to share examples across batch and p…
Browse files Browse the repository at this point in the history
…owershell resource specs
  • Loading branch information
adamedx authored and adamedx committed Feb 19, 2013
1 parent 63d34bb commit a3f5ab6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 168 deletions.
2 changes: 1 addition & 1 deletion lib/chef/provider/powershell.rb
Expand Up @@ -27,7 +27,7 @@ def initialize (new_resource, run_context)
end

def flags
@new_resource.flags.nil? ? '-ExecutionPolicy RemoteSigned -Command' : new_resource.flags + '-ExecutionPolicy RemoteSigned -Commmand'
@new_resource.flags.nil? ? '-ExecutionPolicy RemoteSigned -Command' : @new_resource.flags + '-ExecutionPolicy RemoteSigned -Command'
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/provider/script.rb
Expand Up @@ -63,7 +63,7 @@ def flags
protected

def interpreter_script_path
@script_file.path
script_file.path
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/provider/windows_script.rb
Expand Up @@ -41,7 +41,7 @@ def script_file
end

def interpreter_script_path
@script_file.path.gsub(::File::SEPARATOR) { | replace | ::File::ALT_SEPARATOR }
script_file.path.gsub(::File::SEPARATOR) { | replace | ::File::ALT_SEPARATOR }
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/provider/powershell_spec.rb
Expand Up @@ -21,8 +21,12 @@

before(:each) do
@node = Chef::Node.new

@node.default["kernel"] = Hash.new
@node.default["kernel"][:machine] = :x86_64.to_s

@run_context = Chef::RunContext.new(@node, {}, @events)
@new_resource = Chef::Resource::Powershell.new('run some powershell code')
@new_resource = Chef::Resource::Powershell.new('run some powershell code', @run_context)
# @new_resource.code "$| = 1; print 'i like beans'"
# @new_resource.interpreter 'perl'

Expand Down
23 changes: 7 additions & 16 deletions spec/unit/resource/batch_spec.rb
Expand Up @@ -33,25 +33,16 @@
end

it "should create a new Chef::Resource::Batch" do
@resource.should be_a_kind_of(Chef::Resource)
@resource.should be_a_kind_of(Chef::Resource::Batch)
end

it "should have a resource name of :batch" do
@resource.resource_name.should eql(:batch)
end

it "should have an interpreter with a file name of cmd.exe" do

context "windowssystemscript" do
let(:resource_instance) { @resource }
let(:resource_instance_name ) { @resource.command }
let(:resource_name) { :batch }
let(:interpreter_file_name) { 'cmd.exe' }

# When rspec-mocks 2.11 is released, switch to constant_stubbing
# with const_stub below
# stub_const("::File::ALT_SEPARATOR",::File::SEPARATOR).
# For now, stub out a method that exists just for this purpose
# @resource.respond_to?(:windows_separator).should == true
# @resource.stub(:windows_separator) { ::File::SEPARATOR }

@resource.interpreter.split('\\').pop.casecmp('cmd.exe').should == 0
it_should_behave_like "a Windows system script resource"
end

end
104 changes: 3 additions & 101 deletions spec/unit/resource/execute_spec.rb
Expand Up @@ -20,105 +20,7 @@
require 'spec_helper'

describe Chef::Resource::Execute do

before(:each) do
@resource = Chef::Resource::Execute.new("some command")
end

it "should create a new Chef::Resource::Execute" do
@resource.should be_a_kind_of(Chef::Resource)
@resource.should be_a_kind_of(Chef::Resource::Execute)
end

it "should set the command to the first argument to new" do
@resource.command.should eql("some command")
end

it "should accept an array on instantiation, too" do
resource = Chef::Resource::Execute.new(%w{something else})
resource.should be_a_kind_of(Chef::Resource)
resource.should be_a_kind_of(Chef::Resource::Execute)
resource.command.should eql(%w{something else})
end

it "should accept a string for the command to run" do
@resource.command "something"
@resource.command.should eql("something")
end

it "should accept an array for the command to run" do
@resource.command %w{something else}
@resource.command.should eql(%w{something else})
end

it "should accept a string for the cwd" do
@resource.cwd "something"
@resource.cwd.should eql("something")
end

it "should accept a hash for the environment" do
test_hash = { :one => :two }
@resource.environment(test_hash)
@resource.environment.should eql(test_hash)
end

it "allows the environment to be specified with #env" do
@resource.should respond_to(:env)
end

it "should accept a string for the group" do
@resource.group "something"
@resource.group.should eql("something")
end

it "should accept an integer for the group" do
@resource.group 1
@resource.group.should eql(1)
end

it "should accept an array for the execution path" do
@resource.path ["woot"]
@resource.path.should eql(["woot"])
end

it "should accept an integer for the return code" do
@resource.returns 1
@resource.returns.should eql(1)
end

it "should accept an integer for the timeout" do
@resource.timeout 1
@resource.timeout.should eql(1)
end

it "should accept a string for the user" do
@resource.user "something"
@resource.user.should eql("something")
end

it "should accept an integer for the user" do
@resource.user 1
@resource.user.should eql(1)
end

it "should accept a string for creates" do
@resource.creates "something"
@resource.creates.should eql("something")
end

describe "when it has cwd, environment, group, path, return value, and a user" do
before do
@resource.command("grep")
@resource.cwd("/tmp/")
@resource.environment({ :one => :two })
@resource.group("legos")
@resource.path(["/var/local/"])
@resource.returns(1)
@resource.user("root")
end

it "returns the command as its identity" do
@resource.identity.should == "grep"
end
end
let(:resource_instance_name) { "some command" }
let(:execute_resource) { Chef::Resource::Execute.new(resource_instance_name) }
it_behaves_like "an execute resource"
end
18 changes: 7 additions & 11 deletions spec/unit/resource/powershell_spec.rb
Expand Up @@ -26,27 +26,23 @@
node.default["kernel"] = Hash.new
node.default["kernel"][:machine] = :x86_64.to_s

run_context = Chef::RunContext.new(node, nil, nil)

@resource = Chef::Resource::Batch.new("batch_unit_test", run_context)

run_context = Chef::RunContext.new(node, nil, nil)

@resource = Chef::Resource::Powershell.new("powershell_unit_test", run_context)

end

it "should create a new Chef::Resource::Powershell" do
@resource.should be_a_kind_of(Chef::Resource)
@resource.should be_a_kind_of(Chef::Resource::Powershell)
end

it "should have a resource name of :powershell" do
@resource.resource_name.should eql(:powershell)
end

it "should have an interpreter with a file name of powershell.exe" do
@resource.interpreter.split('\\').pop.casecmp('powershell.exe').should == 0
context "windowssystemscript" do
let(:resource_instance) { @resource }
let(:resource_instance_name ) { @resource.command }
let(:resource_name) { :powershell }
let(:interpreter_file_name) { 'powershell.exe' }

it_should_behave_like "a Windows system script resource"
end

end
49 changes: 13 additions & 36 deletions spec/unit/resource/script_spec.rb
Expand Up @@ -20,50 +20,27 @@
require 'spec_helper'

describe Chef::Resource::Script do
let(:resource_instance_name) { "fakey_fakerton" }
let(:script_resource) { Chef::Resource::Script.new(resource_instance_name) }
let(:resource_name) { :script }

before(:each) do
@resource = Chef::Resource::Script.new("fakey_fakerton")
end

it "should create a new Chef::Resource::Script" do
@resource.should be_a_kind_of(Chef::Resource)
@resource.should be_a_kind_of(Chef::Resource::Script)
end

it "should have a resource name of :script" do
@resource.resource_name.should eql(:script)
end

it "should set command to the argument provided to new" do
@resource.command.should eql("fakey_fakerton")
end

it "should accept a string for the code" do
@resource.code "hey jude"
@resource.code.should eql("hey jude")
end

it "should accept a string for the interpreter" do
@resource.interpreter "naaaaNaNaNaaNaaNaaNaa"
@resource.interpreter.should eql("naaaaNaNaNaaNaaNaaNaa")
end

it "should accept a string for the flags" do
@resource.flags "-f"
@resource.flags.should eql("-f")
script_resource.interpreter "naaaaNaNaNaaNaaNaaNaa"
script_resource.interpreter.should eql("naaaaNaNaNaaNaaNaaNaa")
end

describe "when it has interpreter and flags" do
before do
@resource.command("grep")
@resource.interpreter("gcc")
@resource.flags("-al")
script_resource.command("grep")
script_resource.interpreter("gcc")
script_resource.flags("-al")
end

it "returns the command as its identity" do
@resource.identity.should == "grep"
it "returns the command as its identity" do
script_resource.identity.should == "grep"
end
end


it_behaves_like "a script resource"
end

0 comments on commit a3f5ab6

Please sign in to comment.