thor-dleavitt 0.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/CHANGELOG.md +139 -0
  4. data/LICENSE.md +20 -0
  5. data/README.md +35 -0
  6. data/Thorfile +30 -0
  7. data/bin/thor +6 -0
  8. data/lib/thor.rb +473 -0
  9. data/lib/thor/actions.rb +318 -0
  10. data/lib/thor/actions/create_file.rb +105 -0
  11. data/lib/thor/actions/create_link.rb +60 -0
  12. data/lib/thor/actions/directory.rb +119 -0
  13. data/lib/thor/actions/empty_directory.rb +137 -0
  14. data/lib/thor/actions/file_manipulation.rb +317 -0
  15. data/lib/thor/actions/inject_into_file.rb +109 -0
  16. data/lib/thor/base.rb +654 -0
  17. data/lib/thor/command.rb +136 -0
  18. data/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
  19. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  20. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  21. data/lib/thor/error.rb +32 -0
  22. data/lib/thor/group.rb +282 -0
  23. data/lib/thor/invocation.rb +172 -0
  24. data/lib/thor/parser.rb +4 -0
  25. data/lib/thor/parser/argument.rb +74 -0
  26. data/lib/thor/parser/arguments.rb +171 -0
  27. data/lib/thor/parser/option.rb +121 -0
  28. data/lib/thor/parser/options.rb +218 -0
  29. data/lib/thor/rake_compat.rb +72 -0
  30. data/lib/thor/runner.rb +322 -0
  31. data/lib/thor/shell.rb +88 -0
  32. data/lib/thor/shell/basic.rb +422 -0
  33. data/lib/thor/shell/color.rb +148 -0
  34. data/lib/thor/shell/html.rb +127 -0
  35. data/lib/thor/util.rb +270 -0
  36. data/lib/thor/version.rb +3 -0
  37. data/spec/actions/create_file_spec.rb +170 -0
  38. data/spec/actions/create_link_spec.rb +95 -0
  39. data/spec/actions/directory_spec.rb +169 -0
  40. data/spec/actions/empty_directory_spec.rb +129 -0
  41. data/spec/actions/file_manipulation_spec.rb +382 -0
  42. data/spec/actions/inject_into_file_spec.rb +135 -0
  43. data/spec/actions_spec.rb +331 -0
  44. data/spec/base_spec.rb +291 -0
  45. data/spec/command_spec.rb +80 -0
  46. data/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
  47. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  48. data/spec/exit_condition_spec.rb +19 -0
  49. data/spec/fixtures/application.rb +2 -0
  50. data/spec/fixtures/app{1}/README +3 -0
  51. data/spec/fixtures/bundle/execute.rb +6 -0
  52. data/spec/fixtures/bundle/main.thor +1 -0
  53. data/spec/fixtures/command.thor +10 -0
  54. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  55. data/spec/fixtures/doc/COMMENTER +11 -0
  56. data/spec/fixtures/doc/README +3 -0
  57. data/spec/fixtures/doc/block_helper.rb +3 -0
  58. data/spec/fixtures/doc/config.rb +1 -0
  59. data/spec/fixtures/doc/config.yaml.tt +1 -0
  60. data/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
  61. data/spec/fixtures/enum.thor +10 -0
  62. data/spec/fixtures/group.thor +128 -0
  63. data/spec/fixtures/invoke.thor +118 -0
  64. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  65. data/spec/fixtures/preserve/script.sh +3 -0
  66. data/spec/fixtures/script.thor +220 -0
  67. data/spec/fixtures/subcommand.thor +17 -0
  68. data/spec/group_spec.rb +222 -0
  69. data/spec/helper.rb +67 -0
  70. data/spec/invocation_spec.rb +108 -0
  71. data/spec/parser/argument_spec.rb +53 -0
  72. data/spec/parser/arguments_spec.rb +66 -0
  73. data/spec/parser/option_spec.rb +202 -0
  74. data/spec/parser/options_spec.rb +400 -0
  75. data/spec/rake_compat_spec.rb +72 -0
  76. data/spec/register_spec.rb +197 -0
  77. data/spec/runner_spec.rb +241 -0
  78. data/spec/shell/basic_spec.rb +330 -0
  79. data/spec/shell/color_spec.rb +95 -0
  80. data/spec/shell/html_spec.rb +31 -0
  81. data/spec/shell_spec.rb +47 -0
  82. data/spec/subcommand_spec.rb +30 -0
  83. data/spec/thor_spec.rb +499 -0
  84. data/spec/util_spec.rb +196 -0
  85. data/thor.gemspec +24 -0
  86. metadata +191 -0
