Refactor CLI::Edit.

This commit is contained in:
Markus Reiter 2017-05-19 22:01:50 +02:00
parent 8248345a9a
commit 276adc9e8b
2 changed files with 15 additions and 32 deletions

View File

@ -2,7 +2,11 @@ module Hbc
class CLI class CLI
class Edit < Base class Edit < Base
def self.run(*args) def self.run(*args)
cask_tokens = cask_tokens_from(args) new(*args).run
end
def run
cask_tokens = self.class.cask_tokens_from(@args)
raise CaskUnspecifiedError if cask_tokens.empty? raise CaskUnspecifiedError if cask_tokens.empty?
# only respects the first argument # only respects the first argument
cask_token = cask_tokens.first.sub(/\.rb$/i, "") cask_token = cask_tokens.first.sub(/\.rb$/i, "")

View File

@ -1,51 +1,30 @@
# monkeypatch for testing
module Hbc
class CLI
class Edit
def self.exec_editor(*command)
editor_commands << command
end
def self.reset!
@editor_commands = []
end
def self.editor_commands
@editor_commands ||= []
end
end
end
end
describe Hbc::CLI::Edit, :cask do describe Hbc::CLI::Edit, :cask do
before(:each) do before(:each) do
Hbc::CLI::Edit.reset! allow_any_instance_of(described_class).to receive(:exec_editor)
end end
it "opens the editor for the specified Cask" do it "opens the editor for the specified Cask" do
Hbc::CLI::Edit.run("local-caffeine") command = described_class.new("local-caffeine")
expect(Hbc::CLI::Edit.editor_commands).to eq [ expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caffeine"))
[Hbc::CaskLoader.path("local-caffeine")], command.run
]
end end
it "throws away additional arguments and uses the first" do it "throws away additional arguments and uses the first" do
Hbc::CLI::Edit.run("local-caffeine", "local-transmission") command = described_class.new("local-caffeine", "local-transmission")
expect(Hbc::CLI::Edit.editor_commands).to eq [ expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caffeine"))
[Hbc::CaskLoader.path("local-caffeine")], command.run
]
end end
it "raises an exception when the Cask doesnt exist" do it "raises an exception when the Cask doesnt exist" do
expect { expect {
Hbc::CLI::Edit.run("notacask") described_class.run("notacask")
}.to raise_error(Hbc::CaskUnavailableError) }.to raise_error(Hbc::CaskUnavailableError)
end end
describe "when no Cask is specified" do describe "when no Cask is specified" do
it "raises an exception" do it "raises an exception" do
expect { expect {
Hbc::CLI::Edit.run described_class.run
}.to raise_error(Hbc::CaskUnspecifiedError) }.to raise_error(Hbc::CaskUnspecifiedError)
end end
end end
@ -53,7 +32,7 @@ describe Hbc::CLI::Edit, :cask do
describe "when no Cask is specified, but an invalid option" do describe "when no Cask is specified, but an invalid option" do
it "raises an exception" do it "raises an exception" do
expect { expect {
Hbc::CLI::Edit.run("--notavalidoption") described_class.run("--notavalidoption")
}.to raise_error(Hbc::CaskUnspecifiedError) }.to raise_error(Hbc::CaskUnspecifiedError)
end end
end end