command_mapper 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +27 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +1 -0
  5. data/.yardopts +1 -0
  6. data/ChangeLog.md +25 -0
  7. data/Gemfile +15 -0
  8. data/LICENSE.txt +20 -0
  9. data/README.md +369 -0
  10. data/Rakefile +12 -0
  11. data/commnad_mapper.gemspec +61 -0
  12. data/gemspec.yml +23 -0
  13. data/lib/command_mapper/arg.rb +75 -0
  14. data/lib/command_mapper/argument.rb +142 -0
  15. data/lib/command_mapper/command.rb +606 -0
  16. data/lib/command_mapper/exceptions.rb +19 -0
  17. data/lib/command_mapper/option.rb +282 -0
  18. data/lib/command_mapper/option_value.rb +21 -0
  19. data/lib/command_mapper/sudo.rb +73 -0
  20. data/lib/command_mapper/types/enum.rb +35 -0
  21. data/lib/command_mapper/types/hex.rb +82 -0
  22. data/lib/command_mapper/types/input_dir.rb +35 -0
  23. data/lib/command_mapper/types/input_file.rb +35 -0
  24. data/lib/command_mapper/types/input_path.rb +29 -0
  25. data/lib/command_mapper/types/key_value.rb +131 -0
  26. data/lib/command_mapper/types/key_value_list.rb +45 -0
  27. data/lib/command_mapper/types/list.rb +90 -0
  28. data/lib/command_mapper/types/map.rb +64 -0
  29. data/lib/command_mapper/types/num.rb +50 -0
  30. data/lib/command_mapper/types/str.rb +85 -0
  31. data/lib/command_mapper/types/type.rb +102 -0
  32. data/lib/command_mapper/types.rb +6 -0
  33. data/lib/command_mapper/version.rb +4 -0
  34. data/lib/command_mapper.rb +2 -0
  35. data/spec/arg_spec.rb +137 -0
  36. data/spec/argument_spec.rb +513 -0
  37. data/spec/commnad_spec.rb +1175 -0
  38. data/spec/exceptions_spec.rb +14 -0
  39. data/spec/option_spec.rb +882 -0
  40. data/spec/option_value_spec.rb +17 -0
  41. data/spec/spec_helper.rb +6 -0
  42. data/spec/sudo_spec.rb +24 -0
  43. data/spec/types/enum_spec.rb +31 -0
  44. data/spec/types/hex_spec.rb +158 -0
  45. data/spec/types/input_dir_spec.rb +30 -0
  46. data/spec/types/input_file_spec.rb +34 -0
  47. data/spec/types/input_path_spec.rb +32 -0
  48. data/spec/types/key_value_list_spec.rb +100 -0
  49. data/spec/types/key_value_spec.rb +272 -0
  50. data/spec/types/list_spec.rb +143 -0
  51. data/spec/types/map_spec.rb +62 -0
  52. data/spec/types/num_spec.rb +90 -0
  53. data/spec/types/str_spec.rb +232 -0
  54. data/spec/types/type_spec.rb +59 -0
  55. metadata +118 -0
