wijet-thor 0.14.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. data/CHANGELOG.rdoc +103 -0
  2. data/LICENSE +20 -0
  3. data/README.md +307 -0
  4. data/Thorfile +24 -0
  5. data/bin/rake2thor +86 -0
  6. data/bin/thor +6 -0
  7. data/lib/thor.rb +334 -0
  8. data/lib/thor/actions.rb +314 -0
  9. data/lib/thor/actions/create_file.rb +105 -0
  10. data/lib/thor/actions/create_link.rb +57 -0
  11. data/lib/thor/actions/directory.rb +93 -0
  12. data/lib/thor/actions/empty_directory.rb +134 -0
  13. data/lib/thor/actions/file_manipulation.rb +270 -0
  14. data/lib/thor/actions/inject_into_file.rb +109 -0
  15. data/lib/thor/base.rb +579 -0
  16. data/lib/thor/core_ext/file_binary_read.rb +9 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  18. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  19. data/lib/thor/error.rb +30 -0
  20. data/lib/thor/group.rb +273 -0
  21. data/lib/thor/invocation.rb +168 -0
  22. data/lib/thor/parser.rb +4 -0
  23. data/lib/thor/parser/argument.rb +67 -0
  24. data/lib/thor/parser/arguments.rb +161 -0
  25. data/lib/thor/parser/option.rb +120 -0
  26. data/lib/thor/parser/options.rb +173 -0
  27. data/lib/thor/rake_compat.rb +66 -0
  28. data/lib/thor/runner.rb +309 -0
  29. data/lib/thor/shell.rb +88 -0
  30. data/lib/thor/shell/basic.rb +290 -0
  31. data/lib/thor/shell/color.rb +108 -0
  32. data/lib/thor/shell/html.rb +121 -0
  33. data/lib/thor/task.rb +114 -0
  34. data/lib/thor/util.rb +229 -0
  35. data/lib/thor/version.rb +3 -0
  36. data/spec/actions/create_file_spec.rb +170 -0
  37. data/spec/actions/directory_spec.rb +136 -0
  38. data/spec/actions/empty_directory_spec.rb +98 -0
  39. data/spec/actions/file_manipulation_spec.rb +310 -0
  40. data/spec/actions/inject_into_file_spec.rb +135 -0
  41. data/spec/actions_spec.rb +322 -0
  42. data/spec/base_spec.rb +269 -0
  43. data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  44. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  45. data/spec/fixtures/application.rb +2 -0
  46. data/spec/fixtures/bundle/execute.rb +6 -0
  47. data/spec/fixtures/bundle/main.thor +1 -0
  48. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  49. data/spec/fixtures/doc/README +3 -0
  50. data/spec/fixtures/doc/block_helper.rb +3 -0
  51. data/spec/fixtures/doc/components/.empty_directory +0 -0
  52. data/spec/fixtures/doc/config.rb +1 -0
  53. data/spec/fixtures/group.thor +114 -0
  54. data/spec/fixtures/invoke.thor +112 -0
  55. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  56. data/spec/fixtures/script.thor +184 -0
  57. data/spec/fixtures/task.thor +10 -0
  58. data/spec/group_spec.rb +178 -0
  59. data/spec/invocation_spec.rb +100 -0
  60. data/spec/parser/argument_spec.rb +47 -0
  61. data/spec/parser/arguments_spec.rb +64 -0
  62. data/spec/parser/option_spec.rb +202 -0
  63. data/spec/parser/options_spec.rb +319 -0
  64. data/spec/rake_compat_spec.rb +68 -0
  65. data/spec/register_spec.rb +104 -0
  66. data/spec/runner_spec.rb +210 -0
  67. data/spec/shell/basic_spec.rb +223 -0
  68. data/spec/shell/color_spec.rb +41 -0
  69. data/spec/shell/html_spec.rb +27 -0
  70. data/spec/shell_spec.rb +47 -0
  71. data/spec/spec_helper.rb +54 -0
  72. data/spec/task_spec.rb +74 -0
  73. data/spec/thor_spec.rb +334 -0
  74. data/spec/util_spec.rb +163 -0
  75. metadata +193 -0
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'thor/rake_compat'
3
+ require 'rake/tasklib'
4
+
5
+ class RakeTask < Rake::TaskLib
6
+ def initialize
7
+ define
8
+ end
9
+
10
+ def define
11
+ desc "Say it's cool"
12
+ task :cool do
13
+ puts "COOL"
14
+ end
15
+
16
+ namespace :hiper_mega do
17
+ task :super do
18
+ puts "HIPER MEGA SUPER"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class ThorTask < Thor
25
+ include Thor::RakeCompat
26
+ RakeTask.new
27
+ end
28
+
29
+ describe Thor::RakeCompat do
30
+ it "sets the rakefile application" do
31
+ ["rake_compat_spec.rb", "Thorfile"].should include(Rake.application.rakefile)
32
+ end
33
+
34
+ it "adds rake tasks to thor classes too" do
35
+ task = ThorTask.tasks["cool"]
36
+ task.should be
37
+ end
38
+
39
+ it "uses rake tasks descriptions on thor" do
40
+ ThorTask.tasks["cool"].description.should == "Say it's cool"
41
+ end
42
+
43
+ it "gets usage from rake tasks name" do
44
+ ThorTask.tasks["cool"].usage.should == "cool"
45
+ end
46
+
47
+ it "uses non namespaced name as description if non is available" do
48
+ ThorTask::HiperMega.tasks["super"].description.should == "super"
49
+ end
50
+
51
+ it "converts namespaces to classes" do
52
+ ThorTask.const_get(:HiperMega).should == ThorTask::HiperMega
53
+ end
54
+
55
+ it "does not add tasks from higher namespaces in lowers namespaces" do
56
+ ThorTask.tasks["super"].should_not be
57
+ end
58
+
59
+ it "invoking the thor task invokes the rake task" do
60
+ capture(:stdout) do
61
+ ThorTask.start ["cool"]
62
+ end.should == "COOL\n"
63
+
64
+ capture(:stdout) do
65
+ ThorTask::HiperMega.start ["super"]
66
+ end.should == "HIPER MEGA SUPER\n"
67
+ end
68
+ end
@@ -0,0 +1,104 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class BoringVendorProvidedCLI < Thor
4
+ desc "boring", "do boring stuff"
5
+ def boring
6
+ puts "bored. <yawn>"
7
+ end
8
+ end
9
+
10
+ class ExcitingPluginCLI < Thor
11
+ desc "hooray", "say hooray!"
12
+ def hooray
13
+ puts "hooray!"
14
+ end
15
+
16
+ desc "fireworks", "exciting fireworks!"
17
+ def fireworks
18
+ puts "kaboom!"
19
+ end
20
+ end
21
+
22
+ class SuperSecretPlugin < Thor
23
+ default_task :squirrel
24
+
25
+ desc "squirrel", "All of secret squirrel's secrets"
26
+ def squirrel
27
+ puts "I love nuts"
28
+ end
29
+ end
30
+
31
+ class GroupPlugin < Thor::Group
32
+ desc "part one"
33
+ def part_one
34
+ puts "part one"
35
+ end
36
+
37
+ desc "part two"
38
+ def part_two
39
+ puts "part two"
40
+ end
41
+ end
42
+
43
+
44
+ BoringVendorProvidedCLI.register(
45
+ ExcitingPluginCLI,
46
+ "exciting",
47
+ "do exciting things",
48
+ "Various non-boring actions")
49
+
50
+ BoringVendorProvidedCLI.register(
51
+ SuperSecretPlugin,
52
+ "secret",
53
+ "secret stuff",
54
+ "Nothing to see here. Move along.",
55
+ :hide => true)
56
+
57
+ BoringVendorProvidedCLI.register(
58
+ GroupPlugin,
59
+ 'groupwork',
60
+ "Do a bunch of things in a row",
61
+ "purple monkey dishwasher")
62
+
63
+ describe ".register-ing a Thor subclass" do
64
+ it "registers the plugin as a subcommand" do
65
+ fireworks_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[exciting fireworks]) }
66
+ fireworks_output.should == "kaboom!\n"
67
+ end
68
+
69
+ it "includes the plugin's usage in the help" do
70
+ help_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[help]) }
71
+ help_output.should include('do exciting things')
72
+ end
73
+
74
+ context "when $thor_runner is false" do
75
+ it "includes the plugin's subcommand name in subcommand's help" do
76
+ begin
77
+ $thor_runner = false
78
+ help_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[exciting]) }
79
+ help_output.should include('thor exciting_plugin_c_l_i fireworks')
80
+ ensure
81
+ $thor_runner = true
82
+ end
83
+ end
84
+ end
85
+
86
+ context "when hidden" do
87
+ it "omits the hidden plugin's usage from the help" do
88
+ help_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[help]) }
89
+ help_output.should_not include('secret stuff')
90
+ end
91
+
92
+ it "registers the plugin as a subcommand" do
93
+ secret_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[secret squirrel]) }
94
+ secret_output.should == "I love nuts\n"
95
+ end
96
+ end
97
+ end
98
+
99
+ describe ".register-ing a Thor::Group subclass" do
100
+ it "registers the group as a single command" do
101
+ group_output = capture(:stdout) { BoringVendorProvidedCLI.start(%w[groupwork]) }
102
+ group_output.should == "part one\npart two\n"
103
+ end
104
+ end
@@ -0,0 +1,210 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+ require 'thor/runner'
3
+
4
+ describe Thor::Runner do
5
+ describe "#help" do
6
+ it "shows information about Thor::Runner itself" do
7
+ capture(:stdout){ Thor::Runner.start(["help"]) }.should =~ /List the available thor tasks/
8
+ end
9
+
10
+ it "shows information about an specific Thor::Runner task" do
11
+ content = capture(:stdout){ Thor::Runner.start(["help", "list"]) }
12
+ content.should =~ /List the available thor tasks/
13
+ content.should_not =~ /help \[TASK\]/
14
+ end
15
+
16
+ it "shows information about a specific Thor class" do
17
+ content = capture(:stdout){ Thor::Runner.start(["help", "my_script"]) }
18
+ content.should =~ /zoo\s+# zoo around/m
19
+ end
20
+
21
+ it "shows information about an specific task from an specific Thor class" do
22
+ content = capture(:stdout){ Thor::Runner.start(["help", "my_script:zoo"]) }
23
+ content.should =~ /zoo around/
24
+ content.should_not =~ /help \[TASK\]/
25
+ end
26
+
27
+ it "shows information about a specific Thor group class" do
28
+ content = capture(:stdout){ Thor::Runner.start(["help", "my_counter"]) }
29
+ content.should =~ /my_counter N/
30
+ end
31
+
32
+ it "raises error if a class/task cannot be found" do
33
+ content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
34
+ content.strip.should == 'Could not find task "unknown" in "default" namespace.'
35
+ end
36
+ end
37
+
38
+ describe "#start" do
39
+ it "invokes a task from Thor::Runner" do
40
+ ARGV.replace ["list"]
41
+ capture(:stdout){ Thor::Runner.start }.should =~ /my_counter N/
42
+ end
43
+
44
+ it "invokes a task from a specific Thor class" do
45
+ ARGV.replace ["my_script:zoo"]
46
+ Thor::Runner.start.should be_true
47
+ end
48
+
49
+ it "invokes the default task from a specific Thor class if none is specified" do
50
+ ARGV.replace ["my_script"]
51
+ Thor::Runner.start.should == "default task"
52
+ end
53
+
54
+ it "forwads arguments to the invoked task" do
55
+ ARGV.replace ["my_script:animal", "horse"]
56
+ Thor::Runner.start.should == ["horse"]
57
+ end
58
+
59
+ it "invokes tasks through shortcuts" do
60
+ ARGV.replace ["my_script", "-T", "horse"]
61
+ Thor::Runner.start.should == ["horse"]
62
+ end
63
+
64
+ it "invokes a Thor::Group" do
65
+ ARGV.replace ["my_counter", "1", "2", "--third", "3"]
66
+ Thor::Runner.start.should == [1, 2, 3]
67
+ end
68
+
69
+ it "raises an error if class/task can't be found" do
70
+ ARGV.replace ["unknown"]
71
+ content = capture(:stderr){ Thor::Runner.start }
72
+ content.strip.should == 'Could not find task "unknown" in "default" namespace.'
73
+ end
74
+
75
+ it "does not swallow NoMethodErrors that occur inside the called method" do
76
+ ARGV.replace ["my_script:call_unexistent_method"]
77
+ lambda { Thor::Runner.start }.should raise_error(NoMethodError)
78
+ end
79
+
80
+ it "does not swallow Thor::Group InvocationError" do
81
+ ARGV.replace ["whiny_generator"]
82
+ lambda { Thor::Runner.start }.should raise_error(ArgumentError, /Are you sure it has arity equals to 0\?/)
83
+ end
84
+
85
+ it "does not swallow Thor InvocationError" do
86
+ ARGV.replace ["my_script:animal"]
87
+ content = capture(:stderr) { Thor::Runner.start }
88
+ content.strip.should == '"animal" was called incorrectly. Call as "thor my_script:animal TYPE".'
89
+ end
90
+ end
91
+
92
+ describe "tasks" do
93
+ before(:each) do
94
+ @location = "#{File.dirname(__FILE__)}/fixtures/task.thor"
95
+ @original_yaml = {
96
+ "random" => {
97
+ :location => @location,
98
+ :filename => "4a33b894ffce85d7b412fc1b36f88fe0",
99
+ :namespaces => ["amazing"]
100
+ }
101
+ }
102
+
103
+ root_file = File.join(Thor::Util.thor_root, "thor.yml")
104
+
105
+ # Stub load and save to avoid thor.yaml from being overwritten
106
+ YAML.stub!(:load_file).and_return(@original_yaml)
107
+ File.stub!(:exists?).with(root_file).and_return(true)
108
+ File.stub!(:open).with(root_file, "w")
109
+ end
110
+
111
+ describe "list" do
112
+ it "gives a list of the available tasks" do
113
+ ARGV.replace ["list"]
114
+ content = capture(:stdout) { Thor::Runner.start }
115
+ content.should =~ /amazing:describe NAME\s+# say that someone is amazing/m
116
+ end
117
+
118
+ it "gives a list of the available Thor::Group classes" do
119
+ ARGV.replace ["list"]
120
+ capture(:stdout) { Thor::Runner.start }.should =~ /my_counter N/
121
+ end
122
+
123
+ it "can filter a list of the available tasks by --group" do
124
+ ARGV.replace ["list", "--group", "standard"]
125
+ capture(:stdout) { Thor::Runner.start }.should =~ /amazing:describe NAME/
126
+ ARGV.replace []
127
+ capture(:stdout) { Thor::Runner.start }.should_not =~ /my_script:animal TYPE/
128
+ ARGV.replace ["list", "--group", "script"]
129
+ capture(:stdout) { Thor::Runner.start }.should =~ /my_script:animal TYPE/
130
+ end
131
+
132
+ it "can skip all filters to show all tasks using --all" do
133
+ ARGV.replace ["list", "--all"]
134
+ content = capture(:stdout) { Thor::Runner.start }
135
+ content.should =~ /amazing:describe NAME/
136
+ content.should =~ /my_script:animal TYPE/
137
+ end
138
+
139
+ it "doesn't list superclass tasks in the subclass" do
140
+ ARGV.replace ["list"]
141
+ capture(:stdout) { Thor::Runner.start }.should_not =~ /amazing:help/
142
+ end
143
+
144
+ it "presents tasks in the default namespace with an empty namespace" do
145
+ ARGV.replace ["list"]
146
+ capture(:stdout) { Thor::Runner.start }.should =~ /^thor :cow\s+# prints 'moo'/m
147
+ end
148
+
149
+ it "runs tasks with an empty namespace from the default namespace" do
150
+ ARGV.replace [":task_conflict"]
151
+ capture(:stdout) { Thor::Runner.start }.should == "task\n"
152
+ end
153
+
154
+ it "runs groups even when there is a task with the same name" do
155
+ ARGV.replace ["task_conflict"]
156
+ capture(:stdout) { Thor::Runner.start }.should == "group\n"
157
+ end
158
+
159
+ it "runs tasks with no colon in the default namespace" do
160
+ ARGV.replace ["cow"]
161
+ capture(:stdout) { Thor::Runner.start }.should == "moo\n"
162
+ end
163
+ end
164
+
165
+ describe "uninstall" do
166
+ before(:each) do
167
+ path = File.join(Thor::Util.thor_root, @original_yaml["random"][:filename])
168
+ FileUtils.should_receive(:rm_rf).with(path)
169
+ end
170
+
171
+ it "uninstalls existing thor modules" do
172
+ silence(:stdout) { Thor::Runner.start(["uninstall", "random"]) }
173
+ end
174
+ end
175
+
176
+ describe "installed" do
177
+ before(:each) do
178
+ Dir.should_receive(:[]).and_return([])
179
+ end
180
+
181
+ it "displays the modules installed in a pretty way" do
182
+ stdout = capture(:stdout) { Thor::Runner.start(["installed"]) }
183
+ stdout.should =~ /random\s*amazing/
184
+ stdout.should =~ /amazing:describe NAME\s+# say that someone is amazing/m
185
+ end
186
+ end
187
+
188
+ describe "install/update" do
189
+ before(:each) do
190
+ FileUtils.stub!(:mkdir_p)
191
+ FileUtils.stub!(:touch)
192
+ $stdin.stub!(:gets).and_return("Y")
193
+
194
+ path = File.join(Thor::Util.thor_root, Digest::MD5.hexdigest(@location + "random"))
195
+ File.should_receive(:open).with(path, "w")
196
+ end
197
+
198
+ it "updates existing thor files" do
199
+ path = File.join(Thor::Util.thor_root, @original_yaml["random"][:filename])
200
+ File.should_receive(:delete).with(path)
201
+ silence(:stdout) { Thor::Runner.start(["update", "random"]) }
202
+ end
203
+
204
+ it "installs thor files" do
205
+ ARGV.replace ["install", @location]
206
+ silence(:stdout) { Thor::Runner.start }
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,223 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Thor::Shell::Basic do
4
+ def shell
5
+ @shell ||= Thor::Shell::Basic.new
6
+ end
7
+
8
+ describe "#padding" do
9
+ it "cannot be set to below zero" do
10
+ shell.padding = 10
11
+ shell.padding.should == 10
12
+
13
+ shell.padding = -1
14
+ shell.padding.should == 0
15
+ end
16
+ end
17
+
18
+ describe "#ask" do
19
+ it "prints a message to the user and gets the response" do
20
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
21
+ $stdin.should_receive(:gets).and_return('Sure')
22
+ shell.ask("Should I overwrite it?").should == "Sure"
23
+ end
24
+ end
25
+
26
+ describe "#yes?" do
27
+ it "asks the user and returns true if the user replies yes" do
28
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
29
+ $stdin.should_receive(:gets).and_return('y')
30
+ shell.yes?("Should I overwrite it?").should === true
31
+
32
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
33
+ $stdin.should_receive(:gets).and_return('n')
34
+ shell.yes?("Should I overwrite it?").should_not === true
35
+ end
36
+ end
37
+
38
+ describe "#no?" do
39
+ it "asks the user and returns true if the user replies no" do
40
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
41
+ $stdin.should_receive(:gets).and_return('n')
42
+ shell.no?("Should I overwrite it?").should === true
43
+
44
+ $stdout.should_receive(:print).with("Should I overwrite it? ")
45
+ $stdin.should_receive(:gets).and_return('Yes')
46
+ shell.no?("Should I overwrite it?").should === false
47
+ end
48
+ end
49
+
50
+ describe "#say" do
51
+ it "prints a message to the user" do
52
+ $stdout.should_receive(:puts).with("Running...")
53
+ shell.say("Running...")
54
+ end
55
+
56
+ it "prints a message to the user without new line if it ends with a whitespace" do
57
+ $stdout.should_receive(:print).with("Running... ")
58
+ shell.say("Running... ")
59
+ end
60
+
61
+ it "prints a message to the user without new line" do
62
+ $stdout.should_receive(:print).with("Running...")
63
+ shell.say("Running...", nil, false)
64
+ end
65
+ end
66
+
67
+ describe "#say_status" do
68
+ it "prints a message to the user with status" do
69
+ $stdout.should_receive(:puts).with(" create ~/.thor/task.thor")
70
+ shell.say_status(:create, "~/.thor/task.thor")
71
+ end
72
+
73
+ it "always use new line" do
74
+ $stdout.should_receive(:puts).with(" create ")
75
+ shell.say_status(:create, "")
76
+ end
77
+
78
+ it "does not print a message if base is muted" do
79
+ shell.should_receive(:mute?).and_return(true)
80
+ $stdout.should_not_receive(:puts)
81
+
82
+ shell.mute do
83
+ shell.say_status(:created, "~/.thor/task.thor")
84
+ end
85
+ end
86
+
87
+ it "does not print a message if base is set to quiet" do
88
+ base = MyCounter.new [1,2]
89
+ base.should_receive(:options).and_return(:quiet => true)
90
+
91
+ $stdout.should_not_receive(:puts)
92
+ shell.base = base
93
+ shell.say_status(:created, "~/.thor/task.thor")
94
+ end
95
+
96
+ it "does not print a message if log status is set to false" do
97
+ $stdout.should_not_receive(:puts)
98
+ shell.say_status(:created, "~/.thor/task.thor", false)
99
+ end
100
+
101
+ it "uses padding to set messages left margin" do
102
+ shell.padding = 2
103
+ $stdout.should_receive(:puts).with(" create ~/.thor/task.thor")
104
+ shell.say_status(:create, "~/.thor/task.thor")
105
+ end
106
+ end
107
+
108
+ describe "#print_table" do
109
+ before(:each) do
110
+ @table = []
111
+ @table << ["abc", "#123", "first three"]
112
+ @table << ["", "#0", "empty"]
113
+ @table << ["xyz", "#786", "last three"]
114
+ end
115
+
116
+ it "prints a table" do
117
+ content = capture(:stdout){ shell.print_table(@table) }
118
+ content.should == <<-TABLE
119
+ abc #123 first three
120
+ #0 empty
121
+ xyz #786 last three
122
+ TABLE
123
+ end
124
+
125
+ it "prints a table with identation" do
126
+ content = capture(:stdout){ shell.print_table(@table, :ident => 2) }
127
+ content.should == <<-TABLE
128
+ abc #123 first three
129
+ #0 empty
130
+ xyz #786 last three
131
+ TABLE
132
+ end
133
+
134
+ it "uses maximum terminal width" do
135
+ shell.should_receive(:terminal_width).and_return(20)
136
+ content = capture(:stdout){ shell.print_table(@table, :ident => 2, :truncate => true) }
137
+ content.should == <<-TABLE
138
+ abc #123 firs...
139
+ #0 empty
140
+ xyz #786 last...
141
+ TABLE
142
+ end
143
+
144
+ it "honors the colwidth option" do
145
+ content = capture(:stdout){ shell.print_table(@table, :colwidth => 10)}
146
+ content.should == <<-TABLE
147
+ abc #123 first three
148
+ #0 empty
149
+ xyz #786 last three
150
+ TABLE
151
+ end
152
+ end
153
+
154
+ describe "#file_collision" do
155
+ it "shows a menu with options" do
156
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
157
+ $stdin.should_receive(:gets).and_return('n')
158
+ shell.file_collision('foo')
159
+ end
160
+
161
+ it "returns true if the user choose default option" do
162
+ $stdout.stub!(:print)
163
+ $stdin.should_receive(:gets).and_return('')
164
+ shell.file_collision('foo').should be_true
165
+ end
166
+
167
+ it "returns false if the user choose no" do
168
+ $stdout.stub!(:print)
169
+ $stdin.should_receive(:gets).and_return('n')
170
+ shell.file_collision('foo').should be_false
171
+ end
172
+
173
+ it "returns true if the user choose yes" do
174
+ $stdout.stub!(:print)
175
+ $stdin.should_receive(:gets).and_return('y')
176
+ shell.file_collision('foo').should be_true
177
+ end
178
+
179
+ it "shows help usage if the user choose help" do
180
+ $stdout.stub!(:print)
181
+ $stdin.should_receive(:gets).and_return('h')
182
+ $stdin.should_receive(:gets).and_return('n')
183
+ help = capture(:stdout){ shell.file_collision('foo') }
184
+ help.should =~ /h \- help, show this help/
185
+ end
186
+
187
+ it "quits if the user choose quit" do
188
+ $stdout.stub!(:print)
189
+ $stdout.should_receive(:puts).with('Aborting...')
190
+ $stdin.should_receive(:gets).and_return('q')
191
+
192
+ lambda {
193
+ shell.file_collision('foo')
194
+ }.should raise_error(SystemExit)
195
+ end
196
+
197
+ it "always returns true if the user choose always" do
198
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqh] ')
199
+ $stdin.should_receive(:gets).and_return('a')
200
+
201
+ shell.file_collision('foo').should be_true
202
+
203
+ $stdout.should_not_receive(:print)
204
+ shell.file_collision('foo').should be_true
205
+ end
206
+
207
+ describe "when a block is given" do
208
+ it "displays diff options to the user" do
209
+ $stdout.should_receive(:print).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ')
210
+ $stdin.should_receive(:gets).and_return('s')
211
+ shell.file_collision('foo'){ }
212
+ end
213
+
214
+ it "invokes the diff command" do
215
+ $stdout.stub!(:print)
216
+ $stdin.should_receive(:gets).and_return('d')
217
+ $stdin.should_receive(:gets).and_return('n')
218
+ shell.should_receive(:system).with(/diff -u/)
219
+ capture(:stdout){ shell.file_collision('foo'){ } }
220
+ end
221
+ end
222
+ end
223
+ end