Skip to content

Commit d91e8ca

Browse files
authored
Merge pull request #620 from MaxLap/fix-invalid-path-display
Fix edge cases in `#relative_to_original_destination_root`
2 parents c90d0ab + 506950f commit d91e8ca

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/thor/actions.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def destination_root=(root)
113113
# the script started).
114114
#
115115
def relative_to_original_destination_root(path, remove_dot = true)
116-
path = path.dup
117-
if path.gsub!(@destination_stack[0], ".")
116+
root = @destination_stack[0]
117+
if path.start_with?(root) && [File::SEPARATOR, File::ALT_SEPARATOR, nil, ''].include?(path[root.size..root.size])
118+
path = path.dup
119+
path[0...root.size] = '.'
118120
remove_dot ? (path[2..-1] || "") : path
119121
else
120122
path

spec/actions_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ def file
8686
expect(runner.relative_to_original_destination_root("/test/file")).to eq("/test/file")
8787
end
8888

89+
it "doesn't remove the root path from the absolute path if it is not at the begining" do
90+
runner.destination_root = "/app"
91+
expect(runner.relative_to_original_destination_root("/something/app/project")).to eq("/something/app/project")
92+
end
93+
94+
it "doesn't removes the root path from the absolute path only if it is only the partial name of the directory" do
95+
runner.destination_root = "/app"
96+
expect(runner.relative_to_original_destination_root("/application/project")).to eq("/application/project")
97+
end
98+
99+
it "removes the root path from the absolute path only once" do
100+
runner.destination_root = "/app"
101+
expect(runner.relative_to_original_destination_root("/app/app/project")).to eq("app/project")
102+
end
103+
89104
it "does not fail with files containing regexp characters" do
90105
runner = MyCounter.new([1], {}, :destination_root => File.join(destination_root, "fo[o-b]ar"))
91106
expect(runner.relative_to_original_destination_root("bar")).to eq("bar")

0 commit comments

Comments
 (0)