opt_parse_validator 0.0.2

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +4 -0
  5. data/.simplecov +4 -0
  6. data/.travis.yml +15 -0
  7. data/Gemfile +6 -0
  8. data/README.md +40 -0
  9. data/Rakefile +9 -0
  10. data/lib/opt_parse_validator/hacks.rb +15 -0
  11. data/lib/opt_parse_validator/options_file.rb +62 -0
  12. data/lib/opt_parse_validator/opts/base.rb +74 -0
  13. data/lib/opt_parse_validator/opts/boolean.rb +20 -0
  14. data/lib/opt_parse_validator/opts/choice.rb +33 -0
  15. data/lib/opt_parse_validator/opts/credentials.rb +14 -0
  16. data/lib/opt_parse_validator/opts/directory_path.rb +10 -0
  17. data/lib/opt_parse_validator/opts/file_path.rb +24 -0
  18. data/lib/opt_parse_validator/opts/integer.rb +12 -0
  19. data/lib/opt_parse_validator/opts/path.rb +61 -0
  20. data/lib/opt_parse_validator/opts/positive_integer.rb +13 -0
  21. data/lib/opt_parse_validator/opts/proxy.rb +5 -0
  22. data/lib/opt_parse_validator/opts/string.rb +6 -0
  23. data/lib/opt_parse_validator/opts/uri.rb +24 -0
  24. data/lib/opt_parse_validator/opts/url.rb +9 -0
  25. data/lib/opt_parse_validator/opts.rb +3 -0
  26. data/lib/opt_parse_validator/version.rb +4 -0
  27. data/lib/opt_parse_validator.rb +77 -0
  28. data/opt_parse_validator.gemspec +32 -0
  29. data/spec/fixtures/options_file/default.json +4 -0
  30. data/spec/fixtures/options_file/malformed.json +4 -0
  31. data/spec/fixtures/options_file/override.yml +1 -0
  32. data/spec/fixtures/options_file/unsupported.ext +1 -0
  33. data/spec/fixtures/r.txt +1 -0
  34. data/spec/fixtures/rwx.txt +1 -0
  35. data/spec/lib/opt_parse_validator/options_file_spec.rb +50 -0
  36. data/spec/lib/opt_parse_validator/opts/base_spec.rb +169 -0
  37. data/spec/lib/opt_parse_validator/opts/boolean_spec.rb +36 -0
  38. data/spec/lib/opt_parse_validator/opts/choice_spec.rb +78 -0
  39. data/spec/lib/opt_parse_validator/opts/credentials_spec.rb +23 -0
  40. data/spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb +25 -0
  41. data/spec/lib/opt_parse_validator/opts/file_path_spec.rb +93 -0
  42. data/spec/lib/opt_parse_validator/opts/integer_spec.rb +19 -0
  43. data/spec/lib/opt_parse_validator/opts/path_spec.rb +7 -0
  44. data/spec/lib/opt_parse_validator/opts/positive_integer_spec.rb +19 -0
  45. data/spec/lib/opt_parse_validator/opts/proxy_spec.rb +12 -0
  46. data/spec/lib/opt_parse_validator/opts/uri_spec.rb +58 -0
  47. data/spec/lib/opt_parse_validator/opts/url_spec.rb +28 -0
  48. data/spec/lib/opt_parse_validator/version_spec.rb +7 -0
  49. data/spec/lib/opt_parse_validator_spec.rb +152 -0
  50. data/spec/spec_helper.rb +25 -0
  51. metadata +213 -0
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ describe OptParseValidator::OptParser do
4
+
5
+ subject(:parser) { described_class.new }
6
+ let(:verbose_opt) { OptParseValidator::OptBase.new(%w(-v --verbose)) }
7
+ let(:url_opt) { OptParseValidator::OptURL.new(['-u', '--url URL'], required: true) }
8
+
9
+ describe '#add_option' do
10
+ after do
11
+ if @exception
12
+ expect { parser.add_option(@option) }.to raise_error(@exception)
13
+ else
14
+ parser.add_option(@option)
15
+
16
+ expect(parser.symbols_used).to eq @expected_symbols
17
+ end
18
+ end
19
+
20
+ context 'when not an OptBase' do
21
+ it 'raises an error' do
22
+ @option = 'just a string'
23
+ @exception = 'The option is not an OptBase, String supplied'
24
+ end
25
+ end
26
+
27
+ context 'when the option symbol is already used' do
28
+ it 'raises an error' do
29
+ @option = verbose_opt
30
+ @exception = 'The option verbose is already used !'
31
+ parser.add_option(@option)
32
+ end
33
+ end
34
+
35
+ context 'when a valid option' do
36
+ let(:option) { OptParseValidator::OptBase.new(['-u', '--url URL']) }
37
+
38
+ it 'sets the option' do
39
+ @option = option
40
+ @expected_symbols = [:url]
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#add' do
46
+ context 'when not an Array<OptBase> or an OptBase' do
47
+ after { expect { parser.add(*@options) }.to raise_error(@exception) }
48
+
49
+ it 'raises an error when an Array<String>' do
50
+ @options = ['string', 'another one']
51
+ @exception = 'The option is not an OptBase, String supplied'
52
+ end
53
+ end
54
+
55
+ context 'when valid' do
56
+ after do
57
+ parser.add(*@options)
58
+
59
+ expect(parser.symbols_used).to eq @expected_symbols
60
+ end
61
+
62
+ it 'adds the options' do
63
+ @options = [verbose_opt, url_opt]
64
+ @expected_symbols = [:verbose, :url]
65
+ end
66
+
67
+ it 'adds the option' do
68
+ @options = verbose_opt
69
+ @expected_symbols = [:verbose]
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#results' do
75
+ before { parser.add(*options) }
76
+
77
+ after do
78
+ if @expected
79
+ expect(parser.results(@argv)).to eq @expected
80
+ else
81
+ expect { parser.results(@argv) }.to raise_error(@exception)
82
+ end
83
+ end
84
+
85
+ let(:options) { [verbose_opt, url_opt] }
86
+
87
+ context 'when an option is required but not supplied' do
88
+ it 'raises an error' do
89
+ @exception = 'The option url is required'
90
+ @argv = %w(-v)
91
+ end
92
+ end
93
+
94
+ context 'when the #validate raises an error' do
95
+ it 'adds the option.to_long as a prefix' do
96
+ @exception = Addressable::URI::InvalidURIError
97
+ @argv = %w(--url www.google.com)
98
+ end
99
+ end
100
+
101
+ context 'when the default attribute is used' do
102
+ let(:options) { [verbose_opt, default_opt] }
103
+ let(:default_opt) { OptParseValidator::OptBase.new(['--default VALUE'], default: false) }
104
+
105
+ context 'when the option is supplied' do
106
+ it 'overrides the default value' do
107
+ @argv = %w(--default overriden)
108
+ @expected = { default: 'overriden' }
109
+ end
110
+ end
111
+
112
+ context 'when the option is not supplied' do
113
+ it 'sets the default value' do
114
+ @argv = %w(-v)
115
+ @expected = { verbose: true, default: false }
116
+ end
117
+ end
118
+ end
119
+
120
+ # See https://github.com/wpscanteam/CMSScanner/issues/2
121
+ context 'when no short option' do
122
+ let(:options) { [verbose_opt, http_opt] }
123
+ let(:http_opt) { OptParseValidator::OptBase.new(['--http-auth log:pass']) }
124
+
125
+ it 'calls the help' do
126
+ expect(parser).to receive(:help)
127
+
128
+ @argv = %w(-h)
129
+ @exception = SystemExit
130
+ end
131
+
132
+ it 'returns the results' do
133
+ @argv = %w(--http-auth user:passwd)
134
+ @expected = { http_auth: 'user:passwd' }
135
+ end
136
+ end
137
+
138
+ context 'when the normalize attrbite is used' do
139
+ let(:options) { [OptParseValidator::OptString.new(['--test V'], normalize: :to_sym)] }
140
+
141
+ it 'returns the symbol' do
142
+ @argv = %w(--test test)
143
+ @expected = { test: :test }
144
+ end
145
+ end
146
+
147
+ it 'returns the results' do
148
+ @argv = %w(--url http://hello.com -v)
149
+ @expected = { url: 'http://hello.com', verbose: true }
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ FIXTURES = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
5
+
6
+ require 'rspec/its'
7
+ require 'simplecov'
8
+
9
+ if ENV['TRAVIS']
10
+ require 'coveralls'
11
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
12
+ end
13
+
14
+ SimpleCov.start do
15
+ add_filter '/spec/'
16
+ end
17
+
18
+ # See http://betterspecs.org/
19
+ RSpec.configure do |config|
20
+ config.expect_with :rspec do |c|
21
+ c.syntax = :expect
22
+ end
23
+ end
24
+
25
+ require 'opt_parse_validator'
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opt_parse_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - WPScanTeam - Erwan le Rousseau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.26'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.26'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Testing Gem ...
112
+ email:
113
+ - erwan.lr@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".simplecov"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - README.md
125
+ - Rakefile
126
+ - lib/opt_parse_validator.rb
127
+ - lib/opt_parse_validator/hacks.rb
128
+ - lib/opt_parse_validator/options_file.rb
129
+ - lib/opt_parse_validator/opts.rb
130
+ - lib/opt_parse_validator/opts/base.rb
131
+ - lib/opt_parse_validator/opts/boolean.rb
132
+ - lib/opt_parse_validator/opts/choice.rb
133
+ - lib/opt_parse_validator/opts/credentials.rb
134
+ - lib/opt_parse_validator/opts/directory_path.rb
135
+ - lib/opt_parse_validator/opts/file_path.rb
136
+ - lib/opt_parse_validator/opts/integer.rb
137
+ - lib/opt_parse_validator/opts/path.rb
138
+ - lib/opt_parse_validator/opts/positive_integer.rb
139
+ - lib/opt_parse_validator/opts/proxy.rb
140
+ - lib/opt_parse_validator/opts/string.rb
141
+ - lib/opt_parse_validator/opts/uri.rb
142
+ - lib/opt_parse_validator/opts/url.rb
143
+ - lib/opt_parse_validator/version.rb
144
+ - opt_parse_validator.gemspec
145
+ - spec/fixtures/options_file/default.json
146
+ - spec/fixtures/options_file/malformed.json
147
+ - spec/fixtures/options_file/override.yml
148
+ - spec/fixtures/options_file/unsupported.ext
149
+ - spec/fixtures/r.txt
150
+ - spec/fixtures/rwx.txt
151
+ - spec/lib/opt_parse_validator/options_file_spec.rb
152
+ - spec/lib/opt_parse_validator/opts/base_spec.rb
153
+ - spec/lib/opt_parse_validator/opts/boolean_spec.rb
154
+ - spec/lib/opt_parse_validator/opts/choice_spec.rb
155
+ - spec/lib/opt_parse_validator/opts/credentials_spec.rb
156
+ - spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb
157
+ - spec/lib/opt_parse_validator/opts/file_path_spec.rb
158
+ - spec/lib/opt_parse_validator/opts/integer_spec.rb
159
+ - spec/lib/opt_parse_validator/opts/path_spec.rb
160
+ - spec/lib/opt_parse_validator/opts/positive_integer_spec.rb
161
+ - spec/lib/opt_parse_validator/opts/proxy_spec.rb
162
+ - spec/lib/opt_parse_validator/opts/uri_spec.rb
163
+ - spec/lib/opt_parse_validator/opts/url_spec.rb
164
+ - spec/lib/opt_parse_validator/version_spec.rb
165
+ - spec/lib/opt_parse_validator_spec.rb
166
+ - spec/spec_helper.rb
167
+ homepage: https://github.com/wpscanteam/OptParseValidator
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: 2.0.0
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.2.2
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Testing Gem
191
+ test_files:
192
+ - spec/fixtures/options_file/default.json
193
+ - spec/fixtures/options_file/malformed.json
194
+ - spec/fixtures/options_file/override.yml
195
+ - spec/fixtures/options_file/unsupported.ext
196
+ - spec/fixtures/r.txt
197
+ - spec/fixtures/rwx.txt
198
+ - spec/lib/opt_parse_validator/options_file_spec.rb
199
+ - spec/lib/opt_parse_validator/opts/base_spec.rb
200
+ - spec/lib/opt_parse_validator/opts/boolean_spec.rb
201
+ - spec/lib/opt_parse_validator/opts/choice_spec.rb
202
+ - spec/lib/opt_parse_validator/opts/credentials_spec.rb
203
+ - spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb
204
+ - spec/lib/opt_parse_validator/opts/file_path_spec.rb
205
+ - spec/lib/opt_parse_validator/opts/integer_spec.rb
206
+ - spec/lib/opt_parse_validator/opts/path_spec.rb
207
+ - spec/lib/opt_parse_validator/opts/positive_integer_spec.rb
208
+ - spec/lib/opt_parse_validator/opts/proxy_spec.rb
209
+ - spec/lib/opt_parse_validator/opts/uri_spec.rb
210
+ - spec/lib/opt_parse_validator/opts/url_spec.rb
211
+ - spec/lib/opt_parse_validator/version_spec.rb
212
+ - spec/lib/opt_parse_validator_spec.rb
213
+ - spec/spec_helper.rb