Refactor CLI::Create.
This commit is contained in:
parent
b7347dcc44
commit
8248345a9a
@ -2,7 +2,11 @@ module Hbc
|
||||
class CLI
|
||||
class Create < Base
|
||||
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?
|
||||
cask_token = cask_tokens.first.sub(/\.rb$/i, "")
|
||||
cask_path = CaskLoader.path(cask_token)
|
||||
@ -11,7 +15,7 @@ module Hbc
|
||||
raise CaskAlreadyCreatedError, cask_token if cask_path.exist?
|
||||
|
||||
File.open(cask_path, "w") do |f|
|
||||
f.write template(cask_token)
|
||||
f.write self.class.template(cask_token)
|
||||
end
|
||||
|
||||
exec_editor cask_path
|
||||
|
||||
@ -1,43 +1,26 @@
|
||||
# monkeypatch for testing
|
||||
module Hbc
|
||||
class CLI
|
||||
class Create
|
||||
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::Create, :cask do
|
||||
before(:each) do
|
||||
Hbc::CLI::Create.reset!
|
||||
around(:each) do |example|
|
||||
begin
|
||||
example.run
|
||||
ensure
|
||||
%w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask|
|
||||
FileUtils.rm_f Hbc::CaskLoader.path(cask)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
%w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask|
|
||||
path = Hbc::CaskLoader.path(cask)
|
||||
path.delete if path.exist?
|
||||
end
|
||||
before(:each) do
|
||||
allow_any_instance_of(described_class).to receive(:exec_editor)
|
||||
end
|
||||
|
||||
it "opens the editor for the specified Cask" do
|
||||
Hbc::CLI::Create.run("new-cask")
|
||||
expect(Hbc::CLI::Create.editor_commands).to eq [
|
||||
[Hbc::CaskLoader.path("new-cask")],
|
||||
]
|
||||
command = described_class.new("new-cask")
|
||||
expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("new-cask"))
|
||||
command.run
|
||||
end
|
||||
|
||||
it "drops a template down for the specified Cask" do
|
||||
Hbc::CLI::Create.run("new-cask")
|
||||
described_class.run("new-cask")
|
||||
template = File.read(Hbc::CaskLoader.path("new-cask"))
|
||||
expect(template).to eq <<-EOS.undent
|
||||
cask 'new-cask' do
|
||||
@ -54,36 +37,33 @@ describe Hbc::CLI::Create, :cask do
|
||||
end
|
||||
|
||||
it "throws away additional Cask arguments and uses the first" do
|
||||
Hbc::CLI::Create.run("additional-cask", "another-cask")
|
||||
expect(Hbc::CLI::Create.editor_commands).to eq [
|
||||
[Hbc::CaskLoader.path("additional-cask")],
|
||||
]
|
||||
command = described_class.new("additional-cask", "another-cask")
|
||||
expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("additional-cask"))
|
||||
command.run
|
||||
end
|
||||
|
||||
it "throws away stray options" do
|
||||
Hbc::CLI::Create.run("--notavalidoption", "yet-another-cask")
|
||||
expect(Hbc::CLI::Create.editor_commands).to eq [
|
||||
[Hbc::CaskLoader.path("yet-another-cask")],
|
||||
]
|
||||
command = described_class.new("--notavalidoption", "yet-another-cask")
|
||||
expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("yet-another-cask"))
|
||||
command.run
|
||||
end
|
||||
|
||||
it "raises an exception when the Cask already exists" do
|
||||
expect {
|
||||
Hbc::CLI::Create.run("basic-cask")
|
||||
described_class.run("basic-cask")
|
||||
}.to raise_error(Hbc::CaskAlreadyCreatedError)
|
||||
end
|
||||
|
||||
it "allows creating Casks that are substrings of existing Casks" do
|
||||
Hbc::CLI::Create.run("local-caff")
|
||||
expect(Hbc::CLI::Create.editor_commands).to eq [
|
||||
[Hbc::CaskLoader.path("local-caff")],
|
||||
]
|
||||
command = described_class.new("local-caff")
|
||||
expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caff"))
|
||||
command.run
|
||||
end
|
||||
|
||||
describe "when no Cask is specified" do
|
||||
it "raises an exception" do
|
||||
expect {
|
||||
Hbc::CLI::Create.run
|
||||
described_class.run
|
||||
}.to raise_error(Hbc::CaskUnspecifiedError)
|
||||
end
|
||||
end
|
||||
@ -91,7 +71,7 @@ describe Hbc::CLI::Create, :cask do
|
||||
describe "when no Cask is specified, but an invalid option" do
|
||||
it "raises an exception" do
|
||||
expect {
|
||||
Hbc::CLI::Create.run("--notavalidoption")
|
||||
described_class.run("--notavalidoption")
|
||||
}.to raise_error(Hbc::CaskUnspecifiedError)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user