Permalink
Browse files

add error message when file does not exist.

  • Loading branch information...
1 parent 479c10e commit 623ec82c7b918da62d8d87c5b18f50f11d69ffe0 @achiu committed Oct 10, 2011
Showing with 38 additions and 5 deletions.
  1. +22 −3 lib/consular/cli.rb
  2. +16 −2 spec/cli_spec.rb
View
25 lib/consular/cli.rb
@@ -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.
@@ -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
@@ -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
@@ -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
View
18 spec/cli_spec.rb
@@ -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
@@ -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
@@ -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

0 comments on commit 623ec82

Please sign in to comment.