cliutils 2.1.4 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +3 -2
- data/Gemfile +7 -0
- data/HISTORY.md +7 -0
- data/README.md +3 -1
- data/Rakefile +8 -1
- data/cliutils.gemspec +1 -0
- data/lib/cliutils/configuration.rb +1 -1
- data/lib/cliutils/constants.rb +1 -1
- data/lib/cliutils/ext/hash_extensions.rb +16 -12
- data/lib/cliutils/messaging.rb +2 -116
- data/lib/cliutils/messenger.rb +109 -0
- data/lib/cliutils/prefs/pref.rb +10 -10
- data/lib/cliutils/prefs/pref_actions/open_url_action.rb +1 -1
- data/lib/cliutils/prefs/pref_validators/filepath_exists_validator.rb +2 -0
- data/lib/cliutils/prefs/pref_validators/url_validator.rb +1 -1
- data/lib/cliutils/prefs.rb +1 -1
- data/lib/cliutils/pretty_io.rb +0 -10
- data/lib/cliutils.rb +1 -1
- data/spec/action/open_url_action_spec.rb +20 -0
- data/spec/action/pref_action_spec.rb +11 -0
- data/spec/behavior/capitalize_behavior_spec.rb +11 -0
- data/spec/behavior/expand_filepath_behavior_spec.rb +11 -0
- data/spec/behavior/lowercase_behavior_spec.rb +11 -0
- data/spec/behavior/pref_behavior_spec.rb +11 -0
- data/spec/behavior/prefix_behavior_spec.rb +12 -0
- data/spec/behavior/suffix_behavior_spec.rb +12 -0
- data/spec/behavior/titlecase_behavior_spec.rb +10 -0
- data/spec/behavior/uppercase_behavior_spec.rb +11 -0
- data/spec/configuration_spec.rb +37 -0
- data/spec/configurator_spec.rb +104 -0
- data/spec/ext/hash_extensions_spec.rb +54 -0
- data/spec/ext/logger_extensions_spec.rb +20 -0
- data/spec/ext/string_extensions_spec.rb +26 -0
- data/spec/messaging_spec.rb +91 -0
- data/spec/pref_spec.rb +144 -0
- data/spec/prefs_spec.rb +150 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/validator/alphabetic_validator_spec.rb +20 -0
- data/spec/validator/alphanumeric_validator_spec.rb +20 -0
- data/spec/validator/date_validator_spec.rb +20 -0
- data/spec/validator/datetime_validator_spec.rb +20 -0
- data/spec/validator/filepath_exists_validator_spec.rb +20 -0
- data/spec/validator/non_nil_validator_spec.rb +24 -0
- data/spec/validator/number_validator_spec.rb +20 -0
- data/spec/validator/pref_validator_spec.rb +11 -0
- data/spec/validator/time_validator_spec.rb +20 -0
- data/spec/validator/url_validator_spec.rb +20 -0
- data/{test/test_files → support}/configuration.yaml +1 -1
- data/support/prefstest.yaml +27 -0
- data/{test/test_files → support}/test_action.rb +0 -0
- data/support/test_action_empty.rb +7 -0
- data/{test/test_files → support}/test_behavior.rb +2 -2
- data/support/test_behavior_empty.rb +6 -0
- data/{test/test_files → support}/test_validator.rb +0 -2
- data/support/test_validator_empty.rb +7 -0
- data/test/pref_test.rb +0 -1
- data/test/prefs_test.rb +64 -1
- data/test/test_helper.rb +2 -2
- metadata +91 -67
- data/lib/cliutils/logger_delegator.rb +0 -49
- data/test/action_tests/open_url_action_test.rb +0 -12
- data/test/behavior_tests/capitalize_behavior_test.rb +0 -11
- data/test/behavior_tests/expand_filepath_behavior_test.rb +0 -11
- data/test/behavior_tests/lowercase_behavior_test.rb +0 -11
- data/test/behavior_tests/prefix_behavior_test.rb +0 -12
- data/test/behavior_tests/suffix_behavior_test.rb +0 -12
- data/test/behavior_tests/titlecase_behavior_test.rb +0 -11
- data/test/behavior_tests/uppercase_behavior_test.rb +0 -11
- data/test/configuration_test.rb +0 -49
- data/test/configurator_test.rb +0 -63
- data/test/hash_extensions_test.rb +0 -51
- data/test/logger_extensions_test.rb +0 -17
- data/test/messaging_test.rb +0 -53
- data/test/string_extesions_test.rb +0 -28
- data/test/test_files/prefstest.yaml +0 -38
- data/test/validator_tests/alphabetic_validator_test.rb +0 -22
- data/test/validator_tests/alphanumeric_validator_test.rb +0 -22
- data/test/validator_tests/date_validator_test.rb +0 -22
- data/test/validator_tests/datetime_validator_test.rb +0 -22
- data/test/validator_tests/filepath_exists_validator_test.rb +0 -22
- data/test/validator_tests/non_nil_validator_test.rb +0 -30
- data/test/validator_tests/number_validator_test.rb +0 -22
- data/test/validator_tests/time_validator_test.rb +0 -22
- data/test/validator_tests/url_validator_test.rb +0 -22
@@ -0,0 +1,91 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'cliutils/ext/logger_extensions'
|
3
|
+
require 'cliutils/ext/string_extensions'
|
4
|
+
require 'cliutils/messaging'
|
5
|
+
require 'cliutils/pretty_io'
|
6
|
+
|
7
|
+
describe CLIUtils::Messaging do
|
8
|
+
include CLIUtils::Messaging
|
9
|
+
|
10
|
+
it 'gives a set of standard message formats' do
|
11
|
+
out = capture_stdout { messenger.error('error') }
|
12
|
+
expect(out).to eq('# error'.red + "\n")
|
13
|
+
|
14
|
+
out = capture_stdout { messenger.info('info') }
|
15
|
+
expect(out).to eq('# info'.blue + "\n")
|
16
|
+
|
17
|
+
out = capture_stdout { messenger.section('section') }
|
18
|
+
expect(out).to eq('---> section'.purple + "\n")
|
19
|
+
|
20
|
+
out = capture_stdout { messenger.success('success') }
|
21
|
+
expect(out).to eq('# success'.green + "\n")
|
22
|
+
|
23
|
+
out = capture_stdout { messenger.warn('warn') }
|
24
|
+
expect(out).to eq('# warn'.yellow + "\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'wraps strings' do
|
28
|
+
CLIUtils::PrettyIO.wrap = true
|
29
|
+
CLIUtils::PrettyIO.wrap_char_limit = 35
|
30
|
+
long_str = 'This is a really long string that should wrap itself at ' \
|
31
|
+
'some point, okay?'
|
32
|
+
expected_str = "# This is a really long string that\n# should wrap " \
|
33
|
+
"itself at some point,\n# okay?"
|
34
|
+
|
35
|
+
out = capture_stdout { messenger.info(long_str) }
|
36
|
+
expect(out).to eq(expected_str.blue + "\n")
|
37
|
+
|
38
|
+
CLIUtils::PrettyIO.wrap = false
|
39
|
+
out = capture_stdout { messenger.info(long_str) }
|
40
|
+
expect(out).to eq("# #{ long_str }".blue + "\n")
|
41
|
+
end
|
42
|
+
|
43
|
+
let(:output_filepath) { '/tmp/file1.txt' }
|
44
|
+
it 'attaches and detaches various Logger instances' do
|
45
|
+
file_logger = Logger.new(output_filepath)
|
46
|
+
file_logger.formatter = proc do |severity, datetime, progname, msg|
|
47
|
+
"#{ severity }: #{ msg }\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
out = capture_stdout do
|
51
|
+
messenger.attach(FILE: file_logger)
|
52
|
+
messenger.debug('Debug test')
|
53
|
+
messenger.info('Info test')
|
54
|
+
messenger.error('Error test')
|
55
|
+
messenger.detach(:FILE)
|
56
|
+
messenger.warn('Warn test')
|
57
|
+
end
|
58
|
+
|
59
|
+
File.open(output_filepath, 'r') do |f|
|
60
|
+
file_contents = f.read.lines.to_a[1..-1].join
|
61
|
+
m = "DEBUG: Debug test\nINFO: Info test\nERROR: Error test\n"
|
62
|
+
expect(file_contents).to eq(m)
|
63
|
+
end
|
64
|
+
FileUtils.rm(output_filepath)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'raises an exception when attempting to detach an unknown target' do
|
68
|
+
m = 'Cannot detach invalid target: bachya'
|
69
|
+
expect { messenger.detach(:bachya) }.to raise_error(RuntimeError, m)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'allows users to deliver answers to prompts' do
|
73
|
+
Readline.stub(:readline).and_return('Yes', '')
|
74
|
+
Readline.stub(:completion_proc=).and_return("\t")
|
75
|
+
expect(messenger.prompt('Do you like cheese?', 'No')).to eq('Yes')
|
76
|
+
expect(messenger.prompt('Do you like cheese?', 'No')).to eq('No')
|
77
|
+
expect(messenger.prompt('Do you like cheese?', '')).to eq('')
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'warns about deprecated methods' do
|
81
|
+
out = capture_stdout { messenger.info_block('test') }
|
82
|
+
s = 'As of 2.2.0, `info_block` is deprecated and nonfunctioning.'
|
83
|
+
expect(out).to eq("# #{ s }".yellow + "\n")
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'outputs a useful ASCII color chart' do
|
87
|
+
s = "----------------------------------------------------------------\nESC[0;Foreground;Background\n\e[0;30;40m 30;40 \e[0;30;41m 30;41 \e[0;30;42m 30;42 \e[0;30;43m 30;43 \e[0;30;44m 30;44 \e[0;30;45m 30;45 \e[0;30;46m 30;46 \e[0;30;47m 30;47 \e[0m\n\e[0;31;40m 31;40 \e[0;31;41m 31;41 \e[0;31;42m 31;42 \e[0;31;43m 31;43 \e[0;31;44m 31;44 \e[0;31;45m 31;45 \e[0;31;46m 31;46 \e[0;31;47m 31;47 \e[0m\n\e[0;32;40m 32;40 \e[0;32;41m 32;41 \e[0;32;42m 32;42 \e[0;32;43m 32;43 \e[0;32;44m 32;44 \e[0;32;45m 32;45 \e[0;32;46m 32;46 \e[0;32;47m 32;47 \e[0m\n\e[0;33;40m 33;40 \e[0;33;41m 33;41 \e[0;33;42m 33;42 \e[0;33;43m 33;43 \e[0;33;44m 33;44 \e[0;33;45m 33;45 \e[0;33;46m 33;46 \e[0;33;47m 33;47 \e[0m\n\e[0;34;40m 34;40 \e[0;34;41m 34;41 \e[0;34;42m 34;42 \e[0;34;43m 34;43 \e[0;34;44m 34;44 \e[0;34;45m 34;45 \e[0;34;46m 34;46 \e[0;34;47m 34;47 \e[0m\n\e[0;35;40m 35;40 \e[0;35;41m 35;41 \e[0;35;42m 35;42 \e[0;35;43m 35;43 \e[0;35;44m 35;44 \e[0;35;45m 35;45 \e[0;35;46m 35;46 \e[0;35;47m 35;47 \e[0m\n\e[0;36;40m 36;40 \e[0;36;41m 36;41 \e[0;36;42m 36;42 \e[0;36;43m 36;43 \e[0;36;44m 36;44 \e[0;36;45m 36;45 \e[0;36;46m 36;46 \e[0;36;47m 36;47 \e[0m\n\e[0;37;40m 37;40 \e[0;37;41m 37;41 \e[0;37;42m 37;42 \e[0;37;43m 37;43 \e[0;37;44m 37;44 \e[0;37;45m 37;45 \e[0;37;46m 37;46 \e[0;37;47m 37;47 \e[0m\n----------------------------------------------------------------\nESC[1;Foreground;Background\n\e[1;30;40m 30;40 \e[1;30;41m 30;41 \e[1;30;42m 30;42 \e[1;30;43m 30;43 \e[1;30;44m 30;44 \e[1;30;45m 30;45 \e[1;30;46m 30;46 \e[1;30;47m 30;47 \e[0m\n\e[1;31;40m 31;40 \e[1;31;41m 31;41 \e[1;31;42m 31;42 \e[1;31;43m 31;43 \e[1;31;44m 31;44 \e[1;31;45m 31;45 \e[1;31;46m 31;46 \e[1;31;47m 31;47 \e[0m\n\e[1;32;40m 32;40 \e[1;32;41m 32;41 \e[1;32;42m 32;42 \e[1;32;43m 32;43 \e[1;32;44m 32;44 \e[1;32;45m 32;45 \e[1;32;46m 32;46 \e[1;32;47m 32;47 \e[0m\n\e[1;33;40m 33;40 \e[1;33;41m 33;41 \e[1;33;42m 33;42 \e[1;33;43m 33;43 \e[1;33;44m 33;44 \e[1;33;45m 33;45 \e[1;33;46m 33;46 \e[1;33;47m 33;47 \e[0m\n\e[1;34;40m 34;40 \e[1;34;41m 34;41 \e[1;34;42m 34;42 \e[1;34;43m 34;43 \e[1;34;44m 34;44 \e[1;34;45m 34;45 \e[1;34;46m 34;46 \e[1;34;47m 34;47 \e[0m\n\e[1;35;40m 35;40 \e[1;35;41m 35;41 \e[1;35;42m 35;42 \e[1;35;43m 35;43 \e[1;35;44m 35;44 \e[1;35;45m 35;45 \e[1;35;46m 35;46 \e[1;35;47m 35;47 \e[0m\n\e[1;36;40m 36;40 \e[1;36;41m 36;41 \e[1;36;42m 36;42 \e[1;36;43m 36;43 \e[1;36;44m 36;44 \e[1;36;45m 36;45 \e[1;36;46m 36;46 \e[1;36;47m 36;47 \e[0m\n\e[1;37;40m 37;40 \e[1;37;41m 37;41 \e[1;37;42m 37;42 \e[1;37;43m 37;43 \e[1;37;44m 37;44 \e[1;37;45m 37;45 \e[1;37;46m 37;46 \e[1;37;47m 37;47 \e[0m\n----------------------------------------------------------------\nESC[4;Foreground;Background\n\e[4;30;40m 30;40 \e[4;30;41m 30;41 \e[4;30;42m 30;42 \e[4;30;43m 30;43 \e[4;30;44m 30;44 \e[4;30;45m 30;45 \e[4;30;46m 30;46 \e[4;30;47m 30;47 \e[0m\n\e[4;31;40m 31;40 \e[4;31;41m 31;41 \e[4;31;42m 31;42 \e[4;31;43m 31;43 \e[4;31;44m 31;44 \e[4;31;45m 31;45 \e[4;31;46m 31;46 \e[4;31;47m 31;47 \e[0m\n\e[4;32;40m 32;40 \e[4;32;41m 32;41 \e[4;32;42m 32;42 \e[4;32;43m 32;43 \e[4;32;44m 32;44 \e[4;32;45m 32;45 \e[4;32;46m 32;46 \e[4;32;47m 32;47 \e[0m\n\e[4;33;40m 33;40 \e[4;33;41m 33;41 \e[4;33;42m 33;42 \e[4;33;43m 33;43 \e[4;33;44m 33;44 \e[4;33;45m 33;45 \e[4;33;46m 33;46 \e[4;33;47m 33;47 \e[0m\n\e[4;34;40m 34;40 \e[4;34;41m 34;41 \e[4;34;42m 34;42 \e[4;34;43m 34;43 \e[4;34;44m 34;44 \e[4;34;45m 34;45 \e[4;34;46m 34;46 \e[4;34;47m 34;47 \e[0m\n\e[4;35;40m 35;40 \e[4;35;41m 35;41 \e[4;35;42m 35;42 \e[4;35;43m 35;43 \e[4;35;44m 35;44 \e[4;35;45m 35;45 \e[4;35;46m 35;46 \e[4;35;47m 35;47 \e[0m\n\e[4;36;40m 36;40 \e[4;36;41m 36;41 \e[4;36;42m 36;42 \e[4;36;43m 36;43 \e[4;36;44m 36;44 \e[4;36;45m 36;45 \e[4;36;46m 36;46 \e[4;36;47m 36;47 \e[0m\n\e[4;37;40m 37;40 \e[4;37;41m 37;41 \e[4;37;42m 37;42 \e[4;37;43m 37;43 \e[4;37;44m 37;44 \e[4;37;45m 37;45 \e[4;37;46m 37;46 \e[4;37;47m 37;47 \e[0m\n----------------------------------------------------------------\nESC[5;Foreground;Background\n\e[5;30;40m 30;40 \e[5;30;41m 30;41 \e[5;30;42m 30;42 \e[5;30;43m 30;43 \e[5;30;44m 30;44 \e[5;30;45m 30;45 \e[5;30;46m 30;46 \e[5;30;47m 30;47 \e[0m\n\e[5;31;40m 31;40 \e[5;31;41m 31;41 \e[5;31;42m 31;42 \e[5;31;43m 31;43 \e[5;31;44m 31;44 \e[5;31;45m 31;45 \e[5;31;46m 31;46 \e[5;31;47m 31;47 \e[0m\n\e[5;32;40m 32;40 \e[5;32;41m 32;41 \e[5;32;42m 32;42 \e[5;32;43m 32;43 \e[5;32;44m 32;44 \e[5;32;45m 32;45 \e[5;32;46m 32;46 \e[5;32;47m 32;47 \e[0m\n\e[5;33;40m 33;40 \e[5;33;41m 33;41 \e[5;33;42m 33;42 \e[5;33;43m 33;43 \e[5;33;44m 33;44 \e[5;33;45m 33;45 \e[5;33;46m 33;46 \e[5;33;47m 33;47 \e[0m\n\e[5;34;40m 34;40 \e[5;34;41m 34;41 \e[5;34;42m 34;42 \e[5;34;43m 34;43 \e[5;34;44m 34;44 \e[5;34;45m 34;45 \e[5;34;46m 34;46 \e[5;34;47m 34;47 \e[0m\n\e[5;35;40m 35;40 \e[5;35;41m 35;41 \e[5;35;42m 35;42 \e[5;35;43m 35;43 \e[5;35;44m 35;44 \e[5;35;45m 35;45 \e[5;35;46m 35;46 \e[5;35;47m 35;47 \e[0m\n\e[5;36;40m 36;40 \e[5;36;41m 36;41 \e[5;36;42m 36;42 \e[5;36;43m 36;43 \e[5;36;44m 36;44 \e[5;36;45m 36;45 \e[5;36;46m 36;46 \e[5;36;47m 36;47 \e[0m\n\e[5;37;40m 37;40 \e[5;37;41m 37;41 \e[5;37;42m 37;42 \e[5;37;43m 37;43 \e[5;37;44m 37;44 \e[5;37;45m 37;45 \e[5;37;46m 37;46 \e[5;37;47m 37;47 \e[0m\n----------------------------------------------------------------\nESC[7;Foreground;Background\n\e[7;30;40m 30;40 \e[7;30;41m 30;41 \e[7;30;42m 30;42 \e[7;30;43m 30;43 \e[7;30;44m 30;44 \e[7;30;45m 30;45 \e[7;30;46m 30;46 \e[7;30;47m 30;47 \e[0m\n\e[7;31;40m 31;40 \e[7;31;41m 31;41 \e[7;31;42m 31;42 \e[7;31;43m 31;43 \e[7;31;44m 31;44 \e[7;31;45m 31;45 \e[7;31;46m 31;46 \e[7;31;47m 31;47 \e[0m\n\e[7;32;40m 32;40 \e[7;32;41m 32;41 \e[7;32;42m 32;42 \e[7;32;43m 32;43 \e[7;32;44m 32;44 \e[7;32;45m 32;45 \e[7;32;46m 32;46 \e[7;32;47m 32;47 \e[0m\n\e[7;33;40m 33;40 \e[7;33;41m 33;41 \e[7;33;42m 33;42 \e[7;33;43m 33;43 \e[7;33;44m 33;44 \e[7;33;45m 33;45 \e[7;33;46m 33;46 \e[7;33;47m 33;47 \e[0m\n\e[7;34;40m 34;40 \e[7;34;41m 34;41 \e[7;34;42m 34;42 \e[7;34;43m 34;43 \e[7;34;44m 34;44 \e[7;34;45m 34;45 \e[7;34;46m 34;46 \e[7;34;47m 34;47 \e[0m\n\e[7;35;40m 35;40 \e[7;35;41m 35;41 \e[7;35;42m 35;42 \e[7;35;43m 35;43 \e[7;35;44m 35;44 \e[7;35;45m 35;45 \e[7;35;46m 35;46 \e[7;35;47m 35;47 \e[0m\n\e[7;36;40m 36;40 \e[7;36;41m 36;41 \e[7;36;42m 36;42 \e[7;36;43m 36;43 \e[7;36;44m 36;44 \e[7;36;45m 36;45 \e[7;36;46m 36;46 \e[7;36;47m 36;47 \e[0m\n\e[7;37;40m 37;40 \e[7;37;41m 37;41 \e[7;37;42m 37;42 \e[7;37;43m 37;43 \e[7;37;44m 37;44 \e[7;37;45m 37;45 \e[7;37;46m 37;46 \e[7;37;47m 37;47 \e[0m\n"
|
88
|
+
out = capture_stdout { messenger.color_chart }
|
89
|
+
expect(out).to eq(s)
|
90
|
+
end
|
91
|
+
end
|
data/spec/pref_spec.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'cliutils/ext/hash_extensions'
|
3
|
+
require 'cliutils/prefs/pref'
|
4
|
+
require 'cliutils/prefs/pref_actions/pref_action'
|
5
|
+
require 'cliutils/prefs/pref_behaviors/pref_behavior'
|
6
|
+
require 'cliutils/prefs/pref_validators/pref_validator'
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'support/test_action.rb')
|
8
|
+
require File.join(File.dirname(__FILE__), '..', 'support/test_behavior.rb')
|
9
|
+
require File.join(File.dirname(__FILE__), '..', 'support/test_validator.rb')
|
10
|
+
require 'yaml'
|
11
|
+
|
12
|
+
describe CLIUtils::Pref do
|
13
|
+
let(:base_path) { File.join(File.dirname(__FILE__), '..', 'support') }
|
14
|
+
let(:pref_data) {
|
15
|
+
{
|
16
|
+
behaviors: [{ name: "#{ File.join(base_path, 'test_behavior.rb') }" }],
|
17
|
+
config_key: 'test_prompt',
|
18
|
+
config_section: 'app_data',
|
19
|
+
default: 'bachya',
|
20
|
+
options: ['bachya'],
|
21
|
+
pre: {
|
22
|
+
message: 'Test pre message',
|
23
|
+
action: "#{ File.join(base_path, 'test_action.rb') }",
|
24
|
+
parameters: {
|
25
|
+
param1: 'value1'
|
26
|
+
}
|
27
|
+
},
|
28
|
+
post: {
|
29
|
+
message: 'Test post message',
|
30
|
+
action: "#{ File.join(base_path, 'test_action.rb') }"
|
31
|
+
},
|
32
|
+
prereqs: [
|
33
|
+
{ config_section: 'section' },
|
34
|
+
{ config_value: 'value' }
|
35
|
+
],
|
36
|
+
prompt_text: 'Test',
|
37
|
+
validators: ["#{ File.join(base_path, 'test_validator.rb') }"],
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
let(:pref_data2) {
|
42
|
+
{
|
43
|
+
behaviors: [{ name: 'test' }],
|
44
|
+
config_key: 'test_prompt',
|
45
|
+
config_section: 'app_data',
|
46
|
+
default: 'bachya',
|
47
|
+
options: ['bachya'],
|
48
|
+
pre: {
|
49
|
+
message: 'Test pre message',
|
50
|
+
action: 'test',
|
51
|
+
parameters: {
|
52
|
+
param1: 'value1'
|
53
|
+
}
|
54
|
+
},
|
55
|
+
post: {
|
56
|
+
message: 'Test post message',
|
57
|
+
action: 'test'
|
58
|
+
},
|
59
|
+
prereqs: [
|
60
|
+
{ config_section: 'section' },
|
61
|
+
{ config_value: 'value' }
|
62
|
+
],
|
63
|
+
prompt_text: 'Test',
|
64
|
+
validators: ['test'],
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
let(:pref_data3) {
|
69
|
+
{
|
70
|
+
behaviors: [{ name: 'capitalize' }],
|
71
|
+
config_key: 'test_prompt',
|
72
|
+
config_section: 'app_data',
|
73
|
+
prompt_text: 'Test',
|
74
|
+
validators: ['non_nil'],
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
let(:pref_data4) {
|
79
|
+
{
|
80
|
+
config_key: 'test_prompt',
|
81
|
+
config_section: 'app_data',
|
82
|
+
prompt_text: 'Test',
|
83
|
+
validators: ['non_nilz'],
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
it 'instantiates via a hash' do
|
88
|
+
pref = CLIUtils::Pref.new(pref_data)
|
89
|
+
expect(pref.answer).to eq(nil)
|
90
|
+
expect(pref.behavior_objects[0].class).to eq(CLIUtils::TestBehavior)
|
91
|
+
expect(pref.behaviors).to eq([{ name: "#{ File.join(base_path, 'test_behavior.rb') }" }])
|
92
|
+
expect(pref.config_key).to eq(pref_data[:config_key])
|
93
|
+
expect(pref.config_section).to eq(pref_data[:config_section])
|
94
|
+
expect(pref.default).to eq(pref_data[:default])
|
95
|
+
expect(pref.last_error_message).to eq(nil)
|
96
|
+
expect(pref.options).to eq(['bachya'])
|
97
|
+
expect(pref.post).to eq({ message: 'Test post message', action: "#{ File.join(base_path, 'test_action.rb') }" })
|
98
|
+
expect(pref.pre).to eq({message: 'Test pre message', action: "#{ File.join(base_path, 'test_action.rb') }", parameters: { param1: 'value1' } })
|
99
|
+
expect(pref.prereqs).to eq([{ config_section: 'section' }, { config_value: 'value' }])
|
100
|
+
expect(pref.validator_objects[0].class).to eq(CLIUtils::TestValidator)
|
101
|
+
expect(pref.validators).to eq([File.join(base_path, 'test_validator.rb')])
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'runs pre-registered assets' do
|
105
|
+
Readline.stub(:readline).and_return('', 'bachya', '')
|
106
|
+
CLIUtils::Prefs.register_action(File.join(base_path, 'test_action.rb'))
|
107
|
+
CLIUtils::Prefs.register_behavior(File.join(base_path, 'test_behavior.rb'))
|
108
|
+
CLIUtils::Prefs.register_validator(File.join(base_path, 'test_validator.rb'))
|
109
|
+
|
110
|
+
p = CLIUtils::Pref.new(pref_data2)
|
111
|
+
out = capture_stdout { p.deliver }
|
112
|
+
expect(out).to eq("\e[34m# Test pre message\e[0m\nhere\n\e[34m# Test post message\e[0m\nhere\n")
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'runs built-in assets' do
|
116
|
+
Readline.stub(:readline).and_return('', 'bachya')
|
117
|
+
p = CLIUtils::Pref.new(pref_data3)
|
118
|
+
out = capture_stdout { p.deliver }
|
119
|
+
expect(out).to eq("\e[31m# Nil text not allowed\e[0m\n")
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'returns an error when a validator fails' do
|
123
|
+
Readline.stub(:readline).and_return('', 'bachyazzz', 'bachya', '')
|
124
|
+
p = CLIUtils::Pref.new(pref_data)
|
125
|
+
out = capture_stdout { p.deliver }
|
126
|
+
|
127
|
+
m = "\e[34m# Test pre message\e[0m\nhere\n\e[31m# Invalid option chosen " \
|
128
|
+
"(\"bachyazzz\"); valid options are: [\"bachya\"]\e[0m\n\e[34m# Test post " \
|
129
|
+
"message\e[0m\nhere\n"
|
130
|
+
expect(out).to eq(m)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'raises an exception when given an unknown asset' do
|
134
|
+
m = "\e[33m# Skipping undefined Pref Validator: non_nilz\e[0m\n"
|
135
|
+
out = capture_stdout { p = CLIUtils::Pref.new(pref_data4) }
|
136
|
+
expect(out).to eq("\e[33m# Skipping undefined Pref Validator: non_nilz\e[0m\n")
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'compares equality to other Pref instances' do
|
140
|
+
p1 = CLIUtils::Pref.new(pref_data)
|
141
|
+
p2 = CLIUtils::Pref.new(pref_data)
|
142
|
+
expect(p1).to eq(p2)
|
143
|
+
end
|
144
|
+
end
|
data/spec/prefs_spec.rb
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'cliutils/messaging'
|
3
|
+
require 'cliutils/prefs'
|
4
|
+
require 'cliutils/prefs/pref'
|
5
|
+
|
6
|
+
describe CLIUtils::Pref do
|
7
|
+
let(:base_filepath) { File.join(File.dirname(__FILE__), '..', 'support') }
|
8
|
+
let(:prefs_filepath) { File.join(base_filepath, 'prefstest.yaml') }
|
9
|
+
|
10
|
+
let(:hash_data) {
|
11
|
+
{
|
12
|
+
prompts: [
|
13
|
+
{
|
14
|
+
prompt_text: 'What is your name?',
|
15
|
+
default: 'Bob Cobb',
|
16
|
+
config_key: :name,
|
17
|
+
config_section: :personal_info
|
18
|
+
},
|
19
|
+
{
|
20
|
+
prompt_text: 'What is your age?',
|
21
|
+
default: '45',
|
22
|
+
config_key: :age,
|
23
|
+
config_section: :personal_info
|
24
|
+
},
|
25
|
+
{
|
26
|
+
prompt_text: 'Batman or Superman?',
|
27
|
+
default: 'Batman',
|
28
|
+
config_key: :superhero,
|
29
|
+
config_section: :personal_info
|
30
|
+
}
|
31
|
+
]
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
let(:array_data) {
|
36
|
+
[
|
37
|
+
{
|
38
|
+
prompt_text: 'What is your name?',
|
39
|
+
default: 'Bob Cobb',
|
40
|
+
config_key: :name,
|
41
|
+
config_section: :personal_info
|
42
|
+
},
|
43
|
+
{
|
44
|
+
prompt_text: 'What is your age?',
|
45
|
+
default: '45',
|
46
|
+
config_key: :age,
|
47
|
+
config_section: :personal_info,
|
48
|
+
prereqs: {
|
49
|
+
config_key: :name,
|
50
|
+
config_value: 'Bob Cobb'
|
51
|
+
}
|
52
|
+
},
|
53
|
+
{
|
54
|
+
prompt_text: 'Batman or Superman?',
|
55
|
+
default: 'Batman',
|
56
|
+
config_key: :superhero,
|
57
|
+
config_section: :personal_info
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
|
62
|
+
it 'loads prefs from a YAML file' do
|
63
|
+
prefs = CLIUtils::Prefs.new(prefs_filepath)
|
64
|
+
expect(prefs.prompts.count).to eq(4)
|
65
|
+
expect(prefs.prompts[2].prompt_text).to eq('Why do you prefer Superman?!')
|
66
|
+
expect(prefs.prompts[2].config_key).to eq('superman_answer')
|
67
|
+
expect(prefs.prompts[2].config_section).to eq('personal_info')
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raises an exception when attempting to load a nonexistant YAML file' do
|
71
|
+
m = 'Invalid configuration file: bachya'
|
72
|
+
expect { p = CLIUtils::Prefs.new('bachya') }.to raise_error(RuntimeError, m)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'loads prefs from a Hash' do
|
76
|
+
prefs = CLIUtils::Prefs.new(hash_data)
|
77
|
+
expect(prefs.prompts.count).to eq(3)
|
78
|
+
expect(prefs.prompts[1].prompt_text).to eq('What is your age?')
|
79
|
+
expect(prefs.prompts[1].config_key).to eq(:age)
|
80
|
+
expect(prefs.prompts[1].config_section).to eq(:personal_info)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'loads prefs from an Array' do
|
84
|
+
prefs = CLIUtils::Prefs.new(array_data)
|
85
|
+
expect(prefs.prompts.count).to eq(3)
|
86
|
+
expect(prefs.prompts[1].prompt_text).to eq('What is your age?')
|
87
|
+
expect(prefs.prompts[1].config_key).to eq(:age)
|
88
|
+
expect(prefs.prompts[1].config_section).to eq(:personal_info)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'raises an exception when given bad Prefs data' do
|
92
|
+
m = 'Invalid configuration data'
|
93
|
+
expect { p = CLIUtils::Prefs.new(:bachya) }.to raise_error(RuntimeError, m)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'delivers prompts for the user to answer' do
|
97
|
+
Readline.stub(:readline).and_return('')
|
98
|
+
prefs = CLIUtils::Prefs.new(prefs_filepath)
|
99
|
+
prefs.ask
|
100
|
+
|
101
|
+
expect(prefs.prompts[0].answer).to eq('Batman')
|
102
|
+
expect(prefs.prompts[1].answer).to eq('Y')
|
103
|
+
expect(prefs.prompts[2].answer).to eq(nil)
|
104
|
+
expect(prefs.prompts[3].answer).to eq(nil)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'uses Configurator values as part of its prompts' do
|
108
|
+
c = CLIUtils::Configurator.new('/tmp/file1.txt')
|
109
|
+
c.add_section(:personal_info)
|
110
|
+
c.personal_info['superhero'] = 'Superman'
|
111
|
+
|
112
|
+
Readline.stub(:readline).and_return('')
|
113
|
+
prefs = CLIUtils::Prefs.new(prefs_filepath, c)
|
114
|
+
prefs.ask
|
115
|
+
|
116
|
+
expect(prefs.prompts[0].answer).to eq('Batman')
|
117
|
+
expect(prefs.prompts[1].answer).to eq('Y')
|
118
|
+
expect(prefs.prompts[2].answer).to eq(nil)
|
119
|
+
expect(prefs.prompts[3].answer).to eq(nil)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'pre-registers and de-registers Actions' do
|
123
|
+
CLIUtils::Prefs.register_action(File.join(base_filepath, 'test_action.rb'))
|
124
|
+
expect(CLIUtils::Prefs.registered_actions.count).to eq(1)
|
125
|
+
|
126
|
+
CLIUtils::Prefs.deregister_action(:Test)
|
127
|
+
expect(CLIUtils::Prefs.registered_actions.count).to eq(0)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'pre-registers and de-registers Behaviors' do
|
131
|
+
CLIUtils::Prefs.register_behavior(File.join(base_filepath, 'test_behavior.rb'))
|
132
|
+
expect(CLIUtils::Prefs.registered_behaviors.count).to eq(1)
|
133
|
+
|
134
|
+
CLIUtils::Prefs.deregister_behavior(:Test)
|
135
|
+
expect(CLIUtils::Prefs.registered_behaviors.count).to eq(0)
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'pre-registers and de-registers Validators' do
|
139
|
+
CLIUtils::Prefs.register_validator(File.join(base_filepath, 'test_validator.rb'))
|
140
|
+
expect(CLIUtils::Prefs.registered_validators.count).to eq(1)
|
141
|
+
|
142
|
+
CLIUtils::Prefs.deregister_validator(:Test)
|
143
|
+
expect(CLIUtils::Prefs.registered_validators.count).to eq(0)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'raises an exception when registering a bad Action/Behavior/Validator' do
|
147
|
+
m = 'Registration failed because of unknown filepath: bachya'
|
148
|
+
expect { CLIUtils::Prefs.register_validator('bachya') }.to raise_error(RuntimeError, m)
|
149
|
+
end
|
150
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
require 'rspec'
|
3
|
+
# require 'simplecov'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
# SimpleCov.start
|
7
|
+
Coveralls.wear!
|
8
|
+
|
9
|
+
def capture_stdout
|
10
|
+
old = $stdout
|
11
|
+
$stdout = fake = StringIO.new
|
12
|
+
yield
|
13
|
+
fake.string
|
14
|
+
ensure
|
15
|
+
$stdout = old
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'cliutils/messaging'
|
3
|
+
require 'cliutils/prefs/pref_validators/pref_validator'
|
4
|
+
require 'cliutils/prefs/pref_validators/alphabetic_validator'
|
5
|
+
|
6
|
+
describe CLIUtils::AlphabeticValidator do
|
7
|
+
it 'confirms that its input is alphabetic' do
|
8
|
+
v = CLIUtils::AlphabeticValidator.new
|
9
|
+
v.validate('bachya')
|
10
|
+
expect(v.is_valid).to be_true
|
11
|
+
expect(v.message).to eq('Response is not alphabetic: bachya')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'confirms that its input is not alphabetic' do
|
15
|
+
v = CLIUtils::AlphabeticValidator.new
|
16
|
+
v.validate('12345')
|
17
|
+
expect(v.is_valid).to_not be_true
|
18
|
+
expect(v.message).to eq('Response is not alphabetic: 12345')
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'cliutils/messaging'
|
3
|
+
require 'cliutils/prefs/pref_validators/pref_validator'
|
4
|
+
require 'cliutils/prefs/pref_validators/alphanumeric_validator'
|
5
|
+
|
6
|
+
describe CLIUtils::AlphanumericValidator do
|
7
|
+
it 'confirms that its input is alphanumeric' do
|
8
|
+
v = CLIUtils::AlphanumericValidator.new
|
9
|
+
v.validate('bachya91238 ')
|
10
|
+
expect(v.is_valid).to be_true
|
11
|
+
expect(v.message).to eq('Response is not alphanumeric: bachya91238 ')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'confirms that its input is not alphanumeric' do
|
15
|
+
v = CLIUtils::AlphanumericValidator.new
|
16
|
+
v.validate('!@&^')
|
17
|
+
expect(v.is_valid).to_not be_true
|
18
|
+
expect(v.message).to eq('Response is not alphanumeric: !@&^')
|
19
|
+
end
|
20
|
+
end
|