cliutils 2.0.3 → 2.1.0
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.
- checksums.yaml +4 -4
- data/HISTORY.md +7 -1
- data/cliutils.gemspec +4 -4
- data/lib/cliutils/configurator.rb +1 -1
- data/lib/cliutils/constants.rb +8 -1
- data/lib/cliutils/prefs/pref.rb +44 -17
- data/lib/cliutils/prefs/pref_actions/pref_action.rb +2 -9
- data/lib/cliutils/prefs/pref_behaviors/pref_behavior.rb +2 -7
- data/lib/cliutils/prefs/pref_validators/pref_validator.rb +2 -7
- data/lib/cliutils/prefs.rb +125 -4
- data/lib/cliutils/pretty_io.rb +5 -6
- data/test/configurator_test.rb +1 -1
- data/test/messenging_test.rb +1 -1
- data/test/pref_test.rb +73 -1
- data/test/prefs_test.rb +32 -4
- data/test/test_files/prefstest.yaml +16 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2853602f0ebaccd9bcfe4b6b1372d6fced7bf18b
|
4
|
+
data.tar.gz: 794dd1a40731aba8a6e7f1e3a41eb6aa43fc6818
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 156f2abebd0cf0f60b36d978604c08980ceba30f6f071ade567a82bc637d83a38627c9e37df30176d89d0f4f156caf1ea32b62f3b9e7fac17952d4083b59d84c
|
7
|
+
data.tar.gz: a434896e1b247f2c283b47aa05b0698fa2f1dd7dcceb0d7b423e2f4c9329b4c634398a7bd9d419622a44867f3feec934be3c9fa5edcbbdba3f3d8763e73e68f0
|
data/HISTORY.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
# 2.0
|
1
|
+
# 2.1.0 (2014-04-20)
|
2
2
|
|
3
|
+
* Added ability to register/deregister Pref Actions
|
4
|
+
* Added ability to register/deregister Pref Behaviors
|
5
|
+
* Added ability to register/deregister Pref Validators
|
6
|
+
* Added same "Press enter to continue message" to both pre and post Pref behaviors
|
3
7
|
* Removed section_block from Messaging (because it was totally not useful)
|
8
|
+
* Several bugfixes
|
9
|
+
* Added new Prefs and Pref unit tests
|
4
10
|
|
5
11
|
# 2.0.2 (2014-04-16)
|
6
12
|
|
data/cliutils.gemspec
CHANGED
@@ -8,11 +8,11 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = CLIUtils::VERSION
|
9
9
|
spec.authors = ["Aaron Bach"]
|
10
10
|
spec.email = ["bachya1208@googlemail.com"]
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage = "
|
11
|
+
spec.summary = CLIUtils::SUMMARY
|
12
|
+
spec.description = CLIUtils::DESCRIPTION
|
13
|
+
spec.homepage = "https://github.com/bachya/cliutils"
|
14
14
|
spec.license = "MIT"
|
15
|
-
|
15
|
+
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
data/lib/cliutils/constants.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Stores constants to use.
|
2
2
|
module CLIUtils
|
3
|
+
# The description of the gem
|
4
|
+
DESCRIPTION = 'A library of functionality designed to alleviate common ' \
|
5
|
+
'tasks and headaches when developing command-line (CLI) apps in Ruby.'
|
6
|
+
|
7
|
+
# The summary of the gem
|
8
|
+
SUMMARY = 'Sugary goodness for Ruby CLI apps.'
|
9
|
+
|
3
10
|
# The current version of the gem
|
4
|
-
VERSION = '2.0
|
11
|
+
VERSION = '2.1.0'
|
5
12
|
end
|
data/lib/cliutils/prefs/pref.rb
CHANGED
@@ -195,7 +195,7 @@ module CLIUtils
|
|
195
195
|
|
196
196
|
if (@pre[:action])
|
197
197
|
action_obj = _init_action(@pre[:action][:name])
|
198
|
-
action_obj.run
|
198
|
+
action_obj.run if action_obj
|
199
199
|
end
|
200
200
|
end
|
201
201
|
|
@@ -203,10 +203,11 @@ module CLIUtils
|
|
203
203
|
# @return [void]
|
204
204
|
def _eval_post
|
205
205
|
info(@post[:message])
|
206
|
+
prompt('Press enter to continue')
|
206
207
|
|
207
208
|
if (@post[:action])
|
208
209
|
action_obj = _init_action(@post[:action][:name])
|
209
|
-
action_obj.run
|
210
|
+
action_obj.run if action_obj
|
210
211
|
end
|
211
212
|
end
|
212
213
|
|
@@ -217,7 +218,6 @@ module CLIUtils
|
|
217
218
|
def _init_action(path_or_name)
|
218
219
|
obj = _load_asset(ASSET_TYPE_ACTION, path_or_name)
|
219
220
|
unless obj.nil?
|
220
|
-
obj.pref = self
|
221
221
|
obj.parameters = @pre[:action][:parameters]
|
222
222
|
end
|
223
223
|
obj
|
@@ -230,7 +230,6 @@ module CLIUtils
|
|
230
230
|
def _init_and_add_behavior(behavior_hash)
|
231
231
|
obj = _load_asset(ASSET_TYPE_BEHAVIOR, behavior_hash[:name])
|
232
232
|
unless obj.nil?
|
233
|
-
obj.pref = self
|
234
233
|
obj.parameters = behavior_hash[:parameters]
|
235
234
|
@behavior_objects << obj
|
236
235
|
end
|
@@ -243,7 +242,6 @@ module CLIUtils
|
|
243
242
|
def _init_and_add_validator(path_or_name)
|
244
243
|
obj = _load_asset(ASSET_TYPE_VALIDATOR, path_or_name)
|
245
244
|
unless obj.nil?
|
246
|
-
obj.pref = self
|
247
245
|
@validator_objects << obj
|
248
246
|
end
|
249
247
|
end
|
@@ -253,28 +251,57 @@ module CLIUtils
|
|
253
251
|
# execution continues.
|
254
252
|
# @param [Integer] type ASSET_TYPE_BEHAVIOR or ASSET_TYPE_VALIDATOR
|
255
253
|
# @param [String] path_or_name The path to or name of the asset
|
256
|
-
# @return [Object]
|
254
|
+
# @return [Object]
|
257
255
|
def _load_asset(type, path_or_name)
|
258
|
-
|
256
|
+
asset_found = false
|
257
|
+
if File.file?(File.expand_path(path_or_name))
|
259
258
|
# If the file exists, we're assuming that the user
|
260
259
|
# passed a filepath.
|
261
|
-
|
262
|
-
asset_path =
|
260
|
+
asset_found = true
|
261
|
+
asset_path = File.expand_path(path_or_name) if path_or_name.start_with?('~')
|
263
262
|
asset_name = File.basename(path_or_name, '.*').camelize
|
264
|
-
|
265
|
-
|
266
|
-
|
263
|
+
end
|
264
|
+
|
265
|
+
unless asset_found
|
266
|
+
# If the file doesn't exist, look to see if it's been
|
267
|
+
# pre-registered.
|
268
|
+
symbol = File.basename(path_or_name, '.*').camelize.to_sym
|
269
|
+
case type
|
270
|
+
when Pref::ASSET_TYPE_ACTION
|
271
|
+
if CLIUtils::Prefs.registered_actions.key?(symbol)
|
272
|
+
asset_found = true
|
273
|
+
asset_path = CLIUtils::Prefs.registered_actions[symbol][:path]
|
274
|
+
asset_name = CLIUtils::Prefs.registered_actions[symbol][:class]
|
275
|
+
end
|
276
|
+
when Pref::ASSET_TYPE_BEHAVIOR
|
277
|
+
if CLIUtils::Prefs.registered_behaviors.key?(symbol)
|
278
|
+
asset_found = true
|
279
|
+
asset_path = CLIUtils::Prefs.registered_behaviors[symbol][:path] rescue ''
|
280
|
+
asset_name = CLIUtils::Prefs.registered_behaviors[symbol][:class] rescue ''
|
281
|
+
end
|
282
|
+
when Pref::ASSET_TYPE_VALIDATOR
|
283
|
+
if CLIUtils::Prefs.registered_validators.key?(symbol)
|
284
|
+
asset_found = true
|
285
|
+
asset_path = CLIUtils::Prefs.registered_validators[symbol][:path] rescue ''
|
286
|
+
asset_name = CLIUtils::Prefs.registered_validators[symbol][:class] rescue ''
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
unless asset_found
|
292
|
+
# If the file doesn't exist and there's no pre-registered
|
293
|
+
# asset, as a last check, look for it as a built-in asset.
|
267
294
|
_default = File.join(File.dirname(__FILE__), "pref_#{ @@asset_labels[type][:file_suffix] }s")
|
268
295
|
asset_path = File.join(_default, "#{ path_or_name }_#{ @@asset_labels[type][:file_suffix] }")
|
269
|
-
asset_name = path_or_name.camelize
|
296
|
+
asset_name = "#{ path_or_name.camelize }#{ @@asset_labels[type][:class_suffix] }"
|
270
297
|
end
|
271
298
|
|
272
|
-
# Try to load and instantiate the
|
299
|
+
# Try to load and instantiate the asset. If that fails, warn
|
273
300
|
# the user with a message and skip over it.
|
274
301
|
begin
|
275
|
-
require asset_path
|
276
|
-
Object.const_get("CLIUtils::#{ asset_name }
|
277
|
-
rescue LoadError
|
302
|
+
require File.expand_path(asset_path)
|
303
|
+
Object.const_get("CLIUtils::#{ asset_name }").new
|
304
|
+
rescue LoadError => e
|
278
305
|
messenger.warn("Skipping undefined Pref #{ @@asset_labels[type][:class_suffix] }: #{ path_or_name }")
|
279
306
|
nil
|
280
307
|
end
|
@@ -6,22 +6,15 @@ module CLIUtils
|
|
6
6
|
|
7
7
|
# Holds the parameters that apply to
|
8
8
|
# this Action.
|
9
|
-
# @!attribute parameters
|
10
9
|
# @return [Hash]
|
11
10
|
attr_accessor :parameters
|
12
11
|
|
13
|
-
# Holds a reference to the pref that
|
14
|
-
# is implementing this Action.
|
15
|
-
# @!attribute pref
|
16
|
-
# @return [Pref]
|
17
|
-
attr_accessor :pref
|
18
|
-
|
19
12
|
# Runs the Action. Note that the
|
20
13
|
# method implemented here will throw
|
21
14
|
# an exception by default, meaning that
|
22
15
|
# the user's subclass *needs* to
|
23
16
|
# implement it.
|
24
|
-
# @
|
17
|
+
# @param [Hash] parameters
|
25
18
|
# @raise [StandardError] if the subclass
|
26
19
|
# doesn't implement this method.
|
27
20
|
# @return [void]
|
@@ -29,4 +22,4 @@ module CLIUtils
|
|
29
22
|
fail "`run` method not implemented on caller: #{ self.class }"
|
30
23
|
end
|
31
24
|
end
|
32
|
-
end
|
25
|
+
end
|
@@ -9,13 +9,8 @@ module CLIUtils
|
|
9
9
|
# @return [Hash]
|
10
10
|
attr_accessor :parameters
|
11
11
|
|
12
|
-
# Holds a reference to the Pref that
|
13
|
-
# is applying this Behavior.
|
14
|
-
# @return [Pref]
|
15
|
-
attr_accessor :pref
|
16
|
-
|
17
12
|
# Evaluate the Behavior!
|
18
|
-
# @
|
13
|
+
# @param [String] text
|
19
14
|
# @raise [StandardError] if the subclass
|
20
15
|
# doesn't implement this method.
|
21
16
|
# @return [void]
|
@@ -23,4 +18,4 @@ module CLIUtils
|
|
23
18
|
fail "`evaluate` method not implemented on caller: #{ self.class }"
|
24
19
|
end
|
25
20
|
end
|
26
|
-
end
|
21
|
+
end
|
@@ -14,13 +14,8 @@ module CLIUtils
|
|
14
14
|
# @return [String]
|
15
15
|
attr_accessor :message
|
16
16
|
|
17
|
-
# Holds a reference to the Pref that is
|
18
|
-
# applying this Validator.
|
19
|
-
# @return [Pref]
|
20
|
-
attr_accessor :pref
|
21
|
-
|
22
17
|
# Validate the Validator!
|
23
|
-
# @
|
18
|
+
# @param [String] text
|
24
19
|
# @raise [StandardError] if the subclass
|
25
20
|
# doesn't implement this method.
|
26
21
|
# @return [void]
|
@@ -28,4 +23,4 @@ module CLIUtils
|
|
28
23
|
fail "`validate` method not implemented on caller: #{ self.class }"
|
29
24
|
end
|
30
25
|
end
|
31
|
-
end
|
26
|
+
end
|
data/lib/cliutils/prefs.rb
CHANGED
@@ -6,6 +6,7 @@ module CLIUtils
|
|
6
6
|
# those to a user via a prompt, and collect the results.
|
7
7
|
class Prefs
|
8
8
|
include Messaging
|
9
|
+
|
9
10
|
# Stores the filepath (if it exists) to the prefs file.
|
10
11
|
# @return [String]
|
11
12
|
attr_reader :config_path
|
@@ -18,18 +19,37 @@ module CLIUtils
|
|
18
19
|
# @return [Array]
|
19
20
|
attr_reader :prompts
|
20
21
|
|
22
|
+
class << self
|
23
|
+
# Stores any actions registered for all
|
24
|
+
# instances of Prefs.
|
25
|
+
# @return [Hash]
|
26
|
+
attr_accessor :registered_actions
|
27
|
+
|
28
|
+
# Stores any behaviors registered for all
|
29
|
+
# instances of Prefs.
|
30
|
+
# @return [Hash]
|
31
|
+
attr_accessor :registered_behaviors
|
32
|
+
|
33
|
+
# Stores any validators registered for all
|
34
|
+
# instances of Prefs.
|
35
|
+
# @return [Hash]
|
36
|
+
attr_accessor :registered_validators
|
37
|
+
end
|
38
|
+
|
39
|
+
self.registered_actions = {}
|
40
|
+
self.registered_behaviors = {}
|
41
|
+
self.registered_validators = {}
|
42
|
+
|
21
43
|
# Reads prompt data from and stores it.
|
22
44
|
# @param [<String, Hash, Array>] data Filepath to YAML, Hash, or Array
|
23
45
|
# @param [Configurator] configurator Source of defailt values
|
24
46
|
# @return [void]
|
25
47
|
def initialize(data, configurator = nil)
|
26
|
-
@answers = []
|
27
|
-
@configurator = configurator
|
28
48
|
@prompts = []
|
29
|
-
|
49
|
+
@configurator = configurator
|
30
50
|
case data
|
31
51
|
when String
|
32
|
-
if File.
|
52
|
+
if File.file?(data)
|
33
53
|
@config_path = data
|
34
54
|
data = YAML.load_file(data).deep_symbolize_keys
|
35
55
|
@prompts = _generate_prefs(data)
|
@@ -56,15 +76,64 @@ module CLIUtils
|
|
56
76
|
# complete, questions w/ prerequisites are examined.
|
57
77
|
# @return [void]
|
58
78
|
def ask
|
79
|
+
# First, deliver all the prompts that don't have
|
80
|
+
# any prerequisites.
|
59
81
|
@prompts.reject { |p| p.prereqs }.each do |p|
|
60
82
|
_deliver_prompt(p)
|
61
83
|
end
|
62
84
|
|
85
|
+
# After the "non-prerequisite" prompts are delivered,
|
86
|
+
# deliver any that require prerequisites.
|
63
87
|
@prompts.select { |p| p.prereqs }.each do |p|
|
64
88
|
_deliver_prompt(p) if _prereqs_fulfilled?(p)
|
65
89
|
end
|
66
90
|
end
|
67
91
|
|
92
|
+
# Deregister an action based on the symbol it was
|
93
|
+
# stored under.
|
94
|
+
# @param [Symbol] symbol The symbol to remove
|
95
|
+
# @remove [void]
|
96
|
+
def self.deregister_action(symbol)
|
97
|
+
_deregister_asset(symbol, Pref::ASSET_TYPE_ACTION)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Deregister a behavior based on the symbol it was
|
101
|
+
# stored under.
|
102
|
+
# @param [Symbol] symbol The symbol to remove
|
103
|
+
# @remove [void]
|
104
|
+
def self.deregister_behavior(symbol)
|
105
|
+
_deregister_asset(symbol, Pref::ASSET_TYPE_BEHAVIOR)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Deregister a validator based on the symbol it was
|
109
|
+
# stored under.
|
110
|
+
# @param [Symbol] symbol The symbol to remove
|
111
|
+
# @remove [void]
|
112
|
+
def self.deregister_validator(symbol)
|
113
|
+
_deregister_asset(symbol, Pref::ASSET_TYPE_VALIDATOR)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Register an action.
|
117
|
+
# @param [String] path The filepath of the action
|
118
|
+
# @remove [void]
|
119
|
+
def self.register_action(path)
|
120
|
+
self._register_asset(path, Pref::ASSET_TYPE_ACTION)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Register a behavior.
|
124
|
+
# @param [String] path The filepath of the behavior
|
125
|
+
# @remove [void]
|
126
|
+
def self.register_behavior(path)
|
127
|
+
_register_asset(path, Pref::ASSET_TYPE_BEHAVIOR)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Register a validator.
|
131
|
+
# @param [String] path The filepath of the validator
|
132
|
+
# @remove [void]
|
133
|
+
def self.register_validator(path)
|
134
|
+
_register_asset(path, Pref::ASSET_TYPE_VALIDATOR)
|
135
|
+
end
|
136
|
+
|
68
137
|
private
|
69
138
|
|
70
139
|
# Utility method for prompting the user to answer the
|
@@ -75,6 +144,9 @@ module CLIUtils
|
|
75
144
|
default = p.default
|
76
145
|
unless @configurator.nil?
|
77
146
|
section_sym = p.config_section.to_sym
|
147
|
+
|
148
|
+
# If a Configurator has been included, replace relevant
|
149
|
+
# prompt defaults with those values from the Configurator.
|
78
150
|
unless @configurator.data[section_sym].nil?
|
79
151
|
config_val = @configurator.data[section_sym][p.config_key.to_sym]
|
80
152
|
default = config_val unless config_val.nil?
|
@@ -107,5 +179,54 @@ module CLIUtils
|
|
107
179
|
end
|
108
180
|
ret
|
109
181
|
end
|
182
|
+
|
183
|
+
# Utility function to deregister an asset.
|
184
|
+
# @param [Symbol] symbol The asset to remove
|
185
|
+
# @param [Fixnum] type A Pref asset type
|
186
|
+
# @return [void]
|
187
|
+
def self._deregister_asset(symbol, type)
|
188
|
+
case type
|
189
|
+
when Pref::ASSET_TYPE_ACTION
|
190
|
+
CLIUtils::Prefs.registered_actions.delete(symbol)
|
191
|
+
when Pref::ASSET_TYPE_BEHAVIOR
|
192
|
+
CLIUtils::Prefs.registered_behaviors.delete(symbol)
|
193
|
+
when Pref::ASSET_TYPE_VALIDATOR
|
194
|
+
CLIUtils::Prefs.registered_validators.delete(symbol)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Utility function to register an asset and associate
|
199
|
+
# it with the Prefs eigenclass.
|
200
|
+
# @param [String] path The filepath to the asset
|
201
|
+
# @param [Fixnum] type A Pref asset type
|
202
|
+
# @raise [StandardError] if the asset is unknown
|
203
|
+
# @return [void]
|
204
|
+
def self._register_asset(path, type)
|
205
|
+
if File.file?(path)
|
206
|
+
class_name = File.basename(path, '.*').camelize
|
207
|
+
case type
|
208
|
+
when Pref::ASSET_TYPE_ACTION
|
209
|
+
k = class_name.sub('Action', '').to_sym
|
210
|
+
unless CLIUtils::Prefs.registered_actions.key?(k)
|
211
|
+
h = { k => { class: class_name, path: path } }
|
212
|
+
CLIUtils::Prefs.registered_actions.merge!(h)
|
213
|
+
end
|
214
|
+
when Pref::ASSET_TYPE_BEHAVIOR
|
215
|
+
k = class_name.sub('Behavior', '').to_sym
|
216
|
+
unless CLIUtils::Prefs.registered_behaviors.key?(k)
|
217
|
+
h = { k => { class: class_name, path: path } }
|
218
|
+
CLIUtils::Prefs.registered_behaviors.merge!(h)
|
219
|
+
end
|
220
|
+
when Pref::ASSET_TYPE_VALIDATOR
|
221
|
+
k = class_name.sub('Validator', '').to_sym
|
222
|
+
unless CLIUtils::Prefs.registered_validators.key?(k)
|
223
|
+
h = { k => { class: class_name, path: path } }
|
224
|
+
CLIUtils::Prefs.registered_validators.merge!(h)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
else
|
228
|
+
fail "Registration failed because of unknown filepath: #{ path }"
|
229
|
+
end
|
230
|
+
end
|
110
231
|
end
|
111
232
|
end
|
data/lib/cliutils/pretty_io.rb
CHANGED
@@ -57,14 +57,13 @@ module CLIUtils
|
|
57
57
|
# @return [String]
|
58
58
|
def _word_wrap(text, prefix_str)
|
59
59
|
if PrettyIO.wrap
|
60
|
-
return text if PrettyIO.wrap_char_limit <= 0
|
61
|
-
|
60
|
+
return prefix_str + text if PrettyIO.wrap_char_limit <= 0
|
62
61
|
limit = PrettyIO.wrap_char_limit - prefix_str.length
|
63
|
-
text.
|
64
|
-
|
65
|
-
|
62
|
+
text.split("\n").collect! do |line|
|
63
|
+
line.length > limit ? text.gsub(/(.{1,#{ limit }})(\s+|$)/, "#{ prefix_str }\\1\n").strip : prefix_str + line
|
64
|
+
end * "\n"
|
66
65
|
else
|
67
|
-
text
|
66
|
+
prefix_str + text
|
68
67
|
end
|
69
68
|
end
|
70
69
|
end
|
data/test/configurator_test.rb
CHANGED
data/test/messenging_test.rb
CHANGED
data/test/pref_test.rb
CHANGED
@@ -3,8 +3,80 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/hash_extensions')
|
5
5
|
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs/pref')
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs/pref_actions/pref_action')
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs/pref_behaviors/pref_behavior')
|
8
|
+
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs/pref_validators/pref_validator')
|
6
9
|
|
7
10
|
# Tests for the Prefs class
|
8
11
|
class TestPref < Test::Unit::TestCase
|
9
|
-
|
12
|
+
def setup
|
13
|
+
@prefs_hash = {
|
14
|
+
behaviors: [{ name: 'test' }],
|
15
|
+
config_key: 'test_prompt',
|
16
|
+
config_section: 'app_data',
|
17
|
+
default: 'bachya',
|
18
|
+
options: ['a', 'b'],
|
19
|
+
pre: {
|
20
|
+
message: 'Test pre message',
|
21
|
+
action: 'test'
|
22
|
+
},
|
23
|
+
post: {
|
24
|
+
message: 'Test post message',
|
25
|
+
action: 'test'
|
26
|
+
},
|
27
|
+
prereqs: [
|
28
|
+
{ config_section: 'section' },
|
29
|
+
{ config_value: 'value' }
|
30
|
+
],
|
31
|
+
prompt_text: 'Test',
|
32
|
+
validators: ['test'],
|
33
|
+
}
|
34
|
+
|
35
|
+
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
36
|
+
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
37
|
+
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
38
|
+
end
|
39
|
+
|
40
|
+
def teardown
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_initialization
|
45
|
+
pref = CLIUtils::Pref.new(@prefs_hash)
|
46
|
+
|
47
|
+
assert_equal(pref.answer, nil)
|
48
|
+
assert_equal(pref.behavior_objects[0].class, CLIUtils::TestBehavior.new.class)
|
49
|
+
assert_equal(pref.behaviors, [{ name: 'test' }])
|
50
|
+
assert_equal(pref.config_key, @prefs_hash[:config_key])
|
51
|
+
assert_equal(pref.config_section, @prefs_hash[:config_section])
|
52
|
+
assert_equal(pref.default, @prefs_hash[:default])
|
53
|
+
assert_equal(pref.last_error_message, nil)
|
54
|
+
assert_equal(pref.options, ['a', 'b'])
|
55
|
+
assert_equal(pref.post, { message: 'Test post message', action: 'test' })
|
56
|
+
assert_equal(pref.pre, { message: 'Test pre message', action: 'test' })
|
57
|
+
assert_equal(pref.prereqs, [{ config_section: 'section' }, { config_value: 'value' }])
|
58
|
+
assert_equal(pref.validator_objects[0].class, CLIUtils::TestValidator.new.class)
|
59
|
+
assert_equal(pref.validators, ['test'])
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_action
|
63
|
+
require File.join(File.dirname(__FILE__), 'test_files/test_action.rb')
|
64
|
+
a = CLIUtils::TestAction.new
|
65
|
+
assert_output("here\n") { a.run }
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_behavior
|
69
|
+
require File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb')
|
70
|
+
b = CLIUtils::TestBehavior.new
|
71
|
+
assert_equal(b.evaluate('test'), 'test_behavior: test')
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_validator
|
75
|
+
require File.join(File.dirname(__FILE__), 'test_files/test_validator.rb')
|
76
|
+
v = CLIUtils::TestValidator.new
|
77
|
+
v.validate('bachya')
|
78
|
+
|
79
|
+
assert_equal(v.is_valid, true)
|
80
|
+
assert_equal(v.message, "String did not equal 'bachya': bachya")
|
81
|
+
end
|
10
82
|
end
|
data/test/prefs_test.rb
CHANGED
@@ -47,27 +47,55 @@ class TestPrefs < Test::Unit::TestCase
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def teardown
|
50
|
-
FileUtils.rm(@prefs_filepath) if File.
|
50
|
+
FileUtils.rm(@prefs_filepath) if File.file?(@prefs_filepath)
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_file_creation
|
54
54
|
p = CLIUtils::Prefs.new(@prefs_filepath)
|
55
55
|
prefs = YAML::load_file(@prefs_filepath).deep_symbolize_keys
|
56
|
-
|
57
56
|
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
58
57
|
end
|
59
58
|
|
60
59
|
def test_array_creation
|
61
60
|
p = CLIUtils::Prefs.new(@prefs_arr)
|
62
61
|
prefs = @prefs_hash.deep_symbolize_keys
|
63
|
-
|
64
62
|
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
65
63
|
end
|
66
64
|
|
67
65
|
def test_hash_creation
|
68
66
|
p = CLIUtils::Prefs.new(@prefs_hash)
|
69
67
|
prefs = @prefs_hash.deep_symbolize_keys
|
70
|
-
|
71
68
|
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
72
69
|
end
|
70
|
+
|
71
|
+
def test_register
|
72
|
+
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
73
|
+
assert_equal(CLIUtils::Prefs.registered_actions.key?(:Test), true)
|
74
|
+
assert_equal(CLIUtils::Prefs.registered_actions[:Test][:class], 'TestAction')
|
75
|
+
assert_equal(CLIUtils::Prefs.registered_actions[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
76
|
+
|
77
|
+
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
78
|
+
assert_equal(CLIUtils::Prefs.registered_behaviors.key?(:Test), true)
|
79
|
+
assert_equal(CLIUtils::Prefs.registered_behaviors[:Test][:class], 'TestBehavior')
|
80
|
+
assert_equal(CLIUtils::Prefs.registered_behaviors[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
81
|
+
|
82
|
+
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
83
|
+
assert_equal(CLIUtils::Prefs.registered_validators.key?(:Test), true)
|
84
|
+
assert_equal(CLIUtils::Prefs.registered_validators[:Test][:class], 'TestValidator')
|
85
|
+
assert_equal(CLIUtils::Prefs.registered_validators[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_deregister
|
89
|
+
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
90
|
+
CLIUtils::Prefs.deregister_action(:Test)
|
91
|
+
assert_equal(CLIUtils::Prefs.registered_actions.key?(:Test), false)
|
92
|
+
|
93
|
+
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
94
|
+
CLIUtils::Prefs.deregister_behavior(:Test)
|
95
|
+
assert_equal(CLIUtils::Prefs.registered_behaviors.key?(:Test), false)
|
96
|
+
|
97
|
+
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
98
|
+
CLIUtils::Prefs.deregister_validator(:Test)
|
99
|
+
assert_equal(CLIUtils::Prefs.registered_validators.key?(:Test), false)
|
100
|
+
end
|
73
101
|
end
|
@@ -1,17 +1,21 @@
|
|
1
1
|
prompts:
|
2
|
-
- prompt_text:
|
3
|
-
config_key:
|
2
|
+
- prompt_text: This is a test prompt
|
3
|
+
config_key: test_prompt
|
4
4
|
config_section: app_data
|
5
|
-
pre:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
# pre:
|
6
|
+
# message: I will now test a custom action via filepath
|
7
|
+
# action:
|
8
|
+
# name: test
|
9
|
+
# pre:
|
10
|
+
# message: 'I will now open espn.com in your default browser.'
|
11
|
+
# action:
|
12
|
+
# name: test
|
13
|
+
# parameters:
|
14
|
+
# url: http://www.espn.com
|
15
|
+
# post:
|
16
|
+
# message: 'Thanks for inputting!'
|
13
17
|
validators:
|
14
|
-
|
18
|
+
- alphabetic
|
15
19
|
# - alphanumeric
|
16
20
|
# - date
|
17
21
|
# - datetime
|
@@ -21,7 +25,7 @@ prompts:
|
|
21
25
|
# - time
|
22
26
|
# - url
|
23
27
|
behaviors:
|
24
|
-
|
28
|
+
- name: capitalize
|
25
29
|
# - name: expand_filepath
|
26
30
|
# - name: lowercase
|
27
31
|
# - name: prefix
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Bach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -139,7 +139,7 @@ files:
|
|
139
139
|
- test/string_extesions_test.rb
|
140
140
|
- test/test_files/prefstest.yaml
|
141
141
|
- test/test_helper.rb
|
142
|
-
homepage:
|
142
|
+
homepage: https://github.com/bachya/cliutils
|
143
143
|
licenses:
|
144
144
|
- MIT
|
145
145
|
metadata: {}
|