Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active October 1, 2020 23:41
Show Gist options
  • Save natemccurdy/1958e0dcaa37ef45c71e7b5bbd5da99e to your computer and use it in GitHub Desktop.
Save natemccurdy/1958e0dcaa37ef45c71e7b5bbd5da99e to your computer and use it in GitHub Desktop.
# This is: ben_weird_sort/functions/sort_hiera_roles.pp
#
# Returns a list of hiera role paths in sorted order
#
# Strings are returned in lexical order, unless one path is a substring
# of another, in which case the longer string appears first.
#
# This is so that child roles are checked before parent roles
#
function ben_weird_sort::sort_hiera_roles($list) {
$list.sort |$a, $b| {
if $a == $b {
0
} elsif length($a) > length($b) and $a[0, length($b)+1] == "${b}/" {
-1
} elsif length($b) > length($a) and $b[0, length($a)+1] == "${a}/" {
1
} else {
compare($a, $b)
}
}
}
# This is: ben_weird_sort/spec/functions/sort_hiera_roles.rb
require 'spec_helper'
describe 'ben_weird_sort::sort_hiera_roles' do
context 'with multiple roles' do
it {
is_expected.to run.with_params(
['qux', 'foo', 'foo/barbie', 'foo/bar', 'foo/bar/baz', 'foo/baz'],
).and_return(
['foo/bar/baz', 'foo/bar', 'foo/barbie', 'foo/baz', 'foo', 'qux'],
)
}
end
end
@natemccurdy
Copy link
Author

 ~/ben_weird_sort $ pdk test unit
pdk (INFO): Using Ruby 2.5.8
pdk (INFO): Using Puppet 6.17.0
[✔] Preparing to run the unit tests.
/opt/puppetlabs/pdk/private/ruby/2.5.8/bin/ruby -I/opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-core-3.9.2/lib:/opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-support-3.9.3/lib /opt/puppetlabs/pdk/share/cache/ruby/2.5.0/gems/rspec-core-3.9.2/exe/rspec --pattern spec/\{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit\}/\*\*/\*_spec.rb --format progress
Run options: exclude {:bolt=>true}
.

Finished in 0.61572 seconds (files took 3.67 seconds to load)
1 example, 0 failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment