cliutils 2.1.4 → 2.2.0

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 +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +3 -2
  4. data/Gemfile +7 -0
  5. data/HISTORY.md +7 -0
  6. data/README.md +3 -1
  7. data/Rakefile +8 -1
  8. data/cliutils.gemspec +1 -0
  9. data/lib/cliutils/configuration.rb +1 -1
  10. data/lib/cliutils/constants.rb +1 -1
  11. data/lib/cliutils/ext/hash_extensions.rb +16 -12
  12. data/lib/cliutils/messaging.rb +2 -116
  13. data/lib/cliutils/messenger.rb +109 -0
  14. data/lib/cliutils/prefs/pref.rb +10 -10
  15. data/lib/cliutils/prefs/pref_actions/open_url_action.rb +1 -1
  16. data/lib/cliutils/prefs/pref_validators/filepath_exists_validator.rb +2 -0
  17. data/lib/cliutils/prefs/pref_validators/url_validator.rb +1 -1
  18. data/lib/cliutils/prefs.rb +1 -1
  19. data/lib/cliutils/pretty_io.rb +0 -10
  20. data/lib/cliutils.rb +1 -1
  21. data/spec/action/open_url_action_spec.rb +20 -0
  22. data/spec/action/pref_action_spec.rb +11 -0
  23. data/spec/behavior/capitalize_behavior_spec.rb +11 -0
  24. data/spec/behavior/expand_filepath_behavior_spec.rb +11 -0
  25. data/spec/behavior/lowercase_behavior_spec.rb +11 -0
  26. data/spec/behavior/pref_behavior_spec.rb +11 -0
  27. data/spec/behavior/prefix_behavior_spec.rb +12 -0
  28. data/spec/behavior/suffix_behavior_spec.rb +12 -0
  29. data/spec/behavior/titlecase_behavior_spec.rb +10 -0
  30. data/spec/behavior/uppercase_behavior_spec.rb +11 -0
  31. data/spec/configuration_spec.rb +37 -0
  32. data/spec/configurator_spec.rb +104 -0
  33. data/spec/ext/hash_extensions_spec.rb +54 -0
  34. data/spec/ext/logger_extensions_spec.rb +20 -0
  35. data/spec/ext/string_extensions_spec.rb +26 -0
  36. data/spec/messaging_spec.rb +91 -0
  37. data/spec/pref_spec.rb +144 -0
  38. data/spec/prefs_spec.rb +150 -0
  39. data/spec/spec_helper.rb +16 -0
  40. data/spec/validator/alphabetic_validator_spec.rb +20 -0
  41. data/spec/validator/alphanumeric_validator_spec.rb +20 -0
  42. data/spec/validator/date_validator_spec.rb +20 -0
  43. data/spec/validator/datetime_validator_spec.rb +20 -0
  44. data/spec/validator/filepath_exists_validator_spec.rb +20 -0
  45. data/spec/validator/non_nil_validator_spec.rb +24 -0
  46. data/spec/validator/number_validator_spec.rb +20 -0
  47. data/spec/validator/pref_validator_spec.rb +11 -0
  48. data/spec/validator/time_validator_spec.rb +20 -0
  49. data/spec/validator/url_validator_spec.rb +20 -0
  50. data/{test/test_files → support}/configuration.yaml +1 -1
  51. data/support/prefstest.yaml +27 -0
  52. data/{test/test_files → support}/test_action.rb +0 -0
  53. data/support/test_action_empty.rb +7 -0
  54. data/{test/test_files → support}/test_behavior.rb +2 -2
  55. data/support/test_behavior_empty.rb +6 -0
  56. data/{test/test_files → support}/test_validator.rb +0 -2
  57. data/support/test_validator_empty.rb +7 -0
  58. data/test/pref_test.rb +0 -1
  59. data/test/prefs_test.rb +64 -1
  60. data/test/test_helper.rb +2 -2
  61. metadata +91 -67
  62. data/lib/cliutils/logger_delegator.rb +0 -49
  63. data/test/action_tests/open_url_action_test.rb +0 -12
  64. data/test/behavior_tests/capitalize_behavior_test.rb +0 -11
  65. data/test/behavior_tests/expand_filepath_behavior_test.rb +0 -11
  66. data/test/behavior_tests/lowercase_behavior_test.rb +0 -11
  67. data/test/behavior_tests/prefix_behavior_test.rb +0 -12
  68. data/test/behavior_tests/suffix_behavior_test.rb +0 -12
  69. data/test/behavior_tests/titlecase_behavior_test.rb +0 -11
  70. data/test/behavior_tests/uppercase_behavior_test.rb +0 -11
  71. data/test/configuration_test.rb +0 -49
  72. data/test/configurator_test.rb +0 -63
  73. data/test/hash_extensions_test.rb +0 -51
  74. data/test/logger_extensions_test.rb +0 -17
  75. data/test/messaging_test.rb +0 -53
  76. data/test/string_extesions_test.rb +0 -28
  77. data/test/test_files/prefstest.yaml +0 -38
  78. data/test/validator_tests/alphabetic_validator_test.rb +0 -22
  79. data/test/validator_tests/alphanumeric_validator_test.rb +0 -22
  80. data/test/validator_tests/date_validator_test.rb +0 -22
  81. data/test/validator_tests/datetime_validator_test.rb +0 -22
  82. data/test/validator_tests/filepath_exists_validator_test.rb +0 -22
  83. data/test/validator_tests/non_nil_validator_test.rb +0 -30
  84. data/test/validator_tests/number_validator_test.rb +0 -22
  85. data/test/validator_tests/time_validator_test.rb +0 -22
  86. data/test/validator_tests/url_validator_test.rb +0 -22
