From a1dbaacf5a682d131ec81d6318d7a2cbf14f2409 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Sat, 8 Jul 2017 22:39:15 -0700 Subject: [PATCH 1/2] Switch from yajl-ruby to ffi-yajl ffi-yajl is used by Chef already so this is one less requirement in ChefDK. yajl-ruby install size in macOS ChefDK is 3.8M Signed-off-by: Tim Smith --- foodcritic.gemspec | 2 +- lib/foodcritic.rb | 2 +- lib/foodcritic/api.rb | 3 +-- lib/foodcritic/chef.rb | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/foodcritic.gemspec b/foodcritic.gemspec index 5f362846..35d7eada 100644 --- a/foodcritic.gemspec +++ b/foodcritic.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.add_dependency("nokogiri", ">= 1.5", "< 2.0") s.add_dependency("rake") s.add_dependency("treetop", "~> 1.4") - s.add_dependency("yajl-ruby", "~> 1.1") + s.add_dependency("ffi-yajl", "~> 2.0") s.add_dependency("erubis") s.add_dependency("rufus-lru", "~> 1.0") diff --git a/lib/foodcritic.rb b/lib/foodcritic.rb index bf3b7330..8916c821 100644 --- a/lib/foodcritic.rb +++ b/lib/foodcritic.rb @@ -2,7 +2,7 @@ require "cucumber/core" require "treetop" require "ripper" -require "yajl" +require "ffi_yajl" require "erubis" require_relative "foodcritic/chef" diff --git a/lib/foodcritic/api.rb b/lib/foodcritic/api.rb index abfb527f..2883afe3 100644 --- a/lib/foodcritic/api.rb +++ b/lib/foodcritic/api.rb @@ -1,6 +1,5 @@ require "nokogiri" require "rufus-lru" -require "json" module FoodCritic # Helper methods that form part of the Rules DSL. @@ -424,7 +423,7 @@ def json_file_to_hash(filename) file = File.read(filename) begin - JSON.parse(file) + FFI_Yajl::Parser.parse(file) rescue RuntimeError raise "File #{filename} does not appear to contain valid JSON" end diff --git a/lib/foodcritic/chef.rb b/lib/foodcritic/chef.rb index 1fa94710..5b3a0a72 100644 --- a/lib/foodcritic/chef.rb +++ b/lib/foodcritic/chef.rb @@ -55,7 +55,7 @@ def load_metadata Linter::DEFAULT_CHEF_VERSION].map do |version| metadata_path(version) end.find { |m| File.exist?(m) } - @dsl_metadata ||= Yajl::Parser.parse(IO.read(metadata_path), + @dsl_metadata ||= FFI_Yajl::Parser.parse(IO.read(metadata_path), symbolize_keys: true) end From 4859358634c6e89e68139fb36435f87c66255715 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Sun, 9 Jul 2017 12:40:56 -0700 Subject: [PATCH 2/2] Make sure we still error properly on bad JSON data Signed-off-by: Tim Smith --- lib/foodcritic/api.rb | 2 +- spec/unit/api_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/foodcritic/api.rb b/lib/foodcritic/api.rb index 2883afe3..f1bdc96a 100644 --- a/lib/foodcritic/api.rb +++ b/lib/foodcritic/api.rb @@ -424,7 +424,7 @@ def json_file_to_hash(filename) file = File.read(filename) begin FFI_Yajl::Parser.parse(file) - rescue RuntimeError + rescue FFI_Yajl::ParseError raise "File #{filename} does not appear to contain valid JSON" end end diff --git a/spec/unit/api_spec.rb b/spec/unit/api_spec.rb index 4db72faa..3ebeab48 100644 --- a/spec/unit/api_spec.rb +++ b/spec/unit/api_spec.rb @@ -1928,7 +1928,7 @@ def template_ast(content) it "raises if the json is not valid" do expect(::File).to receive(:exist?).with("/some/path/with/a/file").and_return(true) allow(File).to receive(:read).with("/some/path/with/a/file").and_return("I am bogus data") - expect { api.json_file_to_hash("/some/path/with/a/file") }.to raise_error JSON::ParserError + expect { api.json_file_to_hash("/some/path/with/a/file") }.to raise_error RuntimeError end end