Skip to content

Commit

Permalink
Improve error message for source_root and add a shortcut to define it.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 30, 2010
1 parent fd2b32b commit 75231d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
23 changes: 18 additions & 5 deletions lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ module ClassMethods
# inherited paths and the source root.
#
def source_paths
@source_paths ||= []
@_source_paths ||= []
end

# Stores and return the source root for this class
def source_root(path=nil)
@_source_root = path if path
@_source_root
end

# Returns the source paths in the following order:
Expand All @@ -32,7 +38,7 @@ def source_paths
def source_paths_for_search
paths = []
paths += self.source_paths
paths << self.source_root if self.respond_to?(:source_root)
paths << self.source_root if self.source_root
paths += from_superclass(:source_paths, [])
paths
end
Expand Down Expand Up @@ -126,12 +132,19 @@ def find_in_source_paths(file)
return source_file if File.exists?(source_file)
end

message = "Could not find #{file.inspect} in any of your source paths. "

unless self.class.source_root
message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
end

if source_paths.empty?
raise Error, "You don't have any source path defined for class #{self.class.name}. To fix this, " <<
"you can define a source_root in your class."
message << "Currently you have no source paths."
else
raise Error, "Could not find #{file.inspect} in source paths: \n#{source_paths.join("\n")}"
message << "Your current source paths are: \n#{source_paths.join("\n")}"
end

raise Error, message
end

# Do something in the root or on a provided subfolder. If a relative path
Expand Down
2 changes: 1 addition & 1 deletion spec/actions/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def exists_and_identical?(source_path, destination_path)
it "raises an error if the source does not exist" do
lambda {
invoke! "unknown"
}.must raise_error(Thor::Error, /Could not find "unknown" in source paths/)
}.must raise_error(Thor::Error, /Could not find "unknown" in any of your source paths/)
end

it "copies the whole directory recursively to the default destination" do
Expand Down
2 changes: 1 addition & 1 deletion spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def file
it "raises an error if source path is empty" do
lambda {
A.new.find_in_source_paths("foo")
}.must raise_error(Thor::Error, /You don't have any source path defined for class A/)
}.must raise_error(Thor::Error, /Currently you have no source paths/)
end

it "finds a template inside the source path" do
Expand Down
4 changes: 1 addition & 3 deletions spec/fixtures/group.thor
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class MyCounter < Thor::Group
from_superclass(:get_from_super, 13)
end

def self.source_root
File.expand_path(File.dirname(__FILE__))
end
source_root File.expand_path(File.dirname(__FILE__))
source_paths << File.expand_path("broken", File.dirname(__FILE__))

argument :first, :type => :numeric
Expand Down

0 comments on commit 75231d1

Please sign in to comment.