Refactor CLI::Home.

This commit is contained in:
Markus Reiter 2017-05-19 21:48:21 +02:00
parent 1f5828c72d
commit 66e9a060de
2 changed files with 24 additions and 40 deletions

View File

@ -1,19 +1,28 @@
module Hbc module Hbc
class CLI class CLI
class Home < Base class Home < Base
def self.run(*cask_tokens) def self.run(*args)
if cask_tokens.empty? new(*args).run
end
def run
casks = @args.map(&CaskLoader.public_method(:load))
if casks.empty?
odebug "Opening project homepage" odebug "Opening project homepage"
system "/usr/bin/open", "--", "https://caskroom.github.io/" self.class.open_url "https://caskroom.github.io/"
else else
cask_tokens.each do |cask_token| casks.each do |cask|
odebug "Opening homepage for Cask #{cask_token}" odebug "Opening homepage for Cask #{cask}"
cask = CaskLoader.load(cask_token) self.class.open_url cask.homepage
system "/usr/bin/open", "--", cask.homepage
end end
end end
end end
def self.open_url(url)
SystemCommand.run!(OS::PATH_OPEN, args: ["--", url])
end
def self.help def self.help
"opens the homepage of the given Cask" "opens the homepage of the given Cask"
end end

View File

@ -1,46 +1,21 @@
# monkeypatch for testing
module Hbc
class CLI
class Home
def self.system(*command)
system_commands << command
end
def self.reset!
@system_commands = []
end
def self.system_commands
@system_commands ||= []
end
end
end
end
describe Hbc::CLI::Home, :cask do describe Hbc::CLI::Home, :cask do
before do before do
Hbc::CLI::Home.reset! allow(described_class).to receive(:open_url)
end end
it "opens the homepage for the specified Cask" do it "opens the homepage for the specified Cask" do
Hbc::CLI::Home.run("local-caffeine") expect(described_class).to receive(:open_url).with("http://example.com/local-caffeine")
expect(Hbc::CLI::Home.system_commands).to eq [ described_class.run("local-caffeine")
["/usr/bin/open", "--", "http://example.com/local-caffeine"],
]
end end
it "works for multiple Casks" do it "works for multiple Casks" do
Hbc::CLI::Home.run("local-caffeine", "local-transmission") expect(described_class).to receive(:open_url).with("http://example.com/local-caffeine")
expect(Hbc::CLI::Home.system_commands).to eq [ expect(described_class).to receive(:open_url).with("http://example.com/local-transmission")
["/usr/bin/open", "--", "http://example.com/local-caffeine"], described_class.run("local-caffeine", "local-transmission")
["/usr/bin/open", "--", "http://example.com/local-transmission"],
]
end end
it "opens the project page when no Cask is specified" do it "opens the project page when no Cask is specified" do
Hbc::CLI::Home.run expect(described_class).to receive(:open_url).with("https://caskroom.github.io/")
expect(Hbc::CLI::Home.system_commands).to eq [ described_class.run
["/usr/bin/open", "--", "https://caskroom.github.io/"],
]
end end
end end