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:
parent
efef0087a6
commit
dd3267ece0
@ -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
|
||||||
|
@ -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"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user