Skip to content

Commit

Permalink
Add convenience methods for executing commands
Browse files Browse the repository at this point in the history
This follows a similar approach to the Path
helper and extracts methods for each call to
Exec so that Provider and its specs are much
simpler.

I also added stubs for Exec to spec_helper.rb
so that we don't execute commands in the
tests, except where they're explicitly
unstubbed to unit test .execute and .capture.
  • Loading branch information
tuzz committed Sep 10, 2017
1 parent a03b5d6 commit 65a1108
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 36 deletions.
16 changes: 2 additions & 14 deletions lib/zz/commands/provision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@ module ZZ
module Provision
class << self
def execute(args)
install_chef unless chef_installed?
run_chef
end

def install_chef
Exec.execute("curl -sL #{ZZ::Path.chef_installer} | sudo bash")
end

def chef_installed?
Exec.execute("which chef-solo")
end

def run_chef
Exec.execute("chef-solo --config #{ZZ::Path.chef_config}")
Exec.install_chef unless Exec.chef_installed?
Exec.run_chef
end

def name
Expand Down
24 changes: 19 additions & 5 deletions lib/zz/exec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
module ZZ
module Exec
def self.execute(command)
system(command)
end
class << self
def execute(command)
system(command)
end

def capture(command)
`#{command}`
end

def install_chef
execute("curl -sL #{Path.chef_installer} | sudo bash")
end

def chef_installed?
execute("which chef-solo")
end

def self.capture(command)
`#{command}`
def run_chef
execute("chef-solo --config #{Path.chef_config}")
end
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
config.disable_monkey_patching!
config.formatter = :doc
config.color = true

config.before do
allow(ZZ::Exec).to receive(:execute)
allow(ZZ::Exec).to receive(:capture)
end
end
22 changes: 5 additions & 17 deletions spec/zz/commands/provision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,21 @@
end

it "installs chef if it isn't already installed" do
allow(ZZ::Exec).to receive(:execute)
.with("which chef-solo").and_return(false)

expect(ZZ::Exec).to receive(:execute)
.with("curl -sL #{ZZ::Path.chef_installer} | sudo bash")

allow(ZZ::Exec).to receive(:execute)
allow(ZZ::Exec).to receive(:chef_installed?).and_return(false)
expect(ZZ::Exec).to receive(:install_chef)

subject.execute([])
end

it "does not install chef if it's already installed" do
allow(ZZ::Exec).to receive(:execute)
.with("which chef-solo").and_return(true)

expect(ZZ::Exec).not_to receive(:execute)
.with("curl -sL #{ZZ::Path.chef_installer} } | sudo bash")
allow(ZZ::Exec).to receive(:chef_installed?).and_return(true)
expect(ZZ::Exec).not_to receive(:install_chef)

subject.execute([])
end

it "runs chef" do
allow(ZZ::Exec).to receive(:execute)

expect(ZZ::Exec).to receive(:execute)
.with("chef-solo --config #{ZZ::Path.chef_config}")

expect(ZZ::Exec).to receive(:run_chef)
subject.execute([])
end

Expand Down
37 changes: 37 additions & 0 deletions spec/zz/exec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
RSpec.describe ZZ::Exec do
describe ".execute" do
before do
allow(subject).to receive(:execute).and_call_original
end

it "executes the command in a subprocess" do
result = subject.execute("touch foo")

Expand All @@ -11,9 +15,42 @@
end

describe ".capture" do
before do
allow(subject).to receive(:capture).and_call_original
end

it "executes the command and returns its output" do
result = subject.capture("echo foo")
expect(result).to eq("foo\n")
end
end

describe ".install_chef" do
it "curls the chef installer and runs it as root" do
expect(subject).to receive(:execute)
.with("curl -sL #{ZZ::Path.chef_installer} | sudo bash")

subject.install_chef
end
end

describe ".chef_installed?" do
it "returns whether chef-solo is installed" do
bool = double(:bool)

expect(subject).to receive(:execute)
.with("which chef-solo").and_return(bool)

expect(subject.chef_installed?).to eq(bool)
end
end

describe ".run_chef" do
it "runs chef with the path to its config" do
expect(subject).to receive(:execute)
.with("chef-solo --config #{ZZ::Path.chef_config}")

subject.run_chef
end
end
end

0 comments on commit 65a1108

Please sign in to comment.