opt_parse_validator 0.0.9 → 0.0.10

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/lib/opt_parse_validator/opts/regexp.rb +12 -0
  4. data/lib/opt_parse_validator/opts.rb +1 -1
  5. data/lib/opt_parse_validator/version.rb +1 -1
  6. data/lib/opt_parse_validator.rb +1 -1
  7. data/opt_parse_validator.gemspec +14 -4
  8. metadata +7 -62
  9. data/.gitignore +0 -4
  10. data/.rspec +0 -2
  11. data/.rubocop.yml +0 -6
  12. data/.travis.yml +0 -19
  13. data/Gemfile +0 -6
  14. data/Rakefile +0 -9
  15. data/spec/fixtures/options_file/default.json +0 -4
  16. data/spec/fixtures/options_file/malformed.json +0 -4
  17. data/spec/fixtures/options_file/override.yml +0 -1
  18. data/spec/fixtures/options_file/unsupported.ext +0 -1
  19. data/spec/fixtures/r.txt +0 -1
  20. data/spec/fixtures/rwx.txt +0 -1
  21. data/spec/lib/opt_parse_validator/options_file_spec.rb +0 -48
  22. data/spec/lib/opt_parse_validator/opts/array_spec.rb +0 -74
  23. data/spec/lib/opt_parse_validator/opts/base_spec.rb +0 -188
  24. data/spec/lib/opt_parse_validator/opts/boolean_spec.rb +0 -34
  25. data/spec/lib/opt_parse_validator/opts/choice_spec.rb +0 -78
  26. data/spec/lib/opt_parse_validator/opts/credentials_spec.rb +0 -21
  27. data/spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb +0 -23
  28. data/spec/lib/opt_parse_validator/opts/file_path_spec.rb +0 -91
  29. data/spec/lib/opt_parse_validator/opts/integer_range_spec.rb +0 -54
  30. data/spec/lib/opt_parse_validator/opts/integer_spec.rb +0 -17
  31. data/spec/lib/opt_parse_validator/opts/multi_choices_spec.rb +0 -105
  32. data/spec/lib/opt_parse_validator/opts/path_spec.rb +0 -5
  33. data/spec/lib/opt_parse_validator/opts/positive_integer_spec.rb +0 -17
  34. data/spec/lib/opt_parse_validator/opts/proxy_spec.rb +0 -10
  35. data/spec/lib/opt_parse_validator/opts/uri_spec.rb +0 -73
  36. data/spec/lib/opt_parse_validator/opts/url_spec.rb +0 -26
  37. data/spec/lib/opt_parse_validator/version_spec.rb +0 -7
  38. data/spec/lib/opt_parse_validator_spec.rb +0 -185
  39. data/spec/spec_helper.rb +0 -25
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptChoice do
4
- subject(:opt) { described_class.new(option, attrs) }
5
- let(:option) { %w(-f --format FORMAT) }
6
- let(:attrs) { { choices: %w(json cli) } }
7
-
8
- describe '#new' do
9
- context 'when errors' do
10
- after { expect { opt }.to raise_error(@exception) }
11
-
12
- context 'when :choices not provided' do
13
- let(:attrs) { {} }
14
-
15
- it 'raises an error' do
16
- @exception = 'The :choices attribute is mandatory'
17
- end
18
- end
19
-
20
- context 'when :choices is not an array' do
21
- let(:attrs) { { choices: 'wrong type' } }
22
-
23
- it 'raises an error' do
24
- @exception = 'The :choices attribute must be an array'
25
- end
26
- end
27
- end
28
-
29
- context 'when valid' do
30
- it 'sets the option correctly' do
31
- expect { opt }.to_not raise_error
32
- expect(opt.attrs[:choices]).to eq attrs[:choices]
33
- end
34
- end
35
- end
36
-
37
- describe '#validate' do
38
- after :each do
39
- if @exception
40
- expect { opt.validate(@value) }.to raise_error(@exception)
41
- else
42
- expect(opt.validate(@value)).to eq(@expected)
43
- end
44
- end
45
-
46
- context 'when the value is not in the choices' do
47
- it 'raises an error' do
48
- @value = 'invalid-format'
49
- @exception = "'invalid-format' is not a valid choice, expected one of the followings: json,cli"
50
- end
51
-
52
- context 'when :case_sensitive' do
53
- let(:attrs) { { choices: %w(json cli), case_sensitive: true } }
54
-
55
- it 'raises an error' do
56
- @value = 'JSON'
57
- @exception = "'JSON' is not a valid choice, expected one of the followings: json,cli"
58
- end
59
- end
60
- end
61
-
62
- context 'when valid choice' do
63
- it 'returns the choice' do
64
- @value = 'JSON'
65
- @expected = 'json'
66
- end
67
-
68
- context 'when :case_sensitive' do
69
- let(:attrs) { { choices: %w(JSON cli), case_sensitive: true } }
70
-
71
- it 'raises an error' do
72
- @value = 'JSON'
73
- @expected = 'JSON'
74
- end
75
- end
76
- end
77
- end
78
- end
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptCredentials do
4
- subject(:opt) { described_class.new(['-l', '--login USERNAME:PASSWORD']) }
5
-
6
- describe '#validate' do
7
- context 'when incorrect format' do
8
- it 'raises an error' do
9
- expect { opt.validate('wrong') }
10
- .to raise_error 'Incorrect credentials format, username:password expected'
11
- end
12
- end
13
-
14
- context 'when valid format' do
15
- it 'returns a hash with :username and :password' do
16
- expect(opt.validate('admin:P@ssw:rd'))
17
- .to eq(username: 'admin', password: 'P@ssw:rd')
18
- end
19
- end
20
- end
21
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptDirectoryPath do
4
- subject(:opt) { described_class.new(['-d', '--dir DIR'], attrs) }
5
- let(:attrs) { {} }
6
- let(:dir) { File.join(FIXTURES, 'options_file') }
7
-
8
- its(:attrs) { should eq directory: true }
9
-
10
- describe '#validate' do
11
- context 'when it is a directory' do
12
- it 'returns the path' do
13
- expect(opt.validate(dir)).to eq dir
14
- end
15
- end
16
-
17
- context 'when it\s not ' do
18
- it 'raises an error' do
19
- expect { opt.validate('yolo.txt') }.to raise_error "'yolo.txt' is not a directory"
20
- end
21
- end
22
- end
23
- end
@@ -1,91 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptFilePath do
4
- subject(:opt) { described_class.new(['-f', '--file FILE_PATH'], attrs) }
5
- let(:attrs) { {} }
6
- let(:rwx_file) { File.join(FIXTURES, 'rwx.txt') }
7
- let(:r_file) { File.join(FIXTURES, 'r.txt') }
8
-
9
- its(:attrs) { should eq file: true }
10
-
11
- describe '#validate' do
12
- context 'when :extensions' do
13
- let(:attrs) { { extensions: 'txt' } }
14
-
15
- its('allowed_attrs.first') { should eq :extensions }
16
-
17
- context 'when it matches' do
18
- it 'returns the path' do
19
- expect(opt.validate(rwx_file)).to eq rwx_file
20
- end
21
- end
22
-
23
- context 'when it does no match' do
24
- it 'raises an error' do
25
- expect { opt.validate('yolo.aa') }
26
- .to raise_error "The extension of 'yolo.aa' is not allowed"
27
- end
28
- end
29
- end
30
-
31
- context 'when :executable' do
32
- let(:attrs) { { executable: true } }
33
-
34
- it 'returns the path if the file is +x' do
35
- expect(opt.validate(rwx_file)).to eq rwx_file
36
- end
37
-
38
- it 'raises an error if not ' do
39
- expect { opt.validate(r_file) }.to raise_error "'#{r_file}' is not executable"
40
- end
41
- end
42
-
43
- context 'when :readable' do
44
- let(:attrs) { { readable: true, exists: false } }
45
-
46
- it 'returns the path if the file is +r' do
47
- expect(opt.validate(rwx_file)).to eq rwx_file
48
- end
49
-
50
- it 'raises an error otherwise' do
51
- file = File.join(FIXTURES, 'yolo.txt')
52
-
53
- expect { opt.validate(file) }.to raise_error "'#{file}' is not readable"
54
- end
55
- end
56
-
57
- context 'when :writable' do
58
- context 'when the path exists' do
59
- let(:attrs) { { writable: true } }
60
-
61
- it 'returns the path if the path is +x' do
62
- expect(opt.validate(rwx_file)).to eq rwx_file
63
- end
64
-
65
- it 'raises an error otherwise' do
66
- expect { opt.validate(r_file) }.to raise_error "'#{r_file}' is not writable"
67
- end
68
- end
69
-
70
- context 'when it does not exist' do
71
- let(:attrs) { { writable: true, exists: false } }
72
-
73
- context 'when the parent directory is +w' do
74
- let(:file) { File.join(FIXTURES, 'options_file', 'not_there.txt') }
75
-
76
- it 'returns the path' do
77
- expect(opt.validate(file)).to eq file
78
- end
79
- end
80
-
81
- context 'when the parent directory is not +w' do
82
- let(:file) { File.join(FIXTURES, 'hfjhg', 'yolo.rb') }
83
-
84
- it 'raises an error' do
85
- expect { opt.validate(file) }.to raise_error "'#{file}' is not writable"
86
- end
87
- end
88
- end
89
- end
90
- end
91
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptIntegerRange do
4
- subject(:opt) { described_class.new(['--range RANGE'], attrs) }
5
- let(:attrs) { {} }
6
-
7
- describe '#validate' do
8
- context 'when incorrect number of ranges given' do
9
- it 'raises an error' do
10
- expect { opt.validate('1-2-3') }.to raise_error('Incorrect number of ranges found: 3, should be 2')
11
- end
12
- end
13
-
14
- context 'when not an integer range' do
15
- it 'raises an error' do
16
- expect { opt.validate('a-e') }.to raise_error('Argument is not a valid integer range')
17
- end
18
- end
19
-
20
- context 'when a valid range' do
21
- it 'returns the range' do
22
- expect(opt.validate('1-5')).to eql((1..5))
23
- end
24
-
25
- context 'when another separator' do
26
- let(:attrs) { super().merge(separator: ':') }
27
-
28
- it 'returns the range' do
29
- expect(opt.validate('0:10')).to eql((0..10))
30
- end
31
- end
32
- end
33
-
34
- context 'when nil or "" supplied' do
35
- context 'when no value_if_empty attribute' do
36
- it 'raises an error' do
37
- [nil, ''].each do |value|
38
- expect { opt.validate(value) }.to raise_error 'Empty option value supplied'
39
- end
40
- end
41
- end
42
-
43
- context 'when value_if_empty attribute' do
44
- let(:attrs) { super().merge(value_if_empty: '0-2') }
45
-
46
- it 'returns the value_if_empty value' do
47
- [nil, ''].each do |value|
48
- expect(opt.validate(value)).to eql((0..2))
49
- end
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptInteger do
4
- subject(:opt) { described_class.new(['-i', '--int INT']) }
5
-
6
- describe '#validate' do
7
- context 'when not an integer' do
8
- it 'raises an error' do
9
- expect { opt.validate('a') }.to raise_error('a is not an integer')
10
- end
11
- end
12
-
13
- it 'returns the integer' do
14
- expect(opt.validate('12')).to eq 12
15
- end
16
- end
17
- end
@@ -1,105 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptMultiChoices do
4
- subject(:opt) { described_class.new(['--enumerate [CHOICES]'], attrs) }
5
- let(:attrs) do
6
- {
7
- choices: {
8
- vp: OptParseValidator::OptBoolean.new(['--vulnerable-plugins']),
9
- ap: OptParseValidator::OptBoolean.new(['--all-plugins']),
10
- p: OptParseValidator::OptBoolean.new(['--plugins']),
11
- vt: OptParseValidator::OptBoolean.new(['--vulnerable-themes']),
12
- at: OptParseValidator::OptBoolean.new(['--all-themes']),
13
- t: OptParseValidator::OptBoolean.new(['--themes']),
14
- tt: OptParseValidator::OptBoolean.new(['--timthumbs']),
15
- u: OptParseValidator::OptIntegerRange.new(['--users'], value_if_empty: '1-10'),
16
- m: OptParseValidator::OptIntegerRange.new(['--media'], value_if_empty: '1-100')
17
- }
18
- }
19
- end
20
-
21
- describe '#new' do
22
- context 'when no choices attribute' do
23
- let(:attrs) { {} }
24
-
25
- it 'raises an error' do
26
- expect { opt }.to raise_error 'The :choices attribute is mandatory'
27
- end
28
- end
29
-
30
- context 'when choices attribute' do
31
- context 'when not a hash' do
32
- let(:attrs) { { choices: 'invalid' } }
33
-
34
- it 'raises an error' do
35
- expect { opt }.to raise_error 'The :choices attribute must be a hash'
36
- end
37
- end
38
-
39
- context 'when a hash' do
40
- it 'does not raise any error' do
41
- expect { opt }.to_not raise_error
42
- end
43
- end
44
- end
45
- end
46
-
47
- describe '#validate' do
48
- context 'when an unknown choice is given' do
49
- it 'raises an error' do
50
- expect { opt.validate('vp,n') }.to raise_error 'Unknown choice: n'
51
- end
52
- end
53
-
54
- context 'when nil or empty value' do
55
- context 'when no value_if_empty attribute' do
56
- it 'raises an error' do
57
- [nil, ''].each do |value|
58
- expect { opt.validate(value) }.to raise_error 'Empty option value supplied'
59
- end
60
- end
61
- end
62
-
63
- context 'when value_if_empty attribute' do
64
- let(:attrs) { super().merge(value_if_empty: 'vp,u') }
65
-
66
- it 'returns the expected hash' do
67
- [nil, ''].each do |value|
68
- expect(opt.validate(value)).to eql(vulnerable_plugins: true, users: (1..10))
69
- end
70
- end
71
- end
72
- end
73
-
74
- context 'when value' do
75
- let(:attrs) do
76
- super().merge(incompatible: [
77
- [:vulnerable_plugins, :all_plugins, :plugins],
78
- [:vulnerable_themes, :all_themes, :themes]
79
- ])
80
- end
81
-
82
- it 'returns the expected hash' do
83
- expect(opt.validate('u2-5')).to eql(users: (2..5))
84
- end
85
-
86
- context 'when incompatible choices given' do
87
- it 'raises an error' do
88
- {
89
- 'ap,p,t' => 'all_plugins, plugins',
90
- 'ap,t,vp' => 'vulnerable_plugins, all_plugins',
91
- 'ap,at,t' => 'all_themes, themes'
92
- }.each do |value, msg|
93
- expect { opt.validate(value) }.to raise_error "Incompatible choices detected: #{msg}"
94
- end
95
- end
96
- end
97
- end
98
- end
99
-
100
- describe '#normalize' do
101
- it 'returns the same value (no normalization)' do
102
- expect(opt.normalize('a')).to eql 'a'
103
- end
104
- end
105
- end
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptPath do
4
- # Everything is handled in OptFilePath & OptDirectoryPath
5
- end
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptPositiveInteger do
4
- subject(:opt) { described_class.new(['-i', '--int INT']) }
5
-
6
- describe '#validate' do
7
- context 'when not > 0' do
8
- it 'raises an error' do
9
- expect { opt.validate('-3') }.to raise_error('-3 is not > 0')
10
- end
11
- end
12
-
13
- it 'returns the integer' do
14
- expect(opt.validate('20')).to eq 20
15
- end
16
- end
17
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptProxy do
4
- subject(:opt) { described_class.new(['--proxy PROXY'], attrs) }
5
- let(:attrs) { { protocols: %w(http https socks socks5 socks4) } }
6
-
7
- describe '#validate' do
8
- # Handled by OptURI
9
- end
10
- end
@@ -1,73 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptURI do
4
- subject(:opt) { described_class.new(['-u', '--uri URI'], attrs) }
5
- let(:attrs) { {} }
6
-
7
- describe '#new, #allowed_protocols' do
8
- context 'when no attrs supplied' do
9
- its(:allowed_protocols) { should be_empty }
10
- its(:default_protocol) { should be nil }
11
- end
12
-
13
- context 'when only one protocol supplied' do
14
- let(:attrs) { { protocols: 'http' } }
15
-
16
- it 'sets it' do
17
- opt.allowed_protocols << 'ftp'
18
- expect(opt.allowed_protocols).to eq %w(http ftp)
19
- end
20
- end
21
-
22
- context 'when multiple protocols are given' do
23
- let(:attrs) { { protocols: %w(ftp https) } }
24
-
25
- it 'sets them' do
26
- expect(opt.allowed_protocols).to eq attrs[:protocols]
27
- end
28
- end
29
- end
30
-
31
- describe '#validate' do
32
- context 'when allowed_protocols is empty' do
33
- it 'accepts all protocols' do
34
- %w(http ftp file).each do |p|
35
- expected = "#{p}://testing"
36
-
37
- expect(opt.validate(expected)).to eq expected
38
- end
39
- end
40
- end
41
-
42
- context 'when allowed_protocols is set' do
43
- let(:attrs) { { protocols: %w(https) } }
44
-
45
- it 'raises an error if the protocol is not allowed' do
46
- expect { opt.validate('ftp://ishouldnotbethere') }
47
- .to raise_error(Addressable::URI::InvalidURIError)
48
- end
49
-
50
- it 'returns the uri string if valid' do
51
- expected = 'https://example.com/'
52
-
53
- expect(opt.validate(expected)).to eq expected
54
- end
55
- end
56
-
57
- context 'when default_protocol' do
58
- let(:attrs) { { default_protocol: 'ftp' } }
59
-
60
- context 'when the argument already contains a protocol' do
61
- it 'does not add the default protocol' do
62
- expect(opt.validate('http://ex.lo')).to eq 'http://ex.lo'
63
- end
64
- end
65
-
66
- context 'when no protocol given in the argument' do
67
- it 'adds it' do
68
- expect(opt.validate('ex.lo')).to eq 'ftp://ex.lo'
69
- end
70
- end
71
- end
72
- end
73
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator::OptURL do
4
- subject(:opt) { described_class.new(['-u', '--url URL']) }
5
-
6
- describe '#validate' do
7
- context 'when the url is empty' do
8
- it 'raises an error' do
9
- expect { opt.validate('') }.to raise_error(Addressable::URI::InvalidURIError)
10
- end
11
- end
12
-
13
- context 'when the protocol is not allowed' do
14
- it 'raises an error' do
15
- expect { opt.validate('ftp://ftp.domain.com') }
16
- .to raise_error(Addressable::URI::InvalidURIError)
17
- end
18
- end
19
-
20
- it 'returns the url' do
21
- url = 'https://duckduckgo.com/'
22
-
23
- expect(opt.validate(url)).to eq url
24
- end
25
- end
26
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OptParseValidator do
4
- it 'returns the version' do
5
- expect(OptParseValidator::VERSION).to be >= '0'
6
- end
7
- end