Skip to content

Commit 916b6aa

Browse files
committed
Refactor create_link_spec to allow testing varying destinations
1 parent a38582c commit 916b6aa

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

spec/actions/create_link_spec.rb

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,104 @@
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 }
24+
25+
let(:destination) { "doc/config.rb" }
1826

19-
@action = Thor::Actions::CreateLink.new(@base, destination, @tempfile.path,
20-
{:verbose => !@silence}.merge(config))
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
8496
end
8597
end
8698

8799
describe "#revoke!" do
88100
it "removes the symbolic link of non-existent destination" do
89-
create_link("doc/config.rb")
90101
invoke!
91-
File.delete(@tempfile.path)
102+
File.delete(tempfile.path)
92103
revoke!
93-
expect(File.symlink?(@action.destination)).to be false
104+
expect(File.symlink?(action.destination)).to be false
94105
end
95106
end
96107
end

0 commit comments

Comments
 (0)