Skip to content

Commit 506950f

Browse files
committed
Fix relative_to_original_destination_root and better tests
Many broken edge cases were present, such as if a later substring of the path matched the root or if the root only matched a subpart of a directory.
1 parent 17efb65 commit 506950f

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
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.sub!(@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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,19 @@ 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+
8999
it "removes the root path from the absolute path only once" do
90100
runner.destination_root = "/app"
91-
expect(runner.relative_to_original_destination_root("/app/project/application")).to eq("project/application")
101+
expect(runner.relative_to_original_destination_root("/app/app/project")).to eq("app/project")
92102
end
93103

94104
it "does not fail with files containing regexp characters" do

0 commit comments

Comments
 (0)