Skip to content

Commit

Permalink
Make user home logic to its own method so it can be reused.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 4, 2009
1 parent 2fbf19a commit ee97ca0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 23 additions & 17 deletions lib/thor/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions spec/util_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ee97ca0

Please sign in to comment.