cliutils 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/HISTORY.md +5 -0
- data/README.md +35 -16
- data/Rakefile +1 -8
- data/lib/cliutils/configuration.rb +3 -7
- data/lib/cliutils/constants.rb +1 -1
- data/lib/cliutils/messaging.rb +7 -5
- data/lib/cliutils/messenger.rb +11 -2
- data/lib/cliutils/prefs.rb +6 -6
- data/spec/configuration_spec.rb +18 -11
- data/spec/messaging_spec.rb +6 -0
- data/{res → support}/readme-images/actions-1.png +0 -0
- data/{res → support}/readme-images/actions-2.png +0 -0
- data/{res → support}/readme-images/actions-3.png +0 -0
- data/{res → support}/readme-images/messenger-types-1.png +0 -0
- data/{res → support}/readme-images/messenger-warn.png +0 -0
- data/{res → support}/readme-images/multi-logger.png +0 -0
- data/{res → support}/readme-images/prefs-ask-behaviors.png +0 -0
- data/{res → support}/readme-images/prefs-ask-options.png +0 -0
- data/{res → support}/readme-images/prefs-ask-prereqs.png +0 -0
- data/{res → support}/readme-images/prefs-ask-validators.png +0 -0
- data/{res → support}/readme-images/prefs-ask.png +0 -0
- data/{res → support}/readme-images/prettyio-color-chart.png +0 -0
- data/{res → support}/readme-images/prettyio-gnarly-text.png +0 -0
- data/{res → support}/readme-images/prettyio-red-text.png +0 -0
- data/{res → support}/readme-images/prompting.png +0 -0
- data/{res → support}/readme-images/wrapping.png +0 -0
- metadata +17 -23
- data/test/pref_test.rb +0 -81
- data/test/prefs_test.rb +0 -164
- data/test/test_helper.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b754284592bdb559d6b8ed5715d7d23844e77198
|
4
|
+
data.tar.gz: 71596185e2543a8c8896926d05f444fe041c5510
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9171c3888852dec708db91025b5be0804328945a624964540985f6234f197e73ed1bec1df929e28dcb0c68ed1deab6fd7bd9f11a14fd0bba10cfb2694de73c7
|
7
|
+
data.tar.gz: bdbc9330596c23680f26081b9329f86df278bbdbedf206805a46d298b40497e9ed1d531b940b04c05e4464339689fea9a359b08160cecf97e644dc701c0b2d72
|
data/.travis.yml
CHANGED
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -4,11 +4,13 @@ CLIUtils
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/cliutils.svg)](http://badge.fury.io/rb/cliutils)
|
5
5
|
[![Coverage Status](http://img.shields.io/coveralls/bachya/cliutils/master.svg)](https://coveralls.io/r/bachya/cliutils?branch=master)
|
6
6
|
|
7
|
-
CLIUtils is a library of functionality designed to alleviate common tasks and
|
7
|
+
CLIUtils is a library of functionality designed to alleviate common tasks and
|
8
|
+
headaches when developing command-line (CLI) apps in Ruby.
|
8
9
|
|
9
10
|
# *Updating from 1.x.x to 2.0.0?*
|
10
11
|
|
11
|
-
The big difference is schema changes for Validators, Behaviors, and Pre-Post
|
12
|
+
The big difference is schema changes for Validators, Behaviors, and Pre-Post
|
13
|
+
Actions. Make sure you read the [wiki](https://github.com/bachya/cliutils/wiki "CLIUtils Wiki").
|
12
14
|
|
13
15
|
# Why?
|
14
16
|
|
@@ -22,7 +24,7 @@ It's fairly simple:
|
|
22
24
|
|
23
25
|
CLIUtils offers:
|
24
26
|
|
25
|
-
* **PrettyIO:**
|
27
|
+
* **PrettyIO:** some basic CLI sugar
|
26
28
|
* **Messaging:** a system to display nicely-formatted messages to one or more tagets
|
27
29
|
* **Configuration:** a app configuration manager
|
28
30
|
* **Prefs:** a preferences prompter and manager
|
@@ -40,7 +42,7 @@ CLIUtils is certified against the following:
|
|
40
42
|
* Ruby 2.1.0
|
41
43
|
* Ruby 2.0.0
|
42
44
|
* Ruby 1.9.3
|
43
|
-
*
|
45
|
+
* rbx
|
44
46
|
|
45
47
|
# Installation
|
46
48
|
|
@@ -76,17 +78,23 @@ include CLIUtils
|
|
76
78
|
|
77
79
|
# Can I use it with...?
|
78
80
|
|
79
|
-
I often get asked how nicely CLIUtils plays with other Ruby-based CLI
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
I often get asked how nicely CLIUtils plays with other Ruby-based CLI
|
82
|
+
libraries (like Dave Copeland's excellent
|
83
|
+
[Methadone](https://github.com/davetron5000/methadone "Methadone") and
|
84
|
+
[GLI](https://github.com/davetron5000/gli "GLI")). Answer: they play very
|
85
|
+
nicely. I use CLIUtils in
|
86
|
+
[Sifttter Redux](https://github.com/bachya/Sifttter-Redux "Sifttter Redux")
|
87
|
+
(which is built on GLI) and
|
88
|
+
[ExpandSync](https://github.com/bachya/ExpandSync "ExpandSync") (which is built
|
89
|
+
on Methadone).
|
84
90
|
|
85
91
|
# Bugs and Feature Requests
|
86
92
|
|
87
|
-
To view my current roadmap and objectives, check out the
|
93
|
+
To view my current roadmap and objectives, check out the
|
94
|
+
[Trello board](https://trello.com/b/qXs7Yeir/cliutils "CLIUtils on Trello").
|
88
95
|
|
89
|
-
To report bugs with or suggest features/changes for CLIUtils, please use
|
96
|
+
To report bugs with or suggest features/changes for CLIUtils, please use
|
97
|
+
the [Issues Page](http://github.com/bachya/cliutils/issues).
|
90
98
|
|
91
99
|
# Contributing
|
92
100
|
|
@@ -105,8 +113,19 @@ Contributions are welcome and encouraged. To contribute:
|
|
105
113
|
|
106
114
|
Copyright © 2014 Aaron Bach <bachya1208@gmail.com>
|
107
115
|
|
108
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
116
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
117
|
+
this software and associated documentation files (the 'Software'), to deal in
|
118
|
+
the Software without restriction, including without limitation the rights to
|
119
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
120
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
121
|
+
subject to the following conditions:
|
122
|
+
|
123
|
+
The above copyright notice and this permission notice shall be included in all
|
124
|
+
copies or substantial portions of the Software.
|
125
|
+
|
126
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
127
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
128
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
129
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
130
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
131
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -13,13 +13,6 @@ desc 'Create YARD documentation'
|
|
13
13
|
YARD::Rake::YardocTask.new do |t|
|
14
14
|
end
|
15
15
|
|
16
|
-
require 'rake/testtask'
|
17
|
-
desc 'Run unit tests'
|
18
|
-
Rake::TestTask.new do |t|
|
19
|
-
t.libs << "test"
|
20
|
-
t.test_files = FileList['test/*_test.rb', 'test/**/*_test.rb']
|
21
|
-
end
|
22
|
-
|
23
16
|
require 'rspec/core/rake_task'
|
24
17
|
desc "Run the specs."
|
25
18
|
RSpec::Core::RakeTask.new do |t|
|
@@ -48,4 +41,4 @@ task :build do
|
|
48
41
|
FileUtils.mv("./cliutils-#{version}.gem", "pkg")
|
49
42
|
end
|
50
43
|
|
51
|
-
task :default => [:spec
|
44
|
+
task :default => [:spec, :yard]
|
@@ -5,6 +5,8 @@ module CLIUtils
|
|
5
5
|
# Manages any configuration values and the flat YAML file
|
6
6
|
# into which they get stored.
|
7
7
|
module Configuration
|
8
|
+
extend self
|
9
|
+
|
8
10
|
# Allows easy access to Logger levels.
|
9
11
|
LOG_LEVELS = {
|
10
12
|
'DEBUG' => Logger::DEBUG,
|
@@ -16,13 +18,6 @@ module CLIUtils
|
|
16
18
|
|
17
19
|
@@configuration = nil
|
18
20
|
|
19
|
-
# Hook that triggers when this module is included.
|
20
|
-
# @param [Object] k The includer object
|
21
|
-
# @return [void]
|
22
|
-
def self.included(k)
|
23
|
-
k.extend(self)
|
24
|
-
end
|
25
|
-
|
26
21
|
# Singleton method to return (or initialize, if needed)
|
27
22
|
# a Configurator.
|
28
23
|
# @return [Configurator]
|
@@ -42,5 +37,6 @@ module CLIUtils
|
|
42
37
|
def load_configuration(path)
|
43
38
|
@@configuration = Configurator.new(path)
|
44
39
|
end
|
40
|
+
alias_method :filepath=, :load_configuration
|
45
41
|
end
|
46
42
|
end
|
data/lib/cliutils/constants.rb
CHANGED
data/lib/cliutils/messaging.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require 'cliutils/messenger'
|
2
2
|
|
3
3
|
module CLIUtils
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# Allows access to a single, unified instances of
|
5
|
+
# CLUtils::Messenger.
|
6
6
|
module Messaging
|
7
|
+
extend self
|
8
|
+
|
7
9
|
# Singleton method to return (or initialize, if needed)
|
8
|
-
# a
|
9
|
-
# @return [
|
10
|
+
# a CLIUtils::Messenger.
|
11
|
+
# @return [Messenger]
|
10
12
|
def messenger
|
11
|
-
|
13
|
+
@messenger ||= CLIUtils::Messenger.new
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
data/lib/cliutils/messenger.rb
CHANGED
@@ -2,6 +2,8 @@ require 'cliutils/pretty_io'
|
|
2
2
|
require 'readline'
|
3
3
|
|
4
4
|
module CLIUtils
|
5
|
+
# Allows for messages to be sent to STDOUT, as well
|
6
|
+
# as any number of Logger instances.
|
5
7
|
class Messenger
|
6
8
|
include PrettyIO
|
7
9
|
|
@@ -17,8 +19,8 @@ module CLIUtils
|
|
17
19
|
@targets.merge!(target)
|
18
20
|
end
|
19
21
|
|
20
|
-
#
|
21
|
-
#
|
22
|
+
# Outputs a debug message; since this shouldn't appear in STDOUT,
|
23
|
+
# only send it to the Logger targets.
|
22
24
|
# @return [void]
|
23
25
|
def debug(m)
|
24
26
|
@targets.each { |_, t| t.debug(m) }
|
@@ -50,10 +52,17 @@ module CLIUtils
|
|
50
52
|
@targets.each { |_, t| t.info(m) }
|
51
53
|
end
|
52
54
|
|
55
|
+
# Deprecated method to show info messages around
|
56
|
+
# a block of actions
|
57
|
+
# @param [Array] params
|
58
|
+
# @return [void]
|
53
59
|
def info_block(*params)
|
54
60
|
warn('As of 2.2.0, `info_block` is deprecated and nonfunctioning.')
|
55
61
|
end
|
56
62
|
|
63
|
+
# Initializes a new Messenger with an optional
|
64
|
+
# Hash of targets.
|
65
|
+
# @param [Hash] targets Logger targets
|
57
66
|
def initialize(targets = {})
|
58
67
|
@targets = targets
|
59
68
|
end
|
data/lib/cliutils/prefs.rb
CHANGED
@@ -92,7 +92,7 @@ module CLIUtils
|
|
92
92
|
# Deregister an action based on the symbol it was
|
93
93
|
# stored under.
|
94
94
|
# @param [Symbol] symbol The symbol to remove
|
95
|
-
# @
|
95
|
+
# @return [void]
|
96
96
|
def self.deregister_action(symbol)
|
97
97
|
_deregister_asset(symbol, Pref::ASSET_TYPE_ACTION)
|
98
98
|
end
|
@@ -100,7 +100,7 @@ module CLIUtils
|
|
100
100
|
# Deregister a behavior based on the symbol it was
|
101
101
|
# stored under.
|
102
102
|
# @param [Symbol] symbol The symbol to remove
|
103
|
-
# @
|
103
|
+
# @return [void]
|
104
104
|
def self.deregister_behavior(symbol)
|
105
105
|
_deregister_asset(symbol, Pref::ASSET_TYPE_BEHAVIOR)
|
106
106
|
end
|
@@ -108,28 +108,28 @@ module CLIUtils
|
|
108
108
|
# Deregister a validator based on the symbol it was
|
109
109
|
# stored under.
|
110
110
|
# @param [Symbol] symbol The symbol to remove
|
111
|
-
# @
|
111
|
+
# @return [void]
|
112
112
|
def self.deregister_validator(symbol)
|
113
113
|
_deregister_asset(symbol, Pref::ASSET_TYPE_VALIDATOR)
|
114
114
|
end
|
115
115
|
|
116
116
|
# Register an action.
|
117
117
|
# @param [String] path The filepath of the action
|
118
|
-
# @
|
118
|
+
# @return [void]
|
119
119
|
def self.register_action(path)
|
120
120
|
self._register_asset(path, Pref::ASSET_TYPE_ACTION)
|
121
121
|
end
|
122
122
|
|
123
123
|
# Register a behavior.
|
124
124
|
# @param [String] path The filepath of the behavior
|
125
|
-
# @
|
125
|
+
# @return [void]
|
126
126
|
def self.register_behavior(path)
|
127
127
|
_register_asset(path, Pref::ASSET_TYPE_BEHAVIOR)
|
128
128
|
end
|
129
129
|
|
130
130
|
# Register a validator.
|
131
131
|
# @param [String] path The filepath of the validator
|
132
|
-
# @
|
132
|
+
# @return [void]
|
133
133
|
def self.register_validator(path)
|
134
134
|
_register_asset(path, Pref::ASSET_TYPE_VALIDATOR)
|
135
135
|
end
|
data/spec/configuration_spec.rb
CHANGED
@@ -6,12 +6,28 @@ require 'cliutils/configurator'
|
|
6
6
|
describe CLIUtils::Configuration do
|
7
7
|
include CLIUtils::Configuration
|
8
8
|
|
9
|
+
let(:config_path_new) { File.expand_path('support/configuration2.yaml') }
|
10
|
+
let(:config_path_existing) { File.expand_path('support/configuration.yaml') }
|
11
|
+
let(:existing_data) { { my_app: {
|
12
|
+
config_location: '/Users/bob/.my-app-config',
|
13
|
+
log_level: 'WARN',
|
14
|
+
version: '1.0.0' },
|
15
|
+
user_data: {
|
16
|
+
username: 'bob',
|
17
|
+
age: 45 } } }
|
18
|
+
|
9
19
|
it 'raises an exception if not loaded properly' do
|
10
20
|
m = 'Attempted to access `configuration` before executing `load_configuration`'
|
11
21
|
expect { configuration }.to raise_error(RuntimeError, m)
|
12
22
|
end
|
13
23
|
|
14
|
-
|
24
|
+
it 'returns the same Configurator each time' do
|
25
|
+
load_configuration(config_path_new)
|
26
|
+
c1 = configuration
|
27
|
+
c2 = configuration
|
28
|
+
expect(c1).to eq(c2)
|
29
|
+
end
|
30
|
+
|
15
31
|
it 'initializies configuration from scratch' do
|
16
32
|
load_configuration(config_path_new)
|
17
33
|
expect(configuration.class).to eq(CLIUtils::Configurator)
|
@@ -19,19 +35,10 @@ describe CLIUtils::Configuration do
|
|
19
35
|
expect(configuration.data).to eq({})
|
20
36
|
end
|
21
37
|
|
22
|
-
let(:config_path_existing) { File.expand_path('support/configuration.yaml') }
|
23
|
-
let(:existing_data) { { my_app: {
|
24
|
-
config_location: '/Users/bob/.my-app-config',
|
25
|
-
log_level: 'WARN',
|
26
|
-
version: '1.0.0' },
|
27
|
-
user_data: {
|
28
|
-
username: 'bob',
|
29
|
-
age: 45 } } }
|
30
|
-
|
31
38
|
it 'works with existing configuration data' do
|
32
39
|
load_configuration(config_path_existing)
|
33
40
|
expect(configuration.class).to eq(CLIUtils::Configurator)
|
34
41
|
expect(configuration.config_path).to eq(config_path_existing)
|
35
42
|
expect(configuration.data).to eq(existing_data)
|
36
43
|
end
|
37
|
-
end
|
44
|
+
end
|
data/spec/messaging_spec.rb
CHANGED
@@ -7,6 +7,12 @@ require 'cliutils/pretty_io'
|
|
7
7
|
describe CLIUtils::Messaging do
|
8
8
|
include CLIUtils::Messaging
|
9
9
|
|
10
|
+
it 'returns the same messenger each time' do
|
11
|
+
m1 = messenger
|
12
|
+
m2 = messenger
|
13
|
+
expect(m1).to eq(m2)
|
14
|
+
end
|
15
|
+
|
10
16
|
it 'gives a set of standard message formats' do
|
11
17
|
out = capture_stdout { messenger.error('error') }
|
12
18
|
expect(out).to eq('# error'.red + "\n")
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Bach
|
@@ -128,22 +128,6 @@ files:
|
|
128
128
|
- lib/cliutils/prefs/pref_validators/time_validator.rb
|
129
129
|
- lib/cliutils/prefs/pref_validators/url_validator.rb
|
130
130
|
- lib/cliutils/pretty_io.rb
|
131
|
-
- res/readme-images/actions-1.png
|
132
|
-
- res/readme-images/actions-2.png
|
133
|
-
- res/readme-images/actions-3.png
|
134
|
-
- res/readme-images/messenger-types-1.png
|
135
|
-
- res/readme-images/messenger-warn.png
|
136
|
-
- res/readme-images/multi-logger.png
|
137
|
-
- res/readme-images/prefs-ask-behaviors.png
|
138
|
-
- res/readme-images/prefs-ask-options.png
|
139
|
-
- res/readme-images/prefs-ask-prereqs.png
|
140
|
-
- res/readme-images/prefs-ask-validators.png
|
141
|
-
- res/readme-images/prefs-ask.png
|
142
|
-
- res/readme-images/prettyio-color-chart.png
|
143
|
-
- res/readme-images/prettyio-gnarly-text.png
|
144
|
-
- res/readme-images/prettyio-red-text.png
|
145
|
-
- res/readme-images/prompting.png
|
146
|
-
- res/readme-images/wrapping.png
|
147
131
|
- spec/action/open_url_action_spec.rb
|
148
132
|
- spec/action/pref_action_spec.rb
|
149
133
|
- spec/behavior/capitalize_behavior_spec.rb
|
@@ -175,15 +159,28 @@ files:
|
|
175
159
|
- spec/validator/url_validator_spec.rb
|
176
160
|
- support/configuration.yaml
|
177
161
|
- support/prefstest.yaml
|
162
|
+
- support/readme-images/actions-1.png
|
163
|
+
- support/readme-images/actions-2.png
|
164
|
+
- support/readme-images/actions-3.png
|
165
|
+
- support/readme-images/messenger-types-1.png
|
166
|
+
- support/readme-images/messenger-warn.png
|
167
|
+
- support/readme-images/multi-logger.png
|
168
|
+
- support/readme-images/prefs-ask-behaviors.png
|
169
|
+
- support/readme-images/prefs-ask-options.png
|
170
|
+
- support/readme-images/prefs-ask-prereqs.png
|
171
|
+
- support/readme-images/prefs-ask-validators.png
|
172
|
+
- support/readme-images/prefs-ask.png
|
173
|
+
- support/readme-images/prettyio-color-chart.png
|
174
|
+
- support/readme-images/prettyio-gnarly-text.png
|
175
|
+
- support/readme-images/prettyio-red-text.png
|
176
|
+
- support/readme-images/prompting.png
|
177
|
+
- support/readme-images/wrapping.png
|
178
178
|
- support/test_action.rb
|
179
179
|
- support/test_action_empty.rb
|
180
180
|
- support/test_behavior.rb
|
181
181
|
- support/test_behavior_empty.rb
|
182
182
|
- support/test_validator.rb
|
183
183
|
- support/test_validator_empty.rb
|
184
|
-
- test/pref_test.rb
|
185
|
-
- test/prefs_test.rb
|
186
|
-
- test/test_helper.rb
|
187
184
|
homepage: https://github.com/bachya/cliutils
|
188
185
|
licenses:
|
189
186
|
- MIT
|
@@ -238,7 +235,4 @@ test_files:
|
|
238
235
|
- spec/validator/pref_validator_spec.rb
|
239
236
|
- spec/validator/time_validator_spec.rb
|
240
237
|
- spec/validator/url_validator_spec.rb
|
241
|
-
- test/pref_test.rb
|
242
|
-
- test/prefs_test.rb
|
243
|
-
- test/test_helper.rb
|
244
238
|
has_rdoc:
|
data/test/pref_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/hash_extensions')
|
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')
|
9
|
-
|
10
|
-
class TestPref < Test::Unit::TestCase
|
11
|
-
def setup
|
12
|
-
@prefs_hash = {
|
13
|
-
behaviors: [{ name: 'test' }],
|
14
|
-
config_key: 'test_prompt',
|
15
|
-
config_section: 'app_data',
|
16
|
-
default: 'bachya',
|
17
|
-
options: ['a', 'b'],
|
18
|
-
pre: {
|
19
|
-
message: 'Test pre message',
|
20
|
-
action: 'test'
|
21
|
-
},
|
22
|
-
post: {
|
23
|
-
message: 'Test post message',
|
24
|
-
action: 'test'
|
25
|
-
},
|
26
|
-
prereqs: [
|
27
|
-
{ config_section: 'section' },
|
28
|
-
{ config_value: 'value' }
|
29
|
-
],
|
30
|
-
prompt_text: 'Test',
|
31
|
-
validators: ['test'],
|
32
|
-
}
|
33
|
-
|
34
|
-
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
35
|
-
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
36
|
-
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
37
|
-
end
|
38
|
-
|
39
|
-
def teardown
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_initialization
|
44
|
-
pref = CLIUtils::Pref.new(@prefs_hash)
|
45
|
-
|
46
|
-
assert_equal(pref.answer, nil)
|
47
|
-
assert_equal(pref.behavior_objects[0].class, CLIUtils::TestBehavior.new.class)
|
48
|
-
assert_equal(pref.behaviors, [{ name: 'test' }])
|
49
|
-
assert_equal(pref.config_key, @prefs_hash[:config_key])
|
50
|
-
assert_equal(pref.config_section, @prefs_hash[:config_section])
|
51
|
-
assert_equal(pref.default, @prefs_hash[:default])
|
52
|
-
assert_equal(pref.last_error_message, nil)
|
53
|
-
assert_equal(pref.options, ['a', 'b'])
|
54
|
-
assert_equal(pref.post, { message: 'Test post message', action: 'test' })
|
55
|
-
assert_equal(pref.pre, { message: 'Test pre message', action: 'test' })
|
56
|
-
assert_equal(pref.prereqs, [{ config_section: 'section' }, { config_value: 'value' }])
|
57
|
-
assert_equal(pref.validator_objects[0].class, CLIUtils::TestValidator.new.class)
|
58
|
-
assert_equal(pref.validators, ['test'])
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_action
|
62
|
-
require File.join(File.dirname(__FILE__), 'test_files/test_action.rb')
|
63
|
-
a = CLIUtils::TestAction.new
|
64
|
-
assert_output("here\n") { a.run }
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_behavior
|
68
|
-
require File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb')
|
69
|
-
b = CLIUtils::TestBehavior.new
|
70
|
-
assert_equal(b.evaluate('test'), 'test_behavior: test')
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_validator
|
74
|
-
require File.join(File.dirname(__FILE__), 'test_files/test_validator.rb')
|
75
|
-
v = CLIUtils::TestValidator.new
|
76
|
-
v.validate('bachya')
|
77
|
-
|
78
|
-
assert_equal(v.is_valid, true)
|
79
|
-
assert_equal(v.message, "String did not equal 'bachya': bachya")
|
80
|
-
end
|
81
|
-
end
|
data/test/prefs_test.rb
DELETED
@@ -1,164 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'tempfile'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/hash_extensions')
|
6
|
-
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs')
|
7
|
-
require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/prefs/pref')
|
8
|
-
|
9
|
-
class TestPrefs < Test::Unit::TestCase
|
10
|
-
def setup
|
11
|
-
@prefs_arr = [
|
12
|
-
{
|
13
|
-
'prompt_text' => 'Batman or Superman?',
|
14
|
-
'default' => 'Batman',
|
15
|
-
'config_key' => 'superhero',
|
16
|
-
'config_section' => 'personal_info'
|
17
|
-
},
|
18
|
-
{
|
19
|
-
'prompt_text' => 'Do you feel smart for preferring Batman?',
|
20
|
-
'default' => 'Y',
|
21
|
-
'config_key' => 'batman_answer',
|
22
|
-
'config_section' => 'personal_info',
|
23
|
-
'prereqs' => [
|
24
|
-
{
|
25
|
-
'config_key' => 'superhero',
|
26
|
-
'config_value' => 'Batman'
|
27
|
-
}
|
28
|
-
]
|
29
|
-
},
|
30
|
-
{
|
31
|
-
'prompt_text' => 'Why do you prefer Superman?!',
|
32
|
-
'default' => 'No clue',
|
33
|
-
'config_key' => 'superman_answer',
|
34
|
-
'config_section' => 'personal_info',
|
35
|
-
'prereqs' => [
|
36
|
-
{
|
37
|
-
'config_key' => 'superhero',
|
38
|
-
'config_value' => 'Superman'
|
39
|
-
}
|
40
|
-
]
|
41
|
-
}
|
42
|
-
]
|
43
|
-
|
44
|
-
@prefs_hash = {:prompts=>@prefs_arr}
|
45
|
-
@prefs_filepath = '/tmp/prefstest.yaml'
|
46
|
-
FileUtils.cp(File.join(File.dirname(__FILE__), '..', 'test/test_files/prefstest.yaml'), @prefs_filepath)
|
47
|
-
end
|
48
|
-
|
49
|
-
def teardown
|
50
|
-
FileUtils.rm(@prefs_filepath) if File.file?(@prefs_filepath)
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_file_creation
|
54
|
-
p = CLIUtils::Prefs.new(@prefs_filepath)
|
55
|
-
prefs = YAML::load_file(@prefs_filepath).deep_symbolize_keys
|
56
|
-
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_bad_file_creation
|
60
|
-
exception = assert_raise(RuntimeError) { p = CLIUtils::Prefs.new('asd') }
|
61
|
-
assert_equal('Invalid configuration file: asd', exception.message)
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_array_creation
|
65
|
-
p = CLIUtils::Prefs.new(@prefs_arr)
|
66
|
-
prefs = @prefs_hash.deep_symbolize_keys
|
67
|
-
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_hash_creation
|
71
|
-
p = CLIUtils::Prefs.new(@prefs_hash)
|
72
|
-
prefs = @prefs_hash.deep_symbolize_keys
|
73
|
-
assert_equal(prefs[:prompts].map { |p| CLIUtils::Pref.new(p) }, p.prompts)
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_invalid_type_creation
|
77
|
-
exception = assert_raise(RuntimeError) { p = CLIUtils::Prefs.new(123) }
|
78
|
-
assert_equal('Invalid configuration data', exception.message)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_register
|
82
|
-
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
83
|
-
assert_equal(CLIUtils::Prefs.registered_actions.key?(:Test), true)
|
84
|
-
assert_equal(CLIUtils::Prefs.registered_actions[:Test][:class], 'TestAction')
|
85
|
-
assert_equal(CLIUtils::Prefs.registered_actions[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
86
|
-
|
87
|
-
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
88
|
-
assert_equal(CLIUtils::Prefs.registered_behaviors.key?(:Test), true)
|
89
|
-
assert_equal(CLIUtils::Prefs.registered_behaviors[:Test][:class], 'TestBehavior')
|
90
|
-
assert_equal(CLIUtils::Prefs.registered_behaviors[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
91
|
-
|
92
|
-
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
93
|
-
assert_equal(CLIUtils::Prefs.registered_validators.key?(:Test), true)
|
94
|
-
assert_equal(CLIUtils::Prefs.registered_validators[:Test][:class], 'TestValidator')
|
95
|
-
assert_equal(CLIUtils::Prefs.registered_validators[:Test][:path], File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_bad_registration
|
99
|
-
m = 'Registration failed because of unknown filepath: bachya.rb'
|
100
|
-
|
101
|
-
exception = assert_raise(RuntimeError) { CLIUtils::Prefs.register_action('bachya.rb') }
|
102
|
-
assert_equal(m, exception.message)
|
103
|
-
|
104
|
-
exception = assert_raise(RuntimeError) { CLIUtils::Prefs.register_behavior('bachya.rb') }
|
105
|
-
assert_equal(m, exception.message)
|
106
|
-
|
107
|
-
exception = assert_raise(RuntimeError) { CLIUtils::Prefs.register_validator('bachya.rb') }
|
108
|
-
assert_equal(m, exception.message)
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_deregister
|
112
|
-
CLIUtils::Prefs.register_action(File.join(File.dirname(__FILE__), 'test_files/test_action.rb'))
|
113
|
-
CLIUtils::Prefs.deregister_action(:Test)
|
114
|
-
assert_equal(CLIUtils::Prefs.registered_actions.key?(:Test), false)
|
115
|
-
|
116
|
-
CLIUtils::Prefs.register_behavior(File.join(File.dirname(__FILE__), 'test_files/test_behavior.rb'))
|
117
|
-
CLIUtils::Prefs.deregister_behavior(:Test)
|
118
|
-
assert_equal(CLIUtils::Prefs.registered_behaviors.key?(:Test), false)
|
119
|
-
|
120
|
-
CLIUtils::Prefs.register_validator(File.join(File.dirname(__FILE__), 'test_files/test_validator.rb'))
|
121
|
-
CLIUtils::Prefs.deregister_validator(:Test)
|
122
|
-
assert_equal(CLIUtils::Prefs.registered_validators.key?(:Test), false)
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_ask
|
126
|
-
p = CLIUtils::Prefs.new(@prefs_filepath)
|
127
|
-
stdin = Tempfile.new("test_readline_stdin")
|
128
|
-
stdout = Tempfile.new("test_readline_stdout")
|
129
|
-
begin
|
130
|
-
stdin.write("\n")
|
131
|
-
stdin.close
|
132
|
-
stdout.close
|
133
|
-
line = replace_stdio(stdin.path, stdout.path) {
|
134
|
-
p.ask
|
135
|
-
}
|
136
|
-
ensure
|
137
|
-
stdin.close(true)
|
138
|
-
stdout.close(true)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
private
|
143
|
-
|
144
|
-
def replace_stdio(stdin_path, stdout_path)
|
145
|
-
open(stdin_path, "r"){|stdin|
|
146
|
-
open(stdout_path, "w"){|stdout|
|
147
|
-
orig_stdin = STDIN.dup
|
148
|
-
orig_stdout = STDOUT.dup
|
149
|
-
STDIN.reopen(stdin)
|
150
|
-
STDOUT.reopen(stdout)
|
151
|
-
begin
|
152
|
-
Readline.input = STDIN
|
153
|
-
Readline.output = STDOUT
|
154
|
-
yield
|
155
|
-
ensure
|
156
|
-
STDIN.reopen(orig_stdin)
|
157
|
-
STDOUT.reopen(orig_stdout)
|
158
|
-
orig_stdin.close
|
159
|
-
orig_stdout.close
|
160
|
-
end
|
161
|
-
}
|
162
|
-
}
|
163
|
-
end
|
164
|
-
end
|