Permalink
Browse files

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

  • Loading branch information...
1 parent 2fbf19a commit ee97ca0ae7cb07d7906d8617b717c44d8710557a @josevalim josevalim committed Jul 4, 2009
Showing with 42 additions and 29 deletions.
  1. +1 −1 lib/thor/actions.rb
  2. +23 −17 lib/thor/util.rb
  3. +2 −2 spec/actions_spec.rb
  4. +16 −9 spec/util_spec.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
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
View
@@ -182,15 +182,15 @@ 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
action(:run, "ls", false).must be_empty
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
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,45 +116,46 @@
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
it "returns the windows system path if file cannot be expanded and a separator exists" do
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

0 comments on commit ee97ca0

Please sign in to comment.