From dd3267ece0b2252c13f23e10a8d905f96f067406 Mon Sep 17 00:00:00 2001 From: Claudia Date: Mon, 27 Apr 2020 14:54:56 +0200 Subject: [PATCH] Add test for JSON-based cask config loader Previously, the JSON-based cask config loader was untested. This commit changes the interface to accept a string, making the loader easier to test. The commit also adds a test. --- Library/Homebrew/cask/config.rb | 6 +++--- Library/Homebrew/test/cask/config_spec.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cask/config.rb b/Library/Homebrew/cask/config.rb index 6ddad5430b..4ca3b367a9 100644 --- a/Library/Homebrew/cask/config.rb +++ b/Library/Homebrew/cask/config.rb @@ -34,15 +34,15 @@ module Cask def self.for_cask(cask) if cask.config_path.exist? - from_file(cask.config_path) + from_json(File.read(cask.config_path)) else global end end - def self.from_file(path) + def self.from_json(json) config = begin - JSON.parse(File.read(path)) + JSON.parse(json) rescue JSON::ParserError => e raise e, "Cannot parse #{path}: #{e}", e.backtrace end diff --git a/Library/Homebrew/test/cask/config_spec.rb b/Library/Homebrew/test/cask/config_spec.rb index f968f533c6..6add7d7360 100644 --- a/Library/Homebrew/test/cask/config_spec.rb +++ b/Library/Homebrew/test/cask/config_spec.rb @@ -3,6 +3,21 @@ describe Cask::Config, :cask do subject(:config) { described_class.new } + describe "::from_json" do + it "deserializes a configuration in JSON format" do + config = described_class.from_json <<~EOS + { + "default": { + "appdir": "/path/to/apps" + }, + "env": {}, + "explicit": {} + } + EOS + expect(config.appdir).to eq(Pathname("/path/to/apps")) + end + end + describe "#default" do it "returns the default directories" do expect(config.default[:appdir]).to eq(Pathname(TEST_TMPDIR).join("cask-appdir"))