From 19b23f51b0e164c6097ecfd0e7c1bdd5bf0bb2a2 Mon Sep 17 00:00:00 2001 From: Will Fleming Date: Sun, 9 Oct 2016 22:05:45 -0400 Subject: [PATCH] Use fetch_language to coerce python config Addresses #136 The changes made in #135 didn't account for configuring the languages with empty hash contents. With YAML parsing, something like ``` languages: python: ``` Gets parsed as `{ "languages" => { "python" => "" } }`, leading to a `NoMethodError` when the Python class attempted to discover the configured Python version. `EngineConfig` already had logic to ensure a language configuration was a hash, so this makes the appropriate coercing method public & uses it from the Python class. (Since the python version key is a Python-only option, the `EngineConfig` class still doesn't seem like the appropriate place for that logic.) --- lib/cc/engine/analyzers/engine_config.rb | 8 ++++---- lib/cc/engine/analyzers/python/main.rb | 2 +- spec/cc/engine/analyzers/python/main_spec.rb | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/cc/engine/analyzers/engine_config.rb b/lib/cc/engine/analyzers/engine_config.rb index 74834acc..e0d062d1 100644 --- a/lib/cc/engine/analyzers/engine_config.rb +++ b/lib/cc/engine/analyzers/engine_config.rb @@ -30,10 +30,6 @@ def mass_threshold_for(language) end end - private - - attr_reader :config - def fetch_language(language) language = config. fetch("languages", {}). @@ -46,6 +42,10 @@ def fetch_language(language) end end + private + + attr_reader :config + def normalize(hash) hash.tap do |config| languages = config.fetch("config", {}).fetch("languages", {}) diff --git a/lib/cc/engine/analyzers/python/main.rb b/lib/cc/engine/analyzers/python/main.rb index e1d408fa..ca5a5ca9 100644 --- a/lib/cc/engine/analyzers/python/main.rb +++ b/lib/cc/engine/analyzers/python/main.rb @@ -27,7 +27,7 @@ def parser(path) end def python_version - engine_config.languages.fetch("python", {}).fetch("python_version", DEFAULT_PYTHON_VERSION) + engine_config.fetch_language(LANGUAGE).fetch("python_version", DEFAULT_PYTHON_VERSION) end end end diff --git a/spec/cc/engine/analyzers/python/main_spec.rb b/spec/cc/engine/analyzers/python/main_spec.rb index d62e2c01..89b03a2e 100644 --- a/spec/cc/engine/analyzers/python/main_spec.rb +++ b/spec/cc/engine/analyzers/python/main_spec.rb @@ -117,6 +117,23 @@ def c(thing: str): end end + it "handles an empty yml key in config" do + create_source_file("foo.py", <<-EOPY) +def a(thing): + print("Hello", thing) + EOPY + + conf = CC::Engine::Analyzers::EngineConfig.new({ + "config" => { + "languages" => { + "python" => "" + } + } + }) + + expect(run_engine(engine_conf)).to eq("") + end + def engine_conf CC::Engine::Analyzers::EngineConfig.new({ "config" => {