Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/linux_admin/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

module LinuxAdmin
module Common
def cmd(cmd)
Distros.local.command(cmd)
BIN_DIRS = %w(/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin)

def cmd(name)
BIN_DIRS.collect { |dir| "#{dir}/#{name}" }.detect { |cmd| File.exist?(cmd) }
end

def cmd?(cmd)
Distros.local.command?(cmd)
def cmd?(name)
!cmd(name).nil?
end

def run(cmd, options = {})
Expand Down
9 changes: 0 additions & 9 deletions lib/linux_admin/distro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Distro

def initialize(id, release_file = nil, etc_issue_keywords = [], info_class = nil)
@id = id
@path = %w(/sbin /bin /usr/bin /usr/sbin)
@release_file = release_file
@etc_issue_keywords = etc_issue_keywords
@info_class = info_class
Expand All @@ -56,14 +55,6 @@ def detected_by_etc_release?
release_file && File.exist?(release_file)
end

def command(name)
@path.collect { |dir| "#{dir}/#{name}" }.detect { |cmd| File.exist?(cmd) }
end

def command?(name)
!!command(name)
end

def info(pkg)
info_class ? info_class.info(pkg) : nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/linux_admin/rpm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Rpm < Package
extend Logging

def self.rpm_cmd
Distros.local.command(:rpm)
cmd(:rpm)
end

def self.list_installed
Expand Down
32 changes: 25 additions & 7 deletions spec/common_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
describe LinuxAdmin::Common do
subject { Class.new { include LinuxAdmin::Common }.new }

context "#cmd" do
describe "#cmd" do
it "looks up local command from id" do
expect(subject.cmd(:dd)).to match(/bin\/dd/)
end

it "returns nil when it can't find the command" do
expect(subject.cmd(:kasgbdlcvjhals)).to be_nil
end
end

it "#run" do
expect(AwesomeSpawn).to receive(:run).with("echo", nil => "test")
subject.run("echo", nil => "test")
describe "#cmd?" do
it "returns true when the command exists" do
expect(subject.cmd?(:dd)).to be true
end

it "returns false when the command doesn't exist" do
expect(subject.cmd?(:kasgbdlcvjhals)).to be false
end
end

it "#run!" do
expect(AwesomeSpawn).to receive(:run!).with("echo", nil => "test")
subject.run!("echo", nil => "test")
describe "#run" do
it "runs a command with AwesomeSpawn.run" do
expect(AwesomeSpawn).to receive(:run).with("echo", nil => "test")
subject.run("echo", nil => "test")
end
end

describe "#run!" do
it "runs a command with AwesomeSpawn.run!" do
expect(AwesomeSpawn).to receive(:run!).with("echo", nil => "test")
subject.run!("echo", nil => "test")
end
end
end