Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make user home logic to its own method so it can be reused.

  • Loading branch information...
commit ee97ca0ae7cb07d7906d8617b717c44d8710557a 1 parent 2fbf19a
José Valim josevalim authored
2  lib/thor/actions.rb
View
@@ -163,7 +163,7 @@ def chmod(path, mode, log_status=true)
#
def run(command, log_status=true)
return unless behavior == :invoke
- say_status :run, "#{command} from #{relative_to_absolute_root(root, false)}", log_status
+ say_status :run, "\"#{command}\" from #{relative_to_absolute_root(root, false)}", log_status
`#{command}` unless options[:pretend]
end
40 lib/thor/util.rb
View
@@ -162,26 +162,32 @@ def self.convert_constants_to_namespaces(yaml)
yaml_changed
end
+ def self.user_home
+ @@user_home ||= if ENV["HOME"]
+ ENV["HOME"]
+ elsif ENV["USERPROFILE"]
+ ENV["USERPROFILE"]
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
+ elsif ENV["APPDATA"]
+ ENV["APPDATA"]
+ else
+ begin
+ File.expand_path("~")
+ rescue
+ if File::ALT_SEPARATOR
+ "C:/"
+ else
+ "/"
+ end
+ end
+ end
+ end
+
# Returns the root where thor files are located, dependending on the OS.
#
def self.thor_root
- return File.join(ENV["HOME"], '.thor') if ENV["HOME"]
-
- if ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
- return File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"], '.thor')
- end
-
- return File.join(ENV["APPDATA"], '.thor') if ENV["APPDATA"]
-
- begin
- File.expand_path("~")
- rescue
- if File::ALT_SEPARATOR
- "C:/"
- else
- "/"
- end
- end
+ File.join(user_home, ".thor")
end
# Returns the files in the thor root. On Windows thor_root will be something
4 spec/actions_spec.rb
View
@@ -182,7 +182,7 @@ def file
end
it "logs status" do
- action(:run, "ls").must == " run ls from .\n"
+ action(:run, "ls").must == " run \"ls\" from .\n"
end
it "does not log status if required" do
@@ -190,7 +190,7 @@ def file
end
it "accepts a color as status" do
- mock(runner.shell).say_status(:run, "ls from .", :yellow)
+ mock(runner.shell).say_status(:run, '"ls" from .', :yellow)
action :run, "ls", :yellow
end
end
25 spec/util_spec.rb
View
@@ -1,5 +1,11 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+module Thor::Util
+ def self.clear_user_home!
+ @@user_home = nil
+ end
+end
+
describe Thor::Util do
describe "#find_by_namespace" do
it "returns 'Default' if no namespace is given" do
@@ -110,20 +116,21 @@
end
end
- describe "#thor_root" do
+ describe "#user_home" do
before(:each) do
stub(ENV)[]
+ Thor::Util.clear_user_home!
end
it "returns the user path if none variable is set on the environment" do
- Thor::Util.thor_root.must == File.expand_path("~")
+ Thor::Util.user_home.must == File.expand_path("~")
end
it "returns the *unix system path if file cannot be expanded and separator does not exist" do
stub(File).expand_path("~"){ raise }
previous_value = File::ALT_SEPARATOR
capture(:stderr){ File.const_set(:ALT_SEPARATOR, false) }
- Thor::Util.thor_root.must == "/"
+ Thor::Util.user_home.must == "/"
capture(:stderr){ File.const_set(:ALT_SEPARATOR, previous_value) }
end
@@ -131,24 +138,24 @@
stub(File).expand_path("~"){ raise }
previous_value = File::ALT_SEPARATOR
capture(:stderr){ File.const_set(:ALT_SEPARATOR, true) }
- Thor::Util.thor_root.must == "C:/"
+ Thor::Util.user_home.must == "C:/"
capture(:stderr){ File.const_set(:ALT_SEPARATOR, previous_value) }
end
it "returns HOME/.thor if set" do
- stub(ENV)["HOME"].returns{ "/home/user" }
- Thor::Util.thor_root.must == "/home/user/.thor"
+ stub(ENV)["HOME"].returns{ "/home/user/" }
+ Thor::Util.user_home.must == "/home/user/"
end
it "returns path with HOMEDRIVE and HOMEPATH if set" do
stub(ENV)["HOMEDRIVE"].returns{ "D:/" }
stub(ENV)["HOMEPATH"].returns{ "Documents and Settings/James" }
- Thor::Util.thor_root.must == "D:/Documents and Settings/James/.thor"
+ Thor::Util.user_home.must == "D:/Documents and Settings/James"
end
it "returns APPDATA/.thor if set" do
- stub(ENV)["APPDATA"].returns{ "/home/user" }
- Thor::Util.thor_root.must == "/home/user/.thor"
+ stub(ENV)["APPDATA"].returns{ "/home/user/" }
+ Thor::Util.user_home.must == "/home/user/"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.