From 023c89e326251e8c2f063aa6f4ecd787cd35316f Mon Sep 17 00:00:00 2001 From: "Marc A. Paradise" Date: Fri, 7 Jun 2019 15:39:36 -0400 Subject: Don't explode when there are unknown keys in 'config' Update handle_deprecated_options to be graceful when 'config' has been modified externally to contain unknown keys. Signed-off-by: Marc A. Paradise --- lib/mixlib/cli.rb | 7 ++++++- spec/mixlib/cli_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb index 1a83a11..d0c1c67 100644 --- a/lib/mixlib/cli.rb +++ b/lib/mixlib/cli.rb @@ -392,11 +392,16 @@ module Mixlib merge_in_values = {} config.each_key do |opt_key| opt_cfg = options[opt_key] + # Deprecated entries do not have defaults so no matter what # separate_default_options are set, if we see a 'config' # entry that contains a deprecated indicator, then the option was # explicitly provided by the caller. - next unless opt_cfg[:deprecated] + # + # opt_cfg may not exist if an inheriting application + # has directly inserted values info config. + next unless opt_cfg && opt_cfg[:deprecated] + replacement_key = opt_cfg[:replacement] if replacement_key # This is the value passed into the deprecated flag. We'll use diff --git a/spec/mixlib/cli_spec.rb b/spec/mixlib/cli_spec.rb index acc10f8..46795aa 100644 --- a/spec/mixlib/cli_spec.rb +++ b/spec/mixlib/cli_spec.rb @@ -326,6 +326,18 @@ describe Mixlib::CLI do TestCLI.option(:option_c, short: "-c ARG") end + context "when someone injects an unexpected value into 'config'" do + before do + cli.config[:surprise] = true + end + it "parses and preserves both known and unknown config values" do + cli.parse_options(%w{--option-a}) + expect(cli.config[:surprise]).to eql true + expect(cli.config[:option_a]).to eql true + end + + end + context "when the deprecated option has a replacement" do context "and a value_mapper is provided" do -- cgit v1.2.1