@@ -1,49 +0,0 @@
1
- module CLIUtils
2
- # LoggerDelegator Class
3
- # Delegates certain Logger methods to a number of different
4
- # targets.
5
- class LoggerDelegator
6
- # The endpoints to which delegation occurs.
7
- # @return [Array]
8
- attr_reader :targets
9
-
10
- # Initializes and creates methods for the passed targets.
11
- # @param [Logger] targets The endpoints to delegate to
12
- # @return [void]
13
- def initialize(targets)
14
- @targets = targets
15
- LoggerDelegator.delegate
16
- end
17
-
18
- # Attaches a new target to delegate to.
19
- # @param [Hash] target A hash describing a reference key and a Logger
20
- # @return [void]
21
- def attach(target)
22
- fail "Cannot add invalid target: #{ target }" unless target.is_a?(Hash)
23
- @targets.merge!(target)
24
- LoggerDelegator.delegate
25
- end
26
-
27
- # Creates delegator methods for a specific list of Logger
28
- # functions.
29
- # @return [void]
30
- def self.delegate
31
- %w(log debug info warn error section success).each do |m|
32
- define_method(m) do |*args|
33
- @targets.each_value { |v| v.send(m, *args) }
34
- end
35
- end
36
- end
37
-
38
- # Detaches a delegation target.
39
- # @param [<String, Symbol>] target_name The target to remove
40
- # @return [void]
41
- def detach(target_name)
42
- unless @targets.key?(target_name)
43
- fail "Cannot delete invalid target: #{ target_name }"
44
- end
45
- @targets.delete(target_name)
46
- LoggerDelegator.delegate
47
- end
48
- end
49
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_actions/open_url_action')
4
-
5
- # Tests for the Configurator class
6
- class TestOpenUrlAction < Test::Unit::TestCase
7
- def test_run
8
- a = CLIUtils::OpenUrlAction.new
9
- a.parameters = { url: 'http://www.google.com' }
10
- assert_output() { a.run }
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/capitalize_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestCapitalizeBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::CapitalizeBehavior.new
9
- assert_equal(v.evaluate('bachya'), 'Bachya')
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/expand_filepath_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestExpandFilepathBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::ExpandFilepathBehavior.new
9
- assert_equal(v.evaluate('~/test'), "#{ ENV['HOME'] }/test")
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/lowercase_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestLowercaseBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::LowercaseBehavior.new
9
- assert_equal(v.evaluate('BaChYa'), 'bachya')
10
- end
11
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/prefix_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestPrefixBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::PrefixBehavior.new
9
- v.parameters = { prefix: 'test: ' }
10
- assert_equal(v.evaluate('bachya'), 'test: bachya')
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/suffix_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestSuffixBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::SuffixBehavior.new
9
- v.parameters = { suffix: ' - signing off!' }
10
- assert_equal(v.evaluate('bachya'), 'bachya - signing off!')
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/titlecase_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestTitlecaseBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::TitlecaseBehavior.new
9
- assert_equal(v.evaluate('my sentence is here'), 'My Sentence Is Here')
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', '..', 'lib/cliutils/prefs/pref_behaviors/uppercase_behavior')
4
-
5
- # Tests for the Configurator class
6
- class TestUppercaseBehavior < Test::Unit::TestCase
7
- def test_evaluation
8
- v = CLIUtils::UppercaseBehavior.new
9
- assert_equal(v.evaluate('bachya'), 'BACHYA')
10
- end
11
- end
@@ -1,49 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/configurator')
4
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/configuration')
5
-
6
- # Tests for the Configurator class
7
- class TestConfiguration < Test::Unit::TestCase
8
- include CLIUtils::Configuration
9
-
10
- def setup
11
- @config_path = '/tmp/test.config'
12
- @expected_config_data = {
13
- my_app: {
14
- config_location: '/Users/bob/.my-app-config',
15
- log_level: 'WARN',
16
- version: '1.0.0'
17
- },
18
- user_data: {
19
- username: 'bob',
20
- age: 45
21
- }
22
- }
23
- end
24
-
25
- def teardown
26
- FileUtils.rm(@config_path) if File.file?(@config_path)
27
- end
28
-
29
- def test_before_loading
30
- assert_raise RuntimeError do
31
- configuration
32
- end
33
- end
34
-
35
- def test_empty_configuration
36
- load_configuration(@config_path)
37
- assert_equal(configuration.class, CLIUtils::Configurator)
38
- assert_equal(configuration.config_path, @config_path)
39
- assert_equal(configuration.data, {})
40
- end
41
-
42
- def test_existing_configuration
43
- FileUtils.cp(File.join(File.dirname(__FILE__), '..', 'test/test_files/configuration.yaml'), @config_path)
44
- load_configuration(@config_path)
45
- assert_equal(configuration.class, CLIUtils::Configurator)
46
- assert_equal(configuration.config_path, @config_path)
47
- assert_equal(configuration.data, @expected_config_data)
48
- end
49
- end
@@ -1,63 +0,0 @@
1
- require 'fileutils'
2
- require 'test_helper'
3
-
4
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/configurator')
5
-
6
- # Tests for the Configurator class
7
- class TestConfigurator < Test::Unit::TestCase
8
- def setup
9
- @config_path = '/tmp/test.config'
10
- @config = CLIUtils::Configurator.new(@config_path)
11
- end
12
-
13
- def teardown
14
- FileUtils.rm(@config_path) if File.file?(@config_path)
15
- end
16
-
17
- def test_add_section
18
- @config.add_section(:test)
19
- assert_equal(@config.data, test: {})
20
- end
21
-
22
- def test_delete_section
23
- @config.add_section(:test)
24
- @config.add_section(:test2)
25
- @config.delete_section(:test)
26
- assert_equal(@config.data, test2: {})
27
- end
28
-
29
- def test_accessing
30
- @config.add_section(:test)
31
- @config.data[:test].merge!(name: 'Bob')
32
- assert_equal(@config.test, name: 'Bob')
33
- end
34
-
35
- def test_reset
36
- @config.add_section(:test)
37
- @config.data[:test].merge!(name: 'Bob')
38
- @config.reset
39
- assert_equal(@config.data, {})
40
- end
41
-
42
- def test_save
43
- @config.add_section(:section1)
44
- @config.section1.merge!(a: 'test', b: 'test')
45
- @config.save
46
-
47
- File.open(@config_path, 'r') do |f|
48
- assert_output("---\nsection1:\n a: test\n b: test\n") { puts f.read }
49
- end
50
- end
51
-
52
- def test_compare_version
53
- @config.add_section(:app_data)
54
- @config.app_data.merge!({ VERSION: '1.0.0' })
55
-
56
- @config.current_version = @config.app_data['VERSION']
57
- @config.last_version = '1.0.8'
58
-
59
- @config.compare_version do |c, l|
60
- assert_output('true') { print 'true' }
61
- end
62
- end
63
- end
@@ -1,51 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/hash_extensions')
4
-
5
- # Tests for the Hash extension methods
6
- class TestHashExtensions < Test::Unit::TestCase
7
- def test_deep_merge!
8
- h1 = { key: 'value', key2: %w(value1, value2) }
9
- h2 = { key: 'another_value' }
10
- exp_result = { key: 'another_value', key2: %w(value1, value2) }
11
- actual_result = h1.deep_merge!(h2)
12
-
13
- assert_equal(exp_result, actual_result)
14
- end
15
-
16
- def test_deep_stringify_keys
17
- h = { key: { subkey1: 'value1', subkey2: { subsubkey1: 'value' } } }
18
- exp_result = { 'key' => { 'subkey1' => 'value1', 'subkey2' => { 'subsubkey1' => 'value' } } }
19
- actual_result = h.deep_stringify_keys
20
-
21
- assert_not_equal(h, actual_result)
22
- assert_equal(exp_result, actual_result)
23
- end
24
-
25
- def test_deep_stringify_keys!
26
- h = {key: {subkey1: 'value1', subkey2: {subsubkey1: 'value'}}}
27
- exp_result = { 'key' => { 'subkey1' => 'value1', 'subkey2' => { 'subsubkey1' => 'value' } } }
28
- actual_result = h.deep_stringify_keys!
29
-
30
- assert_equal(h, actual_result)
31
- assert_equal(exp_result, actual_result)
32
- end
33
-
34
- def test_deep_symbolize_keys
35
- h = { 'key' => { 'subkey1' => 'value1', 'subkey2' => { 'subsubkey1' => 'value'} } }
36
- exp_result = { key: { subkey1: 'value1', subkey2: { subsubkey1: 'value'} } }
37
- actual_result = h.deep_symbolize_keys
38
-
39
- assert_not_equal(h, actual_result)
40
- assert_equal(exp_result, actual_result)
41
- end
42
-
43
- def test_deep_symbolize_keys!
44
- h = { 'key' => { 'subkey1' => 'value1', 'subkey2' => { 'subsubkey1' => 'value'} } }
45
- exp_result = { key: { subkey1: 'value1', subkey2: { subsubkey1: 'value'} } }
46
- actual_result = h.deep_symbolize_keys!
47
-
48
- assert_equal(h, actual_result)
49
- assert_equal(exp_result, actual_result)
50
- end
51
- end
@@ -1,17 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/logger_extensions')
4
-
5
- # Tests for the Logger extension methods
6
- class TestLoggerExtensions < Test::Unit::TestCase
7
- def test_custom_level
8
- l = Logger.new(STDOUT)
9
- l.formatter = proc do |severity, datetime, progname, msg|
10
- puts "#{ severity }: #{ msg }"
11
- end
12
-
13
- assert_output("PROMPT: test\n") { l.prompt('test') }
14
- assert_output("SECTION: test\n") { l.section('test') }
15
- assert_output("SUCCESS: test\n") { l.success('test') }
16
- end
17
- end
@@ -1,53 +0,0 @@
1
- require 'logger'
2
- require 'test_helper'
3
-
4
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/string_extensions')
5
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/pretty_io')
6
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/logger_delegator')
7
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/messaging')
8
-
9
- # Tests for the Hash extension methods
10
- class TestMessaging < Test::Unit::TestCase
11
- include CLIUtils::Messaging
12
-
13
- def setup
14
- @file1path = '/tmp/file1.txt'
15
- end
16
-
17
- def teardown
18
- FileUtils.rm(@file1path) if File.file?(@file1path)
19
- end
20
-
21
- def test_stdout_output
22
- assert_output('# This is error'.red + "\n") { messenger.send(:error, 'This is error') }
23
- assert_output('# This is info'.blue + "\n") { messenger.send(:info, 'This is info') }
24
- assert_output('---> This is section'.purple + "\n") { messenger.send(:section, 'This is section') }
25
- assert_output('# This is success'.green + "\n") { messenger.send(:success, 'This is success') }
26
- assert_output('# This is warn'.yellow + "\n") { messenger.send(:warn, 'This is warn') }
27
- end
28
-
29
- def test_wrapping
30
- CLIUtils::PrettyIO.wrap_char_limit = 35
31
-
32
- long_str = 'This is a really long string that should wrap itself at some point, okay?'
33
- expected_str = long_str.gsub(/\n/, ' ').gsub(/(.{1,#{CLIUtils::PrettyIO.wrap_char_limit - 2}})(\s+|$)/, "# \\1\n").strip
34
- assert_output(expected_str.blue + "\n") { messenger.send(:info, long_str) }
35
- end
36
-
37
- def test_attach_detach
38
- file_logger = Logger.new(@file1path)
39
- file_logger.formatter = proc do |severity, datetime, progname, msg|
40
- "#{ severity }: #{ msg }\n"
41
- end
42
-
43
- messenger.attach(FILE: file_logger)
44
- messenger.send(:info, 'Info test')
45
- messenger.send(:error, 'Error test')
46
- messenger.detach(:FILE)
47
- messenger.send(:warn, 'Warn test')
48
-
49
- File.open(@file1path, 'r') do |f|
50
- assert_output("INFO: Info test\nERROR: Error test\n") { puts f.read.lines.to_a[1..-1].join }
51
- end
52
- end
53
- end
@@ -1,28 +0,0 @@
1
- require 'test_helper'
2
-
3
- require File.join(File.dirname(__FILE__), '..', 'lib/cliutils/ext/string_extensions')
4
-
5
- # Tests for the String extension methods
6
- class TestStringExtensions < Test::Unit::TestCase
7
- def test_custom_colors
8
- assert_output("\e[34mtest\e[0m") { print 'test'.blue }
9
- assert_output("\e[36mtest\e[0m") { print 'test'.cyan }
10
- assert_output("\e[32mtest\e[0m") { print 'test'.green }
11
- assert_output("\e[35mtest\e[0m") { print 'test'.purple }
12
- assert_output("\e[31mtest\e[0m") { print 'test'.red }
13
- assert_output("\e[37mtest\e[0m") { print 'test'.white }
14
- assert_output("\e[33mtest\e[0m") { print 'test'.yellow }
15
- end
16
-
17
- def test_colorize
18
- assert_output("\e[35;42mtest\e[0m") { print 'test'.colorize('35;42') }
19
- end
20
-
21
- def test_camelize
22
- assert_output('TestString') { print 'test_string'.camelize }
23
- end
24
-
25
- def test_snakify
26
- assert_output('test_string') { print 'TestString'.snakify }
27
- end
28
- end
@@ -1,38 +0,0 @@
1
- prompts:
2
- - prompt_text: This is a test prompt
3
- config_key: test_prompt
4
- config_section: app_data
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!'
17
- validators:
18
- - alphabetic
19
- # - alphanumeric
20
- # - date
21
- # - datetime
22
- # - filepath_exists
23
- # - non_nil
24
- # - number
25
- # - time
26
- # - url
27
- behaviors:
28
- - name: capitalize
29
- # - name: expand_filepath
30
- # - name: lowercase
31
- # - name: prefix
32
- # parameters:
33
- # prefix: 'back'
34
- # - name: suffix
35
- # parameters:
36
- # suffix: 'test'
37
- # - name: titlecase
38
- # - name: uppercase