Skip to content

Commit 55638c2

Browse files
authored
Merge pull request #223 from DannyBen/update/unique-validation
Improve non-unique command validation by testing name and aliases together
2 parents c6081f9 + 4f00423 commit 55638c2

File tree

11 files changed

+34
-12
lines changed

11 files changed

+34
-12
lines changed

lib/bashly/concerns/validation_helpers.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ def assert_hash(key, value, whitelist = nil)
5252
end
5353
end
5454

55-
def assert_uniq(key, value, array_key)
55+
def assert_uniq(key, value, array_keys)
5656
return unless value
57-
list = value.map { |c| c[array_key] }.compact.flatten
58-
assert list.uniq?, "#{key} cannot have elements with similar #{array_key} values"
57+
array_keys = [array_keys] unless array_keys.is_a? Array
58+
list = []
59+
array_keys.each do |array_key|
60+
list += value.map { |c| c[array_key] }.compact.flatten
61+
end
62+
63+
nonuniqs = list.nonuniq
64+
assert nonuniqs.empty?, "#{key} contains non-unique elements (#{nonuniqs.join ', '}) in #{array_keys.join ' or '}"
5965
end
6066

6167
def assert_string_or_array(key, value)

lib/bashly/config_validator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def assert_command(key, value)
124124
assert_array "#{key}.environment_variables", value['environment_variables'], of: :env_var
125125
assert_array "#{key}.examples", value['examples'], of: :string
126126

127-
assert_uniq "#{key}.commands", value['commands'], 'name'
128-
assert_uniq "#{key}.commands", value['commands'], 'alias'
127+
assert_uniq "#{key}.commands", value['commands'], ['name', 'alias']
129128
assert_uniq "#{key}.flags", value['flags'], 'long'
130129
assert_uniq "#{key}.flags", value['flags'], 'short'
131130
assert_uniq "#{key}.args", value['args'], 'name'

lib/bashly/extensions/array.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ def indent(offset)
55
map { |line| "#{indentation}#{line}" }
66
end
77

8-
def uniq?
9-
self == uniq
8+
def nonuniq
9+
tally.select { |key, count| count > 1 }.keys
1010
end
1111

1212
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.args cannot have elements with similar name values>
1+
#<Bashly::ConfigurationError: root.args contains non-unique elements (file) in name>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.commands cannot have elements with similar alias values>
1+
#<Bashly::ConfigurationError: root.commands contains non-unique elements (c) in name or alias>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.commands contains non-unique elements (pull) in name or alias>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.commands cannot have elements with similar name values>
1+
#<Bashly::ConfigurationError: root.commands contains non-unique elements (download) in name or alias>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.flags cannot have elements with similar long values>
1+
#<Bashly::ConfigurationError: root.flags contains non-unique elements (--force) in long>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.flags cannot have elements with similar short values>
1+
#<Bashly::ConfigurationError: root.flags contains non-unique elements (-f) in short>

spec/bashly/extensions/array_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
end
1414
end
1515
end
16+
17+
describe '#nonuniq' do
18+
subject { %w[one it it two works three works] }
19+
it "returns an array of non unique elements" do
20+
expect(subject.nonuniq).to eq %w[it works]
21+
end
22+
end
1623
end

0 commit comments

Comments
 (0)