From bc92addc6021e217e85586351a91d1cab449a933 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 28 Feb 2017 11:01:04 -0800 Subject: refactor to use the config flag because the initalizer is awful ruby doesn't like optional hashes combined with optional named parameters -- because hashes and kwargs get magically converted and ruby gets confused. plus fixed/added specs Signed-off-by: Lamont Granquist --- lib/ohai/application.rb | 3 ++- lib/ohai/system.rb | 4 ++-- spec/unit/system_spec.rb | 34 ++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/ohai/application.rb b/lib/ohai/application.rb index f01cf842..586a275d 100644 --- a/lib/ohai/application.rb +++ b/lib/ohai/application.rb @@ -88,7 +88,8 @@ class Ohai::Application end def run_application - ohai = Ohai::System.new(config, cli: true) + config[:invoked_from_cli] = true + ohai = Ohai::System.new(config) ohai.all_plugins(@attributes) if @attributes diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 6fa56085..e66ba90b 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -43,8 +43,8 @@ module Ohai # something like chef-client (which doesn't not set this flag) and # which sets up its own loggers, or if we're coming from Ohai::Application # and therefore need to configure Ohai's own logger. - def initialize(config = {}, cli: false) - @cli = cli + def initialize(config = {}) + @cli = config[:invoked_from_cli] @plugin_path = "" @config = config reset_system diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index f184d8ee..8a7cf49b 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob () # Author:: Claire McQuin () -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,8 @@ require "ohai/mixin/os" describe "Ohai::System" do extend IntegrationSupport - let(:ohai) { Ohai::System.new } + let(:ohai_external) { } + let(:ohai) { Ohai::System.new({ invoked_from_cli: true }) } describe "#initialize" do it "returns an Ohai::System object" do @@ -43,8 +44,6 @@ describe "Ohai::System" do disabled_plugins: [ :Foo, :Baz ], directory: "/some/extra/plugins", } - allow(Ohai::Config).to receive(:merge_deprecated_config) - expect(Ohai.config).to receive(:merge!).with(config).and_call_original Ohai::System.new(config) config.each do |option, value| expect(Ohai.config[option]).to eq(value) @@ -56,33 +55,36 @@ describe "Ohai::System" do it "adds directory to plugin_path" do Ohai.config[:directory] = directory - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) expect(Ohai.config[:plugin_path]).to include(directory) end end - context "first time configuration" do - before { allow(Ohai::Log).to receive(:configured?).and_return(false) } - + context "when testing the intializer that does way too much" do it "configures logging" do log_level = :debug Ohai.config[:log_level] = log_level expect(Ohai::Log).to receive(:level=).with(log_level) - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) end it "resolves log_level when set to :auto" do expect(Ohai::Log).to receive(:level=).with(:info) - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) end - end - context "after first time configuration" do - before { allow(Ohai::Log).to receive(:configured?).and_return(true) } + context "when called externally" do + it "does not configure logging" do + log_level = :debug + Ohai.config[:log_level] = log_level + expect(Ohai::Log).not_to receive(:level=).with(log_level) + Ohai::System.new() + end - it "configures logging" do - expect(Ohai::Log).not_to receive(:init).with(Ohai.config[:log_location]) - Ohai::System.new + it "does not resolve log_level when set to :auto" do + expect(Ohai::Log).not_to receive(:level=).with(:info) + Ohai::System.new() + end end end end -- cgit v1.2.1