diff --git a/functions/dir_clean.pp b/functions/dir_clean.pp new file mode 100644 index 0000000..b87d5aa --- /dev/null +++ b/functions/dir_clean.pp @@ -0,0 +1,20 @@ +# @summary Take a path and normalise it to its Unix form. +# Instead of having to deal with the different separators between Unix and Windows this +# function instead formats Windows paths a equivalent Unix like path. +# +# @param dir The path to clean +# @return Stdlib::Unixpath The cleaned path +# @example clean Unix paths to return `/tmp/test/libs` (i.e. noop) +# extlib::dir_clean('/tmp/test/libs') +# @example Clean Windows paths to return `/c/test/libs` +# extlib::dir_clean('c:\\'test\\libs') +# +# $dir is defined as a Variant to support cleaning 'c:' which is not a valid +# Stdlib::Absolutepath +function extlib::dir_clean(Variant[Stdlib::Absolutepath, Pattern[/\A[a-zA-Z]:\z/]] $dir) >> Stdlib::Unixpath { + $dir ? { + Stdlib::Windowspath => $dir.regsubst('^([a-zA-Z]):', '/\\1').regsubst('\\\\', '/', 'G'), + Pattern[/\A[a-z]:\z/] => $dir.regsubst('^([a-zA-Z]):', '/\\1'), + default => $dir, + } +} diff --git a/spec/functions/extlib/dir_clean_spec.rb b/spec/functions/extlib/dir_clean_spec.rb new file mode 100644 index 0000000..fc800f6 --- /dev/null +++ b/spec/functions/extlib/dir_clean_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe 'extlib::dir_clean' do + describe 'check functions' do + let(:dirs) do + { + 'c:' => '/c', + 'c:\windows\puppetlabs\puppet\embedded\gems' => '/c/windows/puppetlabs/puppet/embedded/gems', + '/opt/puppetlabs/puppet/embedded/bin/gems' => '/opt/puppetlabs/puppet/embedded/bin/gems', + } + end + it 'valid dirs' do + dirs.each_pair do |input, output| + is_expected.to run.with_params(input).and_return(output) + end + end + end +end