summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2019-12-20 21:58:25 -0800
committerTim Smith <tsmith84@gmail.com>2019-12-20 21:59:03 -0800
commit60261bd2993c5625bc095ea52a9c91651d745617 (patch)
tree4cc11225724e1f6fccf160e3d21ffe4e30a68c3f /spec
parentc812ebaa631ad00d9d7e136fbe545e18d2589ef8 (diff)
downloadwmi-lite-60261bd2993c5625bc095ea52a9c91651d745617.tar.gz
Apply Chefstyle
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'spec')
-rw-r--r--spec/functional/wmi_spec.rb98
-rw-r--r--spec/spec_helper.rb9
-rw-r--r--spec/unit/wmi_spec.rb120
3 files changed, 113 insertions, 114 deletions
diff --git a/spec/functional/wmi_spec.rb b/spec/functional/wmi_spec.rb
index cf16f44..e6bfe0e 100644
--- a/spec/functional/wmi_spec.rb
+++ b/spec/functional/wmi_spec.rb
@@ -16,101 +16,101 @@
# limitations under the License.
#
-require 'spec_helper'
+require "spec_helper"
describe WmiLite::Wmi, :windows_only do
let(:wmi) { WmiLite::Wmi.new(namespace) }
def validate_wmi_results(results, class_name)
result_collection = cardinality_transform.call(results)
- result_collection.each do | result |
+ result_collection.each do |result|
# make sure the class name of the instance is what we asked for
- expect(result['creationclassname'].downcase).to eql(class_name.downcase)
+ expect(result["creationclassname"].downcase).to eql(class_name.downcase)
end
end
- shared_examples_for 'a valid WMI query result' do
- it 'should successfully return multiple results' do
+ shared_examples_for "a valid WMI query result" do
+ it "should successfully return multiple results" do
query_parameter = wmi_query.nil? ? wmi_class : wmi_query
results = wmi.send(query_method, query_parameter)
validate_wmi_results(results, wmi_class)
end
- describe 'when the namespace is invalid' do
- it_behaves_like 'an invalid namespace'
+ describe "when the namespace is invalid" do
+ it_behaves_like "an invalid namespace"
end
end
- shared_examples_for 'an invalid query' do
- it 'should raise an exception' do
+ shared_examples_for "an invalid query" do
+ it "should raise an exception" do
expect { wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
end
- shared_examples_for 'an invalid namespace' do
- it 'should raise an exception if an invalid namespace is specified' do
- invalid_wmi = WmiLite::Wmi.new('root/notvalid')
+ shared_examples_for "an invalid namespace" do
+ it "should raise an exception if an invalid namespace is specified" do
+ invalid_wmi = WmiLite::Wmi.new("root/notvalid")
expect { invalid_wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
end
- shared_examples_for 'a valid WMI query' do
- let(:wmi_class) { 'Win32_LogicalDisk' }
- it_behaves_like 'a valid WMI query result'
+ shared_examples_for "a valid WMI query" do
+ let(:wmi_class) { "Win32_LogicalDisk" }
+ it_behaves_like "a valid WMI query result"
- let(:wmi_class) { 'Win32_ComputerSystem' }
- it_behaves_like 'a valid WMI query result'
+ let(:wmi_class) { "Win32_ComputerSystem" }
+ it_behaves_like "a valid WMI query result"
- let(:wmi_class) { 'Win32_Process' }
- it_behaves_like 'a valid WMI query result'
+ let(:wmi_class) { "Win32_Process" }
+ it_behaves_like "a valid WMI query result"
- context 'that return 0 results' do
- let(:wmi_class) { 'Win32_TapeDrive' }
- it_behaves_like 'a valid WMI query result'
+ context "that return 0 results" do
+ let(:wmi_class) { "Win32_TapeDrive" }
+ it_behaves_like "a valid WMI query result"
end
end
- context 'when making valid queries' do
+ context "when making valid queries" do
let(:namespace) { nil }
let(:wmi_query) { nil }
- let(:cardinality_transform) { lambda{|x| x} }
- context 'using first_of' do
- let(:cardinality_transform) { lambda{|x| x.nil? ? [] : [x] } }
+ let(:cardinality_transform) { lambda { |x| x } }
+ context "using first_of" do
+ let(:cardinality_transform) { lambda { |x| x.nil? ? [] : [x] } }
let(:query_method) { :first_of }
- it_behaves_like 'a valid WMI query'
+ it_behaves_like "a valid WMI query"
end
- context 'using instances_of' do
+ context "using instances_of" do
let(:query_method) { :instances_of }
- it_behaves_like 'a valid WMI query'
+ it_behaves_like "a valid WMI query"
end
- context 'using query' do
+ context "using query" do
let(:wmi_query) { "select * from #{wmi_class}" }
let(:query_method) { :query }
- it_behaves_like 'a valid WMI query'
+ it_behaves_like "a valid WMI query"
end
end
- context 'when making invalid queries' do
+ context "when making invalid queries" do
let(:namespace) { nil }
- let(:wmi_query) { 'invalidclass' }
+ let(:wmi_query) { "invalidclass" }
let(:query_method) { :first_of }
- it_behaves_like 'an invalid query'
+ it_behaves_like "an invalid query"
let(:query_method) { :instances_of }
- it_behaves_like 'an invalid query'
+ it_behaves_like "an invalid query"
let(:query_method) { :query }
- let(:wmi_query) { 'nosql_4_life' }
- it_behaves_like 'an invalid query'
+ let(:wmi_query) { "nosql_4_life" }
+ it_behaves_like "an invalid query"
end
let(:namespace) { nil }
- describe 'when querying Win32_Environment' do
- it 'should have the same environment variables as the Ruby ENV environment hash' do
- results = wmi.instances_of('Win32_Environment')
+ describe "when querying Win32_Environment" do
+ it "should have the same environment variables as the Ruby ENV environment hash" do
+ results = wmi.instances_of("Win32_Environment")
variables = {}
@@ -119,15 +119,15 @@ describe WmiLite::Wmi, :windows_only do
# PROCESSOR_ARCHITECTURE is actually the real processor arch of the system, so #{ENV['processor_architecture']} will
# report X86, while WMI will (correctly) report X64.
# And username is oddly the username of the WMI service, i.e. 'SYSTEM'.
- ignore = {'path' => true, 'pathext' => true, 'processor_architecture' => true, 'psmodulepath' => true, 'username' => true}
- results.each do | result |
- if ! variables.has_key?(result['name']) || result['username'] != '<SYSTEM>'
- variables[result['name']] = result['variablevalue']
+ ignore = { "path" => true, "pathext" => true, "processor_architecture" => true, "psmodulepath" => true, "username" => true }
+ results.each do |result|
+ if ! variables.key?(result["name"]) || result["username"] != "<SYSTEM>"
+ variables[result["name"]] = result["variablevalue"]
end
end
verified_count = 0
- variables.each_pair do | name, value |
+ variables.each_pair do |name, value|
if ignore[name.downcase] != true
# Turn %SYSTEMROOT% into c:\windows
@@ -146,10 +146,10 @@ describe WmiLite::Wmi, :windows_only do
end
let(:namespace) { nil }
- it 'should ignore case when retrieving WMI properties' do
- result = wmi.first_of('Win32_ComputerSystem')
- caption_mixed = result['Caption']
- caption_lower = result['caption']
+ it "should ignore case when retrieving WMI properties" do
+ result = wmi.first_of("Win32_ComputerSystem")
+ caption_mixed = result["Caption"]
+ caption_lower = result["caption"]
expect(caption_mixed.nil?).to eql(false)
expect(caption_lower.nil?).to eql(false)
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 58036cc..762363c 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,11 +1,10 @@
# $:.unshift File.expand_path('../../lib', __FILE__)
-require 'rspec'
-require 'wmi-lite'
+require "rspec"
+require "wmi-lite"
RSpec.configure do |config|
config.include(RSpec::Matchers)
- config.filter_run :focus => true
- config.filter_run_excluding :windows_only => true if ! (RUBY_PLATFORM =~ /mswin|mingw32|windows/)
+ config.filter_run focus: true
+ config.filter_run_excluding windows_only: true unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
config.run_all_when_everything_filtered = true
end
-
diff --git a/spec/unit/wmi_spec.rb b/spec/unit/wmi_spec.rb
index a116ec9..3ee3805 100644
--- a/spec/unit/wmi_spec.rb
+++ b/spec/unit/wmi_spec.rb
@@ -16,37 +16,37 @@
# limitations under the License.
#
-require 'spec_helper'
+require "spec_helper"
describe WmiLite::Wmi do
- let(:wbem_locator) { double 'WIN32OLE', :ConnectServer => wbem_connection }
- let(:wmi_query_instance1) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance1, :[] => native_properties1 }
- let(:wmi_query_instance2) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance2, :[] => native_properties2 }
+ let(:wbem_locator) { double "WIN32OLE", ConnectServer: wbem_connection }
+ let(:wmi_query_instance1) { double "Wmi::Instance", :wmi_ole_object => native_query_instance1, :[] => native_properties1 }
+ let(:wmi_query_instance2) { double "Wmi::Instance", :wmi_ole_object => native_query_instance2, :[] => native_properties2 }
let(:wmi_query_result1) { [ wmi_query_instance1 ].to_enum }
let(:wmi_query_result2) { [ wmi_query_instance1, wmi_query_instance2 ].to_enum }
let(:native_query_result1) { [ native_query_instance1 ].to_enum }
let(:native_query_result2) { [ native_query_instance1, native_query_instance2 ].to_enum }
let(:wmi_query_result_empty) { [].to_enum }
- let(:native_properties1) { wmi_properties1.map { | property, value | double 'WIN32OLE', :name => property } }
- let(:native_properties2) { wmi_properties2.map { | property, value | double 'WIN32OLE', :name => property } }
- let(:native_query_instance1) { double 'WIN32OLE', :properties_ => native_properties1, :invoke => 'value1' }
- let(:native_query_instance2) { double 'WIN32OLE', :properties_ => native_properties2, :invoke => 'value2' }
- let(:wbem_connection) { double 'WIN32OLE', :ExecQuery => native_query_result }
+ let(:native_properties1) { wmi_properties1.map { |property, value| double "WIN32OLE", name: property } }
+ let(:native_properties2) { wmi_properties2.map { |property, value| double "WIN32OLE", name: property } }
+ let(:native_query_instance1) { double "WIN32OLE", properties_: native_properties1, invoke: "value1" }
+ let(:native_query_instance2) { double "WIN32OLE", properties_: native_properties2, invoke: "value2" }
+ let(:wbem_connection) { double "WIN32OLE", ExecQuery: native_query_result }
def validate_query_result(actual, expected)
expect(actual.count).to eql(expected.count)
index = 0
- expected.each do | expected_value |
+ expected.each do |expected_value|
actual_value = actual[index]
expected_value.wmi_ole_object.invoke == actual_value.wmi_ole_object.invoke
- expected_value.wmi_ole_object.properties_.each do | expected_property |
+ expected_value.wmi_ole_object.properties_.each do |expected_property|
expect(actual_value[expected_property.name]).not_to eql(nil)
- names = actual_value.wmi_ole_object.properties_.map { | property | property.name }
+ names = actual_value.wmi_ole_object.properties_.map(&:name)
expect(names.include?(expected_property.name)).to eql(true)
@@ -56,9 +56,9 @@ describe WmiLite::Wmi do
end
before(:each) do
- stub_const('WIN32OLE', Class.new)
+ stub_const("WIN32OLE", Class.new)
WIN32OLE.stub(:new).with("WbemScripting.SWbemLocator").and_return(wbem_locator)
- stub_const('WIN32OLERuntimeError', Class.new(Exception))
+ stub_const("WIN32OLERuntimeError", Class.new(RuntimeError))
end
let(:wmi) { WmiLite::Wmi.new }
@@ -66,21 +66,21 @@ describe WmiLite::Wmi do
let(:native_query_result) { [].to_enum }
it "should not fail with empty query results" do
- results = wmi.query('')
+ results = wmi.query("")
result_count = 0
- results.each { | result | result_count += 1 }
+ results.each { |result| result_count += 1 }
expect( result_count ).to eq(0)
end
shared_examples_for "the first_of method" do
- let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
- let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
+ let(:wmi_properties1) { { "cores" => 4, "name" => "mycomputer1", "diskspace" => 400, "os" => "windows" } }
+ let(:wmi_properties2) { { "cores" => 2, "name" => "mycomputer2", "bios" => "ami", "os" => "windows" } }
let(:native_query_result) { [].to_enum }
it "should not fail with empty query results" do
- results = wmi.first_of('vm')
+ results = wmi.first_of("vm")
expect( results ).to eq(nil)
end
@@ -89,7 +89,7 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result1 }
it "should get one instance" do
- results = wmi.first_of('vm')
+ results = wmi.first_of("vm")
expected_result = WmiLite::Wmi::Instance.new(native_query_result.first)
validate_query_result([results], [expected_result])
end
@@ -100,7 +100,7 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result2 }
it "should get one instance" do
- results = wmi.first_of('vm')
+ results = wmi.first_of("vm")
expected_result = WmiLite::Wmi::Instance.new(native_query_result.first)
validate_query_result([results], [expected_result])
end
@@ -110,12 +110,12 @@ describe WmiLite::Wmi do
shared_examples_for "the instances_of method" do
- let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
- let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
+ let(:wmi_properties1) { { "cores" => 4, "name" => "mycomputer1", "diskspace" => 400, "os" => "windows" } }
+ let(:wmi_properties2) { { "cores" => 2, "name" => "mycomputer2", "bios" => "ami", "os" => "windows" } }
let(:native_query_result) { [].to_enum }
it "should not fail with empty query results" do
- results = wmi.instances_of('vm')
+ results = wmi.instances_of("vm")
expect( results ).to eq([])
end
@@ -124,9 +124,9 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result1 }
it "should get one instance" do
- results = wmi.instances_of('vm')
+ results = wmi.instances_of("vm")
index = 0
- expected_result = results.map do | result |
+ expected_result = results.map do |result|
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
end
validate_query_result(results, expected_result)
@@ -138,9 +138,9 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result2 }
it "should get one instance" do
- results = wmi.instances_of('vm')
+ results = wmi.instances_of("vm")
index = 0
- expected_result = results.map do | result |
+ expected_result = results.map do |result|
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
end
validate_query_result(results, expected_result)
@@ -149,15 +149,15 @@ describe WmiLite::Wmi do
end
- shared_examples_for 'an invalid query' do
- let(:unparseable_error) { 'unparseableerror' }
- it 'should raise an exception' do
+ shared_examples_for "an invalid query" do
+ let(:unparseable_error) { "unparseableerror" }
+ it "should raise an exception" do
wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError)
wmi_service = WmiLite::Wmi.new
expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
- it 'should raise an exception that ends with the original exception message' do
+ it "should raise an exception that ends with the original exception message" do
wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError.new(unparseable_error))
wmi_service = WmiLite::Wmi.new
error_message = nil
@@ -177,15 +177,15 @@ describe WmiLite::Wmi do
end
end
- shared_examples_for 'an invalid namespace' do
- let(:unparseable_error) { 'unparseableerror' }
- it 'should raise an exception' do
+ shared_examples_for "an invalid namespace" do
+ let(:unparseable_error) { "unparseableerror" }
+ it "should raise an exception" do
wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError)
- wmi_service = WmiLite::Wmi.new('notavalidnamespace')
+ wmi_service = WmiLite::Wmi.new("notavalidnamespace")
expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
end
- it 'should raise an exception that starts with the original exception message' do
+ it "should raise an exception that starts with the original exception message" do
wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError.new(unparseable_error))
wmi_service = WmiLite::Wmi.new
error_message = nil
@@ -205,12 +205,12 @@ describe WmiLite::Wmi do
shared_examples_for "the query method" do
- let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
- let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
+ let(:wmi_properties1) { { "cores" => 4, "name" => "mycomputer1", "diskspace" => 400, "os" => "windows" } }
+ let(:wmi_properties2) { { "cores" => 2, "name" => "mycomputer2", "bios" => "ami", "os" => "windows" } }
let(:native_query_result) { [].to_enum }
it "should not fail with empty query results" do
- results = wmi.query('vm')
+ results = wmi.query("vm")
expect( results ).to eq([])
end
@@ -219,9 +219,9 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result1 }
it "should get one instance" do
- results = wmi.query('vm')
+ results = wmi.query("vm")
index = 0
- expected_result = results.map do | result |
+ expected_result = results.map do |result|
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
end
validate_query_result(results, expected_result)
@@ -233,9 +233,9 @@ describe WmiLite::Wmi do
let(:native_query_result) { native_query_result2 }
it "should get one instance" do
- results = wmi.query('vm')
+ results = wmi.query("vm")
index = 0
- expected_result = results.map do | result |
+ expected_result = results.map do |result|
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
end
validate_query_result(results, expected_result)
@@ -248,7 +248,7 @@ describe WmiLite::Wmi do
it "should not connect to WMI in the constructor" do
WmiLite::Wmi.any_instance.should_not_receive(:connect_to_namespace)
wmi_service_nil_namespace = WmiLite::Wmi.new
- wmi_service_explicit_namespace = WmiLite::Wmi.new('root/cimv2')
+ wmi_service_explicit_namespace = WmiLite::Wmi.new("root/cimv2")
end
end
@@ -258,32 +258,32 @@ describe WmiLite::Wmi do
wmi_service = WmiLite::Wmi.new
# Make a lot of queries to be sure the connection is only created once
- wmi_service.query('select * from Win32_Process')
- wmi_service.query('select * from Win32_Process')
- wmi_service.instances_of('Win32_Processor')
- wmi_service.instances_of('Win32_Processor')
- wmi_service.first_of('Win32_Group')
- wmi_service.first_of('Win32_Group')
+ wmi_service.query("select * from Win32_Process")
+ wmi_service.query("select * from Win32_Process")
+ wmi_service.instances_of("Win32_Processor")
+ wmi_service.instances_of("Win32_Processor")
+ wmi_service.first_of("Win32_Group")
+ wmi_service.first_of("Win32_Group")
end
end
- context 'when making invalid queries' do
+ context "when making invalid queries" do
let(:namespace) { nil }
- let(:wmi_query) { 'invalidclass' }
+ let(:wmi_query) { "invalidclass" }
let(:query_method) { :first_of }
- it_behaves_like 'an invalid query'
- it_behaves_like 'an invalid namespace'
+ it_behaves_like "an invalid query"
+ it_behaves_like "an invalid namespace"
let(:query_method) { :instances_of }
- it_behaves_like 'an invalid query'
- it_behaves_like 'an invalid namespace'
+ it_behaves_like "an invalid query"
+ it_behaves_like "an invalid namespace"
let(:query_method) { :query }
- let(:wmi_query) { 'nosql_4_life' }
- it_behaves_like 'an invalid query'
- it_behaves_like 'an invalid namespace'
+ let(:wmi_query) { "nosql_4_life" }
+ it_behaves_like "an invalid query"
+ it_behaves_like "an invalid namespace"
end
it_should_behave_like "the first_of method"