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