brew/Library/Homebrew/test/cask/config_spec.rb
Claudia cad8be3278
Add broken test, revealing test helper flaw
This commit adds a broken test, which is meant to expose a flaw in the
constructor of `Cask::Config`.

That (broken) test still passes because there’s also a flaw in our
test helper code.

The helper flaw happens to neutralize the `Cask::Config` flaw.
2020-04-27 15:00:22 +02:00

96 lines
2.7 KiB
Ruby

# frozen_string_literal: true
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"))
end
end
describe "#appdir" do
it "returns the default value if no HOMEBREW_CASK_OPTS is unset" do
expect(config.appdir).to eq(Pathname(TEST_TMPDIR).join("cask-appdir"))
end
specify "environment overwrites default" do
ENV["HOMEBREW_CASK_OPTS"] = "--appdir=/path/to/apps"
expect(config.appdir).to eq(Pathname("/path/to/apps"))
end
specify "specific overwrites default" do
config = described_class.new(explicit: { appdir: "/explicit/path/to/apps" })
expect(config.appdir).to eq(Pathname("/explicit/path/to/apps"))
end
specify "explicit overwrites environment" do
ENV["HOMEBREW_CASK_OPTS"] = "--appdir=/path/to/apps"
config = described_class.new(explicit: { appdir: "/explicit/path/to/apps" })
expect(config.appdir).to eq(Pathname("/explicit/path/to/apps"))
end
end
describe "#env" do
it "returns directories specified with the HOMEBREW_CASK_OPTS variable" do
ENV["HOMEBREW_CASK_OPTS"] = "--appdir=/path/to/apps"
expect(config.env).to eq(appdir: Pathname("/path/to/apps"))
end
end
describe "#explicit" do
let(:config) { described_class.new(explicit: { appdir: "/explicit/path/to/apps" }) }
it "returns directories explicitly given as arguments" do
expect(config.explicit[:appdir]).to eq(Pathname("/explicit/path/to/apps"))
end
end
context "when installing a cask and then adding a global default dir" do
let(:config) {
json = <<~EOS
{
"default": {
"appdir": "/default/path/before/adding/fontdir"
},
"env": {},
"explicit": {}
}
EOS
described_class.from_json(json)
}
describe "#appdir" do
it "honors metadata of the installed cask" do
expect(config.appdir).to eq(Pathname("/default/path/before/adding/fontdir"))
end
end
describe "#fontdir" do
it "falls back to global default on incomplete metadata" do
expect(config.default).to include(fontdir: Pathname(TEST_TMPDIR).join("cask-fontdir"))
end
end
end
end