Skip to content

Commit

Permalink
add error message when file does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
achiurizo committed Oct 10, 2011
1 parent 479c10e commit 623ec82
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
25 changes: 22 additions & 3 deletions lib/consular/cli.rb
Expand Up @@ -31,7 +31,8 @@ def self.source_root; File.expand_path('../../', __FILE__); end
desc 'start [PROJECT]', 'runs the consular script. ex: `consular start` or `consular start foobar`'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def start(project = nil)
valid_core.new(termfile_path(project)).process!
path = termfile_path(project)
message_unless_file(path) { valid_core.new(path).process! }
end

# Run the Termfile or project script setup.
Expand All @@ -55,7 +56,8 @@ def start(project = nil)
desc 'setup [PROJECT]', 'run the consular script setup. ex: `consular setup` or `consular setup foobar`'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def setup(project = nil)
valid_core.new(termfile_path(project)).setup!
path = termfile_path(project)
message_unless_file(path) { valid_core.new(path).setup! }
end

# Lists all avaiable global scripts
Expand Down Expand Up @@ -133,7 +135,7 @@ def edit(project = nil)
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def delete(project = nil)
path = termfile_path(project)
remove_file path
message_unless_file(path) { remove_file path }
end

no_tasks do
Expand Down Expand Up @@ -195,6 +197,23 @@ def open_in_editor(path, editor = nil)
editor = editor || Consular.default_editor || ENV['EDITOR']
system "#{editor || 'open'} #{path}"
end

# Returns an error message unless the file exists. If it does
# execute the block
#
# @param [String] file
# Path of file
# @param [Proc] blk
# Proc to execute if file exists.
#
# @api private
def message_unless_file(file, &blk)
if File.exists?(file)
blk.call
else
say "#{file} does not exist. Try running `consular edit` first.", :yellow
end
end
end

end
Expand Down
18 changes: 16 additions & 2 deletions spec/cli_spec.rb
Expand Up @@ -59,15 +59,18 @@ def setup!; puts('setup'); end

it "should start a global term script" do
output = capture_io { Consular::CLI.start ['start', 'foo'] }.join('')

assert_match /process/, output
end

it "should start a global yaml script" do
output = capture_io { Consular::CLI.start ['start', 'foo.yml'] }.join('')

assert_match /process/, output
end

it "should return an error message if it doesn't exist" do
output = capture_io { Consular::CLI.start ['start', 'barr'] }.join('')
assert_match /does not exist/, output
end
end

describe "setup command" do
Expand All @@ -93,6 +96,11 @@ def setup!; puts('setup'); end
output = capture_io { Consular::CLI.start ['setup', 'foo.yml'] }.join('')
assert_match /setup/, output
end

it "should return an error message if it doesn't exist" do
output = capture_io { Consular::CLI.start ['setup', 'barr'] }.join('')
assert_match /does not exist/, output
end
end

it "init creates a new global script directory and consularc" do
Expand Down Expand Up @@ -126,6 +134,12 @@ def setup!; puts('setup'); end
refute File.exists?(Consular.global_path('delete_this.term')), 'deletes .term file'
end

it "removes .term file" do
output = capture_io { Consular::CLI.start ['delete','barr'] }.join('')

assert_match /does not exist/, output
end

end

describe "edit command" do
Expand Down

0 comments on commit 623ec82

Please sign in to comment.