|
4 | 4 |
|
5 | 5 | describe Thor::Actions::CreateLink, :unless => windows? do
|
6 | 6 | before do
|
7 |
| - @silence = false |
8 | 7 | @hardlink_to = File.join(Dir.tmpdir, "linkdest.rb")
|
9 | 8 | ::FileUtils.rm_rf(destination_root)
|
10 | 9 | ::FileUtils.rm_rf(@hardlink_to)
|
11 | 10 | end
|
12 | 11 |
|
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) { {} } |
16 | 14 |
|
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 } |
18 | 24 |
|
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) |
21 | 29 | end
|
22 | 30 |
|
23 | 31 | def invoke!
|
24 |
| - capture(:stdout) { @action.invoke! } |
| 32 | + capture(:stdout) { action.invoke! } |
25 | 33 | end
|
26 | 34 |
|
27 | 35 | def revoke!
|
28 |
| - capture(:stdout) { @action.revoke! } |
29 |
| - end |
30 |
| - |
31 |
| - def silence! |
32 |
| - @silence = true |
| 36 | + capture(:stdout) { action.revoke! } |
33 | 37 | end
|
34 | 38 |
|
35 | 39 | 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 |
42 | 49 | end
|
43 | 50 |
|
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 |
50 | 61 | end
|
51 | 62 |
|
52 | 63 | it "creates a symbolic link by default" do
|
53 |
| - create_link("doc/config.rb") |
54 | 64 | invoke!
|
55 | 65 | destination_path = File.join(destination_root, "doc/config.rb")
|
56 | 66 | expect(File.exist?(destination_path)).to be true
|
57 | 67 | expect(File.symlink?(destination_path)).to be true
|
58 | 68 | end
|
59 | 69 |
|
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 |
64 | 76 | end
|
65 | 77 |
|
66 | 78 | it "shows created status to the user" do
|
67 |
| - create_link("doc/config.rb") |
68 | 79 | expect(invoke!).to eq(" create doc/config.rb\n")
|
69 | 80 | end
|
70 | 81 |
|
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 |
75 | 87 | end
|
76 | 88 | end
|
77 | 89 |
|
78 | 90 | describe "#identical?" do
|
79 | 91 | 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 |
82 | 93 | 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 |
84 | 108 | end
|
85 | 109 | end
|
86 | 110 |
|
87 | 111 | describe "#revoke!" do
|
88 | 112 | it "removes the symbolic link of non-existent destination" do
|
89 |
| - create_link("doc/config.rb") |
90 | 113 | invoke!
|
91 |
| - File.delete(@tempfile.path) |
| 114 | + File.delete(tempfile.path) |
92 | 115 | revoke!
|
93 |
| - expect(File.symlink?(@action.destination)).to be false |
| 116 | + expect(File.symlink?(action.destination)).to be false |
94 | 117 | end
|
95 | 118 | end
|
96 | 119 | end
|
0 commit comments