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.
This commit is contained in:
Claudia 2020-04-27 14:54:56 +02:00
parent efef0087a6
commit dd3267ece0
No known key found for this signature in database
GPG Key ID: 246AC3C0F10BE51F
2 changed files with 18 additions and 3 deletions

View File

@ -34,15 +34,15 @@ module Cask
def self.for_cask(cask) def self.for_cask(cask)
if cask.config_path.exist? if cask.config_path.exist?
from_file(cask.config_path) from_json(File.read(cask.config_path))
else else
global global
end end
end end
def self.from_file(path) def self.from_json(json)
config = begin config = begin
JSON.parse(File.read(path)) JSON.parse(json)
rescue JSON::ParserError => e rescue JSON::ParserError => e
raise e, "Cannot parse #{path}: #{e}", e.backtrace raise e, "Cannot parse #{path}: #{e}", e.backtrace
end end

View File

@ -3,6 +3,21 @@
describe Cask::Config, :cask do describe Cask::Config, :cask do
subject(:config) { described_class.new } 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 describe "#default" do
it "returns the default directories" do it "returns the default directories" do
expect(config.default[:appdir]).to eq(Pathname(TEST_TMPDIR).join("cask-appdir")) expect(config.default[:appdir]).to eq(Pathname(TEST_TMPDIR).join("cask-appdir"))