Skip to content

Commit

Permalink
Add a Hwinfo#present? method
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 2, 2019
1 parent e793f66 commit b252618
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib/y2network/hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require "yast"
require "y2network/driver"
require "y2network/udev_rule"

module Y2Network
class HardwareWrapper
Expand Down Expand Up @@ -171,7 +172,8 @@ def initialize(hwinfo = {})
{ name: "wl_enc_modes", default: nil },
{ name: "wl_channels", default: nil },
{ name: "wl_bitrates", default: nil },
{ name: "dev_port", default: nil }
{ name: "dev_port", default: nil },
{ name: "type", default: nil }
].each do |hwinfo_item|
define_method hwinfo_item[:name].downcase do
@hwinfo ? @hwinfo.fetch(hwinfo_item[:name], hwinfo_item[:default]) : hwinfo_item[:default]
Expand Down Expand Up @@ -214,6 +216,17 @@ def drivers
modules.map { |m| Driver.new(*m) }
end

# Determines whether the hardware is available (plugged)
#
# If the hardware layer was able to get its type, it consider the hardware to be connected. Bear
# in mind that it is not possible to just rely in #exists? because it could include some info
# from udev rules.
#
# @return [Boolean]
def present?
!!type
end

# Determines whether two objects are equivalent
#
# Ignores any element having a nil value.
Expand Down
18 changes: 18 additions & 0 deletions test/y2network/hwinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,22 @@
.to eq(described_class.new("dev_name" => "eth0"))
end
end

describe "#present?" do
context "when the hardware was detected" do
subject(:hwinfo) { described_class.new("type" => "eth") }

it "returns true" do
expect(hwinfo).to be_present
end
end

context "when the hardware was not detected" do
subject(:hwinfo) { described_class.new({}) }

it "returns false" do
expect(hwinfo).to_not be_present
end
end
end
end

0 comments on commit b252618

Please sign in to comment.