@@ -0,0 +1,232 @@
1
+ require 'spec_helper'
2
+ require 'command_mapper/types/str'
3
+
4
+ describe CommandMapper::Types::Str do
5
+ describe "#initialize" do
6
+ it "must default allow_empty: to false" do
7
+ expect(subject.allow_empty?).to be(false)
8
+ end
9
+
10
+ it "must default allow_blank: to false" do
11
+ expect(subject.allow_blank?).to be(false)
12
+ end
13
+
14
+ context "when given allow_empty: true" do
15
+ subject { described_class.new(allow_empty: true) }
16
+
17
+ it "must set allow_empty: to true" do
18
+ expect(subject.allow_empty?).to be(true)
19
+ end
20
+ end
21
+
22
+ context "when given allow_empty: false" do
23
+ subject { described_class.new(allow_empty: false) }
24
+
25
+ it "must set allow_empty: to false" do
26
+ expect(subject.allow_empty?).to be(false)
27
+ end
28
+ end
29
+
30
+ context "when given allow_blank: true" do
31
+ subject { described_class.new(allow_blank: true) }
32
+
33
+ it "must set allow_blank: to true" do
34
+ expect(subject.allow_blank?).to be(true)
35
+ end
36
+ end
37
+
38
+ context "when given allow_blank: false" do
39
+ subject { described_class.new(allow_blank: false) }
40
+
41
+ it "must set allow_blank: to false" do
42
+ expect(subject.allow_blank?).to be(false)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#allow_empty?" do
48
+ it "must be false by default" do
49
+ expect(subject.allow_empty?).to be(false)
50
+ end
51
+
52
+ context "when initialized with allow_empty: true" do
53
+ subject { described_class.new(allow_empty: true) }
54
+
55
+ it "must be true" do
56
+ expect(subject.allow_empty?).to be(true)
57
+ end
58
+ end
59
+
60
+ context "when initialized with allow_empty: true" do
61
+ subject { described_class.new(allow_empty: false) }
62
+
63
+ it "must be false" do
64
+ expect(subject.allow_empty?).to be(false)
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "#allow_blank?" do
70
+ it "must be false by default" do
71
+ expect(subject.allow_blank?).to be(false)
72
+ end
73
+
74
+ context "when initialized with allow_blank: true" do
75
+ subject { described_class.new(allow_blank: true) }
76
+
77
+ it "must be true" do
78
+ expect(subject.allow_blank?).to be(true)
79
+ end
80
+ end
81
+
82
+ context "when initialized with allow_blank: true" do
83
+ subject { described_class.new(allow_blank: false) }
84
+
85
+ it "must be false" do
86
+ expect(subject.allow_blank?).to be(false)
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "#validate" do
92
+ context "when given nil" do
93
+ let(:value) { nil }
94
+
95
+ it "must return [false, \"cannot be nil\"]" do
96
+ expect(subject.validate(value)).to eq(
97
+ [false, "cannot be nil"]
98
+ )
99
+ end
100
+
101
+ context "but #allow_empty? is true" do
102
+ subject { described_class.new(allow_empty: true) }
103
+
104
+ it "must return true" do
105
+ expect(subject.validate(value)).to be(true)
106
+ end
107
+ end
108
+ end
109
+
110
+ context "when a String is given" do
111
+ let(:value) { "foo" }
112
+
113
+ it "must return true" do
114
+ expect(subject.validate(value)).to be(true)
115
+ end
116
+
117
+ context "and it's empty" do
118
+ let(:value) { "" }
119
+
120
+ it "must return [false, \"does not allow an empty value\"]" do
121
+ expect(subject.validate(value)).to eq([false, "does not allow an empty value"])
122
+ end
123
+
124
+ context "but #allow_empty? is true" do
125
+ subject { described_class.new(allow_empty: true) }
126
+
127
+ it "must return true" do
128
+ expect(subject.validate(value)).to be(true)
129
+ end
130
+ end
131
+ end
132
+
133
+ context "and it's blank" do
134
+ let(:value) { " \t\n\r\v " }
135
+
136
+ it "must return [false, \"does not allow a blank value (...)\"]" do
137
+ expect(subject.validate(value)).to eq([false, "does not allow a blank value (#{value.inspect})"])
138
+ end
139
+
140
+ context "but #allow_blank? is true" do
141
+ subject { described_class.new(allow_blank: true) }
142
+
143
+ it "must return true" do
144
+ expect(subject.validate(value)).to be(true)
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ context "when a Symbol is given" do
151
+ let(:value) { :foo }
152
+
153
+ it "must return true" do
154
+ expect(subject.validate(value)).to be(true)
155
+ end
156
+
157
+ context "and it's empty" do
158
+ let(:value) { :"" }
159
+
160
+ it "must return [false, \"does not allow an empty value\"]" do
161
+ expect(subject.validate(value)).to eq([false, "does not allow an empty value"])
162
+ end
163
+
164
+ context "but #allow_empty? is true" do
165
+ subject { described_class.new(allow_empty: true) }
166
+
167
+ it "must return true" do
168
+ expect(subject.validate(value)).to be(true)
169
+ end
170
+ end
171
+ end
172
+
173
+ context "and it's blank" do
174
+ let(:value) { :" \t\n\r\v " }
175
+
176
+ it "must return [false, \"does not allow a blank value (...)\"]" do
177
+ expect(subject.validate(value)).to eq([false, "does not allow a blank value (#{value.inspect})"])
178
+ end
179
+
180
+ context "but #allow_blank? is true" do
181
+ subject { described_class.new(allow_blank: true) }
182
+
183
+ it "must return true" do
184
+ expect(subject.validate(value)).to be(true)
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ context "when an Enumerable object is given" do
191
+ let(:value) { %w[foo bar] }
192
+
193
+ it "must return [false, \"cannot convert an Enumerable object into a String (...)\"]" do
194
+ expect(subject.validate(value)).to eq(
195
+ [false, "cannot convert a #{value.class} into a String (#{value.inspect})"]
196
+ )
197
+ end
198
+ end
199
+
200
+ context "when another kind of Object is given" do
201
+ module TestStr
202
+ class CustomObject
203
+ def to_s
204
+ "foo"
205
+ end
206
+ end
207
+ end
208
+
209
+ let(:value) { TestStr::CustomObject.new }
210
+
211
+ it "must return true" do
212
+ expect(subject.validate(value)).to be(true)
213
+ end
214
+
215
+ context "but the object does not define a #to_s method" do
216
+ module TestStr
217
+ class ObjectWithoutToS
218
+ undef to_s
219
+ end
220
+ end
221
+
222
+ let(:value) { TestStr::ObjectWithoutToS.new }
223
+
224
+ it "must return [false, \"dpes not define a #to_s method (...)\"]" do
225
+ expect(subject.validate(value)).to eq(
226
+ [false, "does not define a #to_s method (#{value.inspect})"]
227
+ )
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'command_mapper/types/type'
3
+
4
+ describe CommandMapper::Types::Type do
5
+ describe "#validate" do
6
+ it "must return true by default" do
7
+ expect(subject.validate(Object.new)).to be(true)
8
+ end
9
+ end
10
+
11
+ describe "#format" do
12
+ it "must convert the given value into a String" do
13
+ expect(subject.format(1)).to eq("1")
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "CommandMapper::Types::Type()" do
19
+ context "when given a CommandMapper::Types::Type" do
20
+ let(:value) { CommandMapper::Types::Type.new }
21
+
22
+ subject { CommandMapper::Types::Type(value) }
23
+
24
+ it "must return the CommandMapper::Types::Type object" do
25
+ expect(subject).to be(value)
26
+ end
27
+ end
28
+
29
+ context "when given a Hash" do
30
+ let(:value) { {allow_empty: true} }
31
+
32
+ subject { CommandMapper::Types::Type(value) }
33
+
34
+ it "must initialize a new CommandMapper::Types::Str" do
35
+ expect(subject).to be_kind_of(CommandMapper::Types::Str)
36
+ expect(subject.allow_empty?).to be(true)
37
+ end
38
+ end
39
+
40
+ context "when given nil" do
41
+ let(:value) { nil }
42
+
43
+ subject { CommandMapper::Types::Type(value) }
44
+
45
+ it "must return nil" do
46
+ expect(subject).to be(nil)
47
+ end
48
+ end
49
+
50
+ context "when given another kind of Object" do
51
+ let(:value) { Object.new }
52
+
53
+ it do
54
+ expect {
55
+ CommandMapper::Types::Type(value)
56
+ }.to raise_error(ArgumentError,"value must be a CommandMapper::Types::Type, Hash, or nil: #{value.inspect}")
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre1
5
+ platform: ruby
6
+ authors:
7
+ - Postmodern
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Command Mapper maps a command's arguments to Class attributes to allow
28
+ safely and securely executing commands.
29
+ email: postmodern.mod3@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - ChangeLog.md
34
+ - LICENSE.txt
35
+ - README.md
36
+ files:
37
+ - ".github/workflows/ruby.yml"
38
+ - ".gitignore"
39
+ - ".rspec"
40
+ - ".yardopts"
41
+ - ChangeLog.md
42
+ - Gemfile
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - commnad_mapper.gemspec
47
+ - gemspec.yml
48
+ - lib/command_mapper.rb
49
+ - lib/command_mapper/arg.rb
50
+ - lib/command_mapper/argument.rb
51
+ - lib/command_mapper/command.rb
52
+ - lib/command_mapper/exceptions.rb
53
+ - lib/command_mapper/option.rb
54
+ - lib/command_mapper/option_value.rb
55
+ - lib/command_mapper/sudo.rb
56
+ - lib/command_mapper/types.rb
57
+ - lib/command_mapper/types/enum.rb
58
+ - lib/command_mapper/types/hex.rb
59
+ - lib/command_mapper/types/input_dir.rb
60
+ - lib/command_mapper/types/input_file.rb
61
+ - lib/command_mapper/types/input_path.rb
62
+ - lib/command_mapper/types/key_value.rb
63
+ - lib/command_mapper/types/key_value_list.rb
64
+ - lib/command_mapper/types/list.rb
65
+ - lib/command_mapper/types/map.rb
66
+ - lib/command_mapper/types/num.rb
67
+ - lib/command_mapper/types/str.rb
68
+ - lib/command_mapper/types/type.rb
69
+ - lib/command_mapper/version.rb
70
+ - spec/arg_spec.rb
71
+ - spec/argument_spec.rb
72
+ - spec/commnad_spec.rb
73
+ - spec/exceptions_spec.rb
74
+ - spec/option_spec.rb
75
+ - spec/option_value_spec.rb
76
+ - spec/spec_helper.rb
77
+ - spec/sudo_spec.rb
78
+ - spec/types/enum_spec.rb
79
+ - spec/types/hex_spec.rb
80
+ - spec/types/input_dir_spec.rb
81
+ - spec/types/input_file_spec.rb
82
+ - spec/types/input_path_spec.rb
83
+ - spec/types/key_value_list_spec.rb
84
+ - spec/types/key_value_spec.rb
85
+ - spec/types/list_spec.rb
86
+ - spec/types/map_spec.rb
87
+ - spec/types/num_spec.rb
88
+ - spec/types/str_spec.rb
89
+ - spec/types/type_spec.rb
90
+ homepage: https://github.com/postmodern/command_mapper.rb#readme
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ documentation_uri: https://rubydoc.info/gems/command_mapper
95
+ source_code_uri: https://github.com/postmodern/command_mapper.rb
96
+ bug_tracker_uri: https://github.com/postmodern/command_mapper.rb/issues
97
+ changelog_uri: https://github.com/postmodern/command_mapper.rb/blob/master/ChangeLog.md
98
+ rubygems_mfa_required: 'true'
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 2.0.0
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.2.22
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Safe and secure execution of commands.
118
+ test_files: []