@@ -0,0 +1,67 @@
1
+ $TESTING=true
2
+
3
+ require 'simplecov'
4
+ require 'coveralls'
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+ SimpleCov.start
11
+
12
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
13
+ require 'thor'
14
+ require 'thor/group'
15
+ require 'stringio'
16
+
17
+ require 'rdoc'
18
+ require 'rspec'
19
+ require 'diff/lcs' # You need diff/lcs installed to run specs (but not to run Thor).
20
+ require 'fakeweb' # You need fakeweb installed to run specs (but not to run Thor).
21
+
22
+ # Set shell to basic
23
+ $0 = "thor"
24
+ $thor_runner = true
25
+ ARGV.clear
26
+ Thor::Base.shell = Thor::Shell::Basic
27
+
28
+ # Load fixtures
29
+ load File.join(File.dirname(__FILE__), "fixtures", "enum.thor")
30
+ load File.join(File.dirname(__FILE__), "fixtures", "group.thor")
31
+ load File.join(File.dirname(__FILE__), "fixtures", "invoke.thor")
32
+ load File.join(File.dirname(__FILE__), "fixtures", "script.thor")
33
+ load File.join(File.dirname(__FILE__), "fixtures", "subcommand.thor")
34
+ load File.join(File.dirname(__FILE__), "fixtures", "command.thor")
35
+
36
+ RSpec.configure do |config|
37
+ config.before do
38
+ ARGV.replace []
39
+ end
40
+
41
+ config.expect_with :rspec do |c|
42
+ c.syntax = :expect
43
+ end
44
+
45
+ def capture(stream)
46
+ begin
47
+ stream = stream.to_s
48
+ eval "$#{stream} = StringIO.new"
49
+ yield
50
+ result = eval("$#{stream}").string
51
+ ensure
52
+ eval("$#{stream} = #{stream.upcase}")
53
+ end
54
+
55
+ result
56
+ end
57
+
58
+ def source_root
59
+ File.join(File.dirname(__FILE__), 'fixtures')
60
+ end
61
+
62
+ def destination_root
63
+ File.join(File.dirname(__FILE__), 'sandbox')
64
+ end
65
+
66
+ alias :silence :capture
67
+ end
@@ -0,0 +1,108 @@
1
+ require 'helper'
2
+ require 'thor/base'
3
+
4
+ describe Thor::Invocation do
5
+ describe "#invoke" do
6
+ it "invokes a command inside another command" do
7
+ expect(capture(:stdout) { A.new.invoke(:two) }).to eq("2\n3\n")
8
+ end
9
+
10
+ it "invokes a command just once" do
11
+ expect(capture(:stdout) { A.new.invoke(:one) }).to eq("1\n2\n3\n")
12
+ end
13
+
14
+ it "invokes a command just once even if they belongs to different classes" do
15
+ expect(capture(:stdout) { Defined.new.invoke(:one) }).to eq("1\n2\n3\n4\n5\n")
16
+ end
17
+
18
+ it "invokes a command with arguments" do
19
+ expect(A.new.invoke(:five, [5])).to be_true
20
+ expect(A.new.invoke(:five, [7])).to be_false
21
+ end
22
+
23
+ it "invokes the default command if none is given to a Thor class" do
24
+ content = capture(:stdout) { A.new.invoke("b") }
25
+ expect(content).to match(/Commands/)
26
+ expect(content).to match(/LAST_NAME/)
27
+ end
28
+
29
+ it "accepts a class as argument without a command to invoke" do
30
+ content = capture(:stdout) { A.new.invoke(B) }
31
+ expect(content).to match(/Commands/)
32
+ expect(content).to match(/LAST_NAME/)
33
+ end
34
+
35
+ it "accepts a class as argument with a command to invoke" do
36
+ base = A.new([], :last_name => "Valim")
37
+ expect(base.invoke(B, :one, ["Jose"])).to eq("Valim, Jose")
38
+ end
39
+
40
+ it "allows customized options to be given" do
41
+ base = A.new([], :last_name => "Wrong")
42
+ expect(base.invoke(B, :one, ["Jose"], :last_name => "Valim")).to eq("Valim, Jose")
43
+ end
44
+
45
+ it "reparses options in the new class" do
46
+ expect(A.start(["invoker", "--last-name", "Valim"])).to eq("Valim, Jose")
47
+ end
48
+
49
+ it "shares initialize options with invoked class" do
50
+ expect(A.new([], :foo => :bar).invoke("b:two")).to eq({ "foo" => :bar })
51
+ end
52
+
53
+ it "uses default options from invoked class if no matching arguments are given" do
54
+ expect(A.new([]).invoke("b:four")).to eq("default")
55
+ end
56
+
57
+ it "overrides default options if options are passed to the invoker" do
58
+ expect(A.new([], { :defaulted_value => "not default"}).invoke("b:four")).to eq("not default")
59
+ end
60
+
61
+ it "dump configuration values to be used in the invoked class" do
62
+ base = A.new
63
+ expect(base.invoke("b:three").shell).to eq(base.shell)
64
+ end
65
+
66
+ it "allow extra configuration values to be given" do
67
+ base, shell = A.new, Thor::Base.shell.new
68
+ expect(base.invoke("b:three", [], {}, :shell => shell).shell).to eq(shell)
69
+ end
70
+
71
+ it "invokes a Thor::Group and all of its commands" do
72
+ expect(capture(:stdout) { A.new.invoke(:c) }).to eq("1\n2\n3\n")
73
+ end
74
+
75
+ it "does not invoke a Thor::Group twice" do
76
+ base = A.new
77
+ silence(:stdout){ base.invoke(:c) }
78
+ expect(capture(:stdout) { base.invoke(:c) }).to be_empty
79
+ end
80
+
81
+ it "does not invoke any of Thor::Group commands twice" do
82
+ base = A.new
83
+ silence(:stdout){ base.invoke(:c) }
84
+ expect(capture(:stdout) { base.invoke("c:one") }).to be_empty
85
+ end
86
+
87
+ it "raises Thor::UndefinedCommandError if the command can't be found" do
88
+ expect {
89
+ A.new.invoke("foo:bar")
90
+ }.to raise_error(Thor::UndefinedCommandError)
91
+ end
92
+
93
+ it "raises Thor::UndefinedCommandError if the command can't be found even if all commands were already executed" do
94
+ base = C.new
95
+ silence(:stdout){ base.invoke_all }
96
+
97
+ expect {
98
+ base.invoke("foo:bar")
99
+ }.to raise_error(Thor::UndefinedCommandError)
100
+ end
101
+
102
+ it "raises an error if a non Thor class is given" do
103
+ expect {
104
+ A.new.invoke(Object)
105
+ }.to raise_error(RuntimeError, "Expected Thor class, got Object")
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,53 @@
1
+ require 'helper'
2
+ require 'thor/parser'
3
+
4
+ describe Thor::Argument do
5
+
6
+ def argument(name, options={})
7
+ @argument ||= Thor::Argument.new(name, options)
8
+ end
9
+
10
+ describe "errors" do
11
+ it "raises an error if name is not supplied" do
12
+ expect {
13
+ argument(nil)
14
+ }.to raise_error(ArgumentError, "Argument name can't be nil.")
15
+ end
16
+
17
+ it "raises an error if type is unknown" do
18
+ expect {
19
+ argument(:command, :type => :unknown)
20
+ }.to raise_error(ArgumentError, "Type :unknown is not valid for arguments.")
21
+ end
22
+
23
+ it "raises an error if argument is required and has default values" do
24
+ expect {
25
+ argument(:command, :type => :string, :default => "bar", :required => true)
26
+ }.to raise_error(ArgumentError, "An argument cannot be required and have default value.")
27
+ end
28
+
29
+ it "raises an error if enum isn't an array" do
30
+ expect {
31
+ argument(:command, :type => :string, :enum => "bar")
32
+ }.to raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
33
+ end
34
+ end
35
+
36
+ describe "#usage" do
37
+ it "returns usage for string types" do
38
+ expect(argument(:foo, :type => :string).usage).to eq("FOO")
39
+ end
40
+
41
+ it "returns usage for numeric types" do
42
+ expect(argument(:foo, :type => :numeric).usage).to eq("N")
43
+ end
44
+
45
+ it "returns usage for array types" do
46
+ expect(argument(:foo, :type => :array).usage).to eq("one two three")
47
+ end
48
+
49
+ it "returns usage for hash types" do
50
+ expect(argument(:foo, :type => :hash).usage).to eq("key:value")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+ require 'thor/parser'
3
+
4
+ describe Thor::Arguments do
5
+ def create(opts={})
6
+ arguments = opts.map do |type, default|
7
+ options = {:required => default.nil?, :type => type, :default => default}
8
+ Thor::Argument.new(type.to_s, options)
9
+ end
10
+
11
+ arguments.sort!{ |a,b| b.name <=> a.name }
12
+ @opt = Thor::Arguments.new(arguments)
13
+ end
14
+
15
+ def parse(*args)
16
+ @opt.parse(args)
17
+ end
18
+
19
+ describe "#parse" do
20
+ it "parses arguments in the given order" do
21
+ create :string => nil, :numeric => nil
22
+ expect(parse("name", "13")["string"]).to eq("name")
23
+ expect(parse("name", "13")["numeric"]).to eq(13)
24
+ end
25
+
26
+ it "accepts hashes" do
27
+ create :string => nil, :hash => nil
28
+ expect(parse("product", "title:string", "age:integer")["string"]).to eq("product")
29
+ expect(parse("product", "title:string", "age:integer")["hash"]).to eq({ "title" => "string", "age" => "integer"})
30
+ expect(parse("product", "url:http://www.amazon.com/gp/product/123")["hash"]).to eq({ "url" => "http://www.amazon.com/gp/product/123" })
31
+ end
32
+
33
+ it "accepts arrays" do
34
+ create :string => nil, :array => nil
35
+ expect(parse("product", "title", "age")["string"]).to eq("product")
36
+ expect(parse("product", "title", "age")["array"]).to eq(%w(title age))
37
+ end
38
+
39
+ describe "with no inputs" do
40
+ it "and no arguments returns an empty hash" do
41
+ create
42
+ expect(parse).to eq({})
43
+ end
44
+
45
+ it "and required arguments raises an error" do
46
+ create :string => nil, :numeric => nil
47
+ expect{ parse }.to raise_error(Thor::RequiredArgumentMissingError, "No value provided for required arguments 'string', 'numeric'")
48
+ end
49
+
50
+ it "and default arguments returns default values" do
51
+ create :string => "name", :numeric => 13
52
+ expect(parse).to eq({ "string" => "name", "numeric" => 13 })
53
+ end
54
+ end
55
+
56
+ it "returns the input if it's already parsed" do
57
+ create :string => nil, :hash => nil, :array => nil, :numeric => nil
58
+ expect(parse("", 0, {}, [])).to eq({ "string" => "", "numeric" => 0, "hash" => {}, "array" => [] })
59
+ end
60
+
61
+ it "returns the default value if none is provided" do
62
+ create :string => "foo", :numeric => 3.0
63
+ expect(parse("bar")).to eq({ "string" => "bar", "numeric" => 3.0 })
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,202 @@
1
+ require 'helper'
2
+ require 'thor/parser'
3
+
4
+ describe Thor::Option do
5
+ def parse(key, value)
6
+ Thor::Option.parse(key, value)
7
+ end
8
+
9
+ def option(name, options={})
10
+ @option ||= Thor::Option.new(name, options)
11
+ end
12
+
13
+ describe "#parse" do
14
+
15
+ describe "with value as a symbol" do
16
+ describe "and symbol is a valid type" do
17
+ it "has type equals to the symbol" do
18
+ expect(parse(:foo, :string).type).to eq(:string)
19
+ expect(parse(:foo, :numeric).type).to eq(:numeric)
20
+ end
21
+
22
+ it "has no default value" do
23
+ expect(parse(:foo, :string).default).to be_nil
24
+ expect(parse(:foo, :numeric).default).to be_nil
25
+ end
26
+ end
27
+
28
+ describe "equals to :required" do
29
+ it "has type equals to :string" do
30
+ expect(parse(:foo, :required).type).to eq(:string)
31
+ end
32
+
33
+ it "has no default value" do
34
+ expect(parse(:foo, :required).default).to be_nil
35
+ end
36
+ end
37
+
38
+ describe "and symbol is not a reserved key" do
39
+ it "has type equal to :string" do
40
+ expect(parse(:foo, :bar).type).to eq(:string)
41
+ end
42
+
43
+ it "has no default value" do
44
+ expect(parse(:foo, :bar).default).to be_nil
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "with value as hash" do
50
+ it "has default type :hash" do
51
+ expect(parse(:foo, :a => :b).type).to eq(:hash)
52
+ end
53
+
54
+ it "has default value equal to the hash" do
55
+ expect(parse(:foo, :a => :b).default).to eq({ :a => :b })
56
+ end
57
+ end
58
+
59
+ describe "with value as array" do
60
+ it "has default type :array" do
61
+ expect(parse(:foo, [:a, :b]).type).to eq(:array)
62
+ end
63
+
64
+ it "has default value equal to the array" do
65
+ expect(parse(:foo, [:a, :b]).default).to eq([:a, :b])
66
+ end
67
+ end
68
+
69
+ describe "with value as string" do
70
+ it "has default type :string" do
71
+ expect(parse(:foo, "bar").type).to eq(:string)
72
+ end
73
+
74
+ it "has default value equal to the string" do
75
+ expect(parse(:foo, "bar").default).to eq("bar")
76
+ end
77
+ end
78
+
79
+ describe "with value as numeric" do
80
+ it "has default type :numeric" do
81
+ expect(parse(:foo, 2.0).type).to eq(:numeric)
82
+ end
83
+
84
+ it "has default value equal to the numeric" do
85
+ expect(parse(:foo, 2.0).default).to eq(2.0)
86
+ end
87
+ end
88
+
89
+ describe "with value as boolean" do
90
+ it "has default type :boolean" do
91
+ expect(parse(:foo, true).type).to eq(:boolean)
92
+ expect(parse(:foo, false).type).to eq(:boolean)
93
+ end
94
+
95
+ it "has default value equal to the boolean" do
96
+ expect(parse(:foo, true).default).to eq(true)
97
+ expect(parse(:foo, false).default).to eq(false)
98
+ end
99
+ end
100
+
101
+ describe "with key as a symbol" do
102
+ it "sets the name equal to the key" do
103
+ expect(parse(:foo, true).name).to eq("foo")
104
+ end
105
+ end
106
+
107
+ describe "with key as an array" do
108
+ it "sets the first items in the array to the name" do
109
+ expect(parse([:foo, :bar, :baz], true).name).to eq("foo")
110
+ end
111
+
112
+ it "sets all other items as aliases" do
113
+ expect(parse([:foo, :bar, :baz], true).aliases).to eq([:bar, :baz])
114
+ end
115
+ end
116
+ end
117
+
118
+ it "returns the switch name" do
119
+ expect(option("foo").switch_name).to eq("--foo")
120
+ expect(option("--foo").switch_name).to eq("--foo")
121
+ end
122
+
123
+ it "returns the human name" do
124
+ expect(option("foo").human_name).to eq("foo")
125
+ expect(option("--foo").human_name).to eq("foo")
126
+ end
127
+
128
+ it "converts underscores to dashes" do
129
+ expect(option("foo_bar").switch_name).to eq("--foo-bar")
130
+ end
131
+
132
+ it "can be required and have default values" do
133
+ option = option("foo", :required => true, :type => :string, :default => "bar")
134
+ expect(option.default).to eq("bar")
135
+ expect(option).to be_required
136
+ end
137
+
138
+ it "boolean options cannot be required" do
139
+ expect {
140
+ option("foo", :required => true, :type => :boolean)
141
+ }.to raise_error(ArgumentError, "An option cannot be boolean and required.")
142
+ end
143
+
144
+ it "allows type predicates" do
145
+ expect(parse(:foo, :string)).to be_string
146
+ expect(parse(:foo, :boolean)).to be_boolean
147
+ expect(parse(:foo, :numeric)).to be_numeric
148
+ end
149
+
150
+ it "raises an error on method missing" do
151
+ expect {
152
+ parse(:foo, :string).unknown?
153
+ }.to raise_error(NoMethodError)
154
+ end
155
+
156
+ describe "#usage" do
157
+
158
+ it "returns usage for string types" do
159
+ expect(parse(:foo, :string).usage).to eq("[--foo=FOO]")
160
+ end
161
+
162
+ it "returns usage for numeric types" do
163
+ expect(parse(:foo, :numeric).usage).to eq("[--foo=N]")
164
+ end
165
+
166
+ it "returns usage for array types" do
167
+ expect(parse(:foo, :array).usage).to eq("[--foo=one two three]")
168
+ end
169
+
170
+ it "returns usage for hash types" do
171
+ expect(parse(:foo, :hash).usage).to eq("[--foo=key:value]")
172
+ end
173
+
174
+ it "returns usage for boolean types" do
175
+ expect(parse(:foo, :boolean).usage).to eq("[--foo]")
176
+ end
177
+
178
+ it "does not use padding when no aliases are given" do
179
+ expect(parse(:foo, :boolean).usage).to eq("[--foo]")
180
+ end
181
+
182
+ it "uses banner when supplied" do
183
+ expect(option(:foo, :required => false, :type => :string, :banner => "BAR").usage).to eq("[--foo=BAR]")
184
+ end
185
+
186
+ it "checks when banner is an empty string" do
187
+ expect(option(:foo, :required => false, :type => :string, :banner => "").usage).to eq("[--foo]")
188
+ end
189
+
190
+ describe "with required values" do
191
+ it "does not show the usage between brackets" do
192
+ expect(parse(:foo, :required).usage).to eq("--foo=FOO")
193
+ end
194
+ end
195
+
196
+ describe "with aliases" do
197
+ it "does not show the usage between brackets" do
198
+ expect(parse([:foo, "-f", "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
199
+ end
200
+ end
201
+ end
202
+ end