Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (maint) Reset Puppet version after version_spec tests
  (PUP-6467) Fix Parallel Spec grouping for RSpec 3.5.0
  (PUP-6347) Use Puppet syntax in programmatic string representation
  (PUP-6347) Rename StringFormatter#format_literal to apply_string_flags
  • Loading branch information
glennsarti committed Jul 8, 2016
2 parents 93f5d63 + 98d644e commit 42becde
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 81 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Expand Up @@ -29,8 +29,7 @@ gem "hiera", *location_for(ENV['HIERA_LOCATION'] || ['>= 2.0', '< 4'])
gem "rake", "10.1.1", :require => false

group(:development, :test) do
# RSpec 3.5.z is not compatible with parallel:spec. The version pin of '< 3.5.0', be removed once PUP-6466 is resolved
gem "rspec", "~> 3.1", "< 3.5.0", :require => false
gem "rspec", "~> 3.1", :require => false
gem "rspec-its", "~> 1.1", :require => false
gem "rspec-collection_matchers", "~> 1.1", :require => false
gem "rspec-legacy_formatters", "~> 1.0", :require => false
Expand Down
82 changes: 61 additions & 21 deletions lib/puppet/pops/types/string_converter.rb
Expand Up @@ -557,7 +557,7 @@ def string_PAnyType(val_type, val, format_map, _)

def string_PDefaultType(val_type, val, format_map, _)
f = get_format(val_type, format_map)
format_literal(f, case f.format
apply_string_flags(f, case f.format
when :d, :s, :p
f.alt? ? '"default"' : 'default'
when :D
Expand All @@ -570,7 +570,7 @@ def string_PDefaultType(val_type, val, format_map, _)
# @api private
def string_PUndefType(val_type, val, format_map, _)
f = get_format(val_type, format_map)
format_literal(f, case f.format
apply_string_flags(f, case f.format
when :n
f.alt? ? 'null' : 'nil'
when :u
Expand All @@ -597,22 +597,22 @@ def string_PBooleanType(val_type, val, format_map, indentation)
when :t
# 'true'/'false' or 't'/'f' if in alt mode
str_bool = val.to_s
format_literal(f, f.alt? ? str_bool[0] : str_bool)
apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

when :T
# 'True'/'False' or 'T'/'F' if in alt mode
str_bool = val.to_s.capitalize
format_literal(f, f.alt? ? str_bool[0] : str_bool)
apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

when :y
# 'yes'/'no' or 'y'/'n' if in alt mode
str_bool = val ? 'yes' : 'no'
format_literal(f, f.alt? ? str_bool[0] : str_bool)
apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

when :Y
# 'Yes'/'No' or 'Y'/'N' if in alt mode
str_bool = val ? 'Yes' : 'No'
format_literal(f, f.alt? ? str_bool[0] : str_bool)
apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

when :d, :x, :X, :o, :b, :B
# Boolean in numeric form, formated by integer rule
Expand All @@ -627,18 +627,18 @@ def string_PBooleanType(val_type, val, format_map, indentation)
_convert(TypeCalculator.infer_set(numeric_bool), numeric_bool, string_formats, indentation)

when :s
format_literal(f, val.to_s)
apply_string_flags(f, val.to_s)

when :p
format_literal(f, val.inspect)
apply_string_flags(f, val.inspect)

else
raise FormatError.new('Boolean', f.format, 'tTyYdxXobBeEfgGaAsp')
end
end

# Performs post-processing of literals to apply width and precision flags
def format_literal(f, literal_str)
def apply_string_flags(f, literal_str)
if f.left || f.width || f.prec
fmt = '%'
fmt << '-' if f.left
Expand All @@ -650,7 +650,7 @@ def format_literal(f, literal_str)
literal_str
end
end
private :format_literal
private :apply_string_flags

# @api private
def string_PIntegerType(val_type, val, format_map, _)
Expand Down Expand Up @@ -700,37 +700,77 @@ def string_PFloatType(val_type, val, format_map, _)
def string_PStringType(val_type, val, format_map, _)
f = get_format(val_type, format_map)
case f.format
when :s, :p
when :s
Kernel.format(f.orig_fmt, val)

when :p
apply_string_flags(f, puppet_quote(val))

when :c
c_val = val.capitalize
substitute = f.alt? ? 'p' : 's'
Kernel.format(f.orig_fmt.gsub('c', substitute), c_val)
f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.gsub('c', 's'), c_val)

when :C
c_val = val.split('::').map {|s| s.capitalize }.join('::')
substitute = f.alt? ? 'p' : 's'
Kernel.format(f.orig_fmt.gsub('C', substitute), c_val)
f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.gsub('C', 's'), c_val)

when :u
substitute = f.alt? ? 'p' : 's'
Kernel.format(f.orig_fmt.gsub('u', substitute), val).upcase
c_val = val.upcase
f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.gsub('u', 's'), c_val)

when :d
substitute = f.alt? ? 'p' : 's'
Kernel.format(f.orig_fmt.gsub('d', substitute), val).downcase
c_val = val.downcase
f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.gsub('d', 's'), c_val)

when :t # trim
c_val = val.strip
substitute = f.alt? ? 'p' : 's'
Kernel.format(f.orig_fmt.gsub('t', substitute), c_val)
f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.gsub('t', 's'), c_val)

else
raise FormatError.new('String', f.format, 'cCudspt')
end
end

# Performs a '%p' formatting of the given _str_ such that the output conforms to Puppet syntax. An ascii string
# without control characters, dollar, single-qoute, or backslash, will be quoted using single quotes. All other
# strings will be quoted using double quotes.
#
# @param [String] str the string that should be formatted
# @return [String] the formatted string
#
# @api public
def puppet_quote(str)
if str.ascii_only? && (str =~ /(?:'|\$|\p{Cntrl}|\\)/).nil?
"'#{str}'"
else
bld = '"'
str.codepoints do |codepoint|
case codepoint
when 0x09
bld << '\\t'
when 0x0a
bld << '\\n'
when 0x0d
bld << '\\r'
when 0x22
bld << '\\"'
when 0x24
bld << '\\$'
when 0x5c
bld << '\\\\'
else
if codepoint < 0x20 || codepoint > 0x7f
bld << sprintf('\\u{%X}', codepoint)
else
bld.concat(codepoint)
end
end
end
bld << '"'
bld
end
end

# @api private
def string_PRegexpType(val_type, val, format_map, _)
f = get_format(val_type, format_map)
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet/pops/types/type_formatter.rb
Expand Up @@ -440,8 +440,7 @@ def string_Regexp(t) ; @bld << t.inspect; end
# @api private
def string_String(t)
# Use single qoute on strings that does not contain single quotes, control characters, or backslashes.
# TODO: This should move to StringConverter when this formatter is changed to take advantage of it
@bld << (t.ascii_only? && (t =~ /^(?:'|\p{Cntrl}|\\)$/).nil? ? "'#{t}'" : t.inspect)
@bld << StringConverter.singleton.puppet_quote(t)
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/functions/new_spec.rb
Expand Up @@ -565,7 +565,7 @@
context 'when invoked on String' do
{ {} => 'Notify[String, {}]',
[] => 'Notify[String, []]',
{'a'=>true} => 'Notify[String, {"a" => true}]',
{'a'=>true} => "Notify[String, {'a' => true}]",
[1,2,3,4] => 'Notify[String, [1, 2, 3, 4]]',
[[1,2],[3,4]] => 'Notify[String, [[1, 2], [3, 4]]]',
'abcd' => 'Notify[String, abcd]',
Expand Down

0 comments on commit 42becde

Please sign in to comment.