Skip to content

Commit ac240ce

Browse files
authored
Merge pull request #701 from chrisandreae/create-relative-link
Handle relative source path in create_link #identical?
2 parents 9933018 + ae1cea9 commit ac240ce

File tree

2 files changed

+66
-42
lines changed

2 files changed

+66
-42
lines changed

lib/thor/actions/create_link.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class CreateLink < CreateFile #:nodoc:
3333
# Boolean:: true if it is identical, false otherwise.
3434
#
3535
def identical?
36-
exists? && File.identical?(render, destination)
36+
source = File.expand_path(render, File.dirname(destination))
37+
exists? && File.identical?(source, destination)
3738
end
3839

3940
def invoke!

spec/actions/create_link_spec.rb

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,116 @@
44

55
describe Thor::Actions::CreateLink, :unless => windows? do
66
before do
7-
@silence = false
87
@hardlink_to = File.join(Dir.tmpdir, "linkdest.rb")
98
::FileUtils.rm_rf(destination_root)
109
::FileUtils.rm_rf(@hardlink_to)
1110
end
1211

13-
def create_link(destination = nil, config = {}, options = {})
14-
@base = MyCounter.new([1, 2], options, :destination_root => destination_root)
15-
allow(@base).to receive(:file_name).and_return("rdoc")
12+
let(:config) { {} }
13+
let(:options) { {} }
1614

17-
@tempfile = Tempfile.new("config.rb")
15+
let(:base) do
16+
base = MyCounter.new([1, 2], options, :destination_root => destination_root)
17+
allow(base).to receive(:file_name).and_return("rdoc")
18+
base
19+
end
20+
21+
let(:tempfile) { Tempfile.new("config.rb") }
22+
23+
let(:source) { tempfile.path }
1824

19-
@action = Thor::Actions::CreateLink.new(@base, destination, @tempfile.path,
20-
{:verbose => !@silence}.merge(config))
25+
let(:destination) { "doc/config.rb" }
26+
27+
let(:action) do
28+
Thor::Actions::CreateLink.new(base, destination, source, config)
2129
end
2230

2331
def invoke!
24-
capture(:stdout) { @action.invoke! }
32+
capture(:stdout) { action.invoke! }
2533
end
2634

2735
def revoke!
28-
capture(:stdout) { @action.revoke! }
29-
end
30-
31-
def silence!
32-
@silence = true
36+
capture(:stdout) { action.revoke! }
3337
end
3438

3539
describe "#invoke!" do
36-
it "creates a symbolic link for :symbolic => true" do
37-
create_link("doc/config.rb", :symbolic => true)
38-
invoke!
39-
destination_path = File.join(destination_root, "doc/config.rb")
40-
expect(File.exist?(destination_path)).to be true
41-
expect(File.symlink?(destination_path)).to be true
40+
context "specifying :symbolic => true" do
41+
let(:config) { {:symbolic => true} }
42+
43+
it "creates a symbolic link" do
44+
invoke!
45+
destination_path = File.join(destination_root, "doc/config.rb")
46+
expect(File.exist?(destination_path)).to be true
47+
expect(File.symlink?(destination_path)).to be true
48+
end
4249
end
4350

44-
it "creates a hard link for :symbolic => false" do
45-
create_link(@hardlink_to, :symbolic => false)
46-
invoke!
47-
destination_path = @hardlink_to
48-
expect(File.exist?(destination_path)).to be true
49-
expect(File.symlink?(destination_path)).to be false
51+
context "specifying :symbolic => false" do
52+
let(:config) { {:symbolic => false} }
53+
let(:destination) { @hardlink_to }
54+
55+
it "creates a hard link" do
56+
invoke!
57+
destination_path = @hardlink_to
58+
expect(File.exist?(destination_path)).to be true
59+
expect(File.symlink?(destination_path)).to be false
60+
end
5061
end
5162

5263
it "creates a symbolic link by default" do
53-
create_link("doc/config.rb")
5464
invoke!
5565
destination_path = File.join(destination_root, "doc/config.rb")
5666
expect(File.exist?(destination_path)).to be true
5767
expect(File.symlink?(destination_path)).to be true
5868
end
5969

60-
it "does not create a link if pretending" do
61-
create_link("doc/config.rb", {}, :pretend => true)
62-
invoke!
63-
expect(File.exist?(File.join(destination_root, "doc/config.rb"))).to be false
70+
context "specifying :pretend => true" do
71+
let(:options) { {:pretend => true} }
72+
it "does not create a link" do
73+
invoke!
74+
expect(File.exist?(File.join(destination_root, "doc/config.rb"))).to be false
75+
end
6476
end
6577

6678
it "shows created status to the user" do
67-
create_link("doc/config.rb")
6879
expect(invoke!).to eq(" create doc/config.rb\n")
6980
end
7081

71-
it "does not show any information if log status is false" do
72-
silence!
73-
create_link("doc/config.rb")
74-
expect(invoke!).to be_empty
82+
context "specifying :verbose => false" do
83+
let(:config) { {:verbose => false} }
84+
it "does not show any information" do
85+
expect(invoke!).to be_empty
86+
end
7587
end
7688
end
7789

7890
describe "#identical?" do
7991
it "returns true if the destination link exists and is identical" do
80-
create_link("doc/config.rb")
81-
expect(@action.identical?).to be false
92+
expect(action.identical?).to be false
8293
invoke!
83-
expect(@action.identical?).to be true
94+
expect(action.identical?).to be true
95+
end
96+
97+
context "with source path relative to destination" do
98+
let(:source) do
99+
destination_path = File.dirname(File.join(destination_root, destination))
100+
Pathname.new(super()).relative_path_from(Pathname.new(destination_path)).to_s
101+
end
102+
103+
it "returns true if the destination link exists and is identical" do
104+
expect(action.identical?).to be false
105+
invoke!
106+
expect(action.identical?).to be true
107+
end
84108
end
85109
end
86110

87111
describe "#revoke!" do
88112
it "removes the symbolic link of non-existent destination" do
89-
create_link("doc/config.rb")
90113
invoke!
91-
File.delete(@tempfile.path)
114+
File.delete(tempfile.path)
92115
revoke!
93-
expect(File.symlink?(@action.destination)).to be false
116+
expect(File.symlink?(action.destination)).to be false
94117
end
95118
end
96119
end

0 commit comments

Comments
 (0)