Last active
August 29, 2015 14:07
-
-
Save aerostitch/4a24d246c18566d7b46a to your computer and use it in GitHub Desktop.
This gist is a part of a Rakefile I use to test my Puppet's ERB (templates) files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This gist is a part of a Rakefile I use to test my Puppet's ERB (templates) files. | |
# Add this to the Rakefile at the root of your puppet module. | |
# | |
# == Examples: | |
# rake test:erb | |
# or | |
# rake test | |
# | |
# Author:: Joseph Herlant ([email protected]) | |
# Copyright:: Copyright (c) 2014 Joseph Herlant | |
# License:: Distributed under the terms of the Apache 2 license | |
# | |
# This is just to have a nice colored output | |
class String | |
def red; "\033[31m#{self}\033[0m" end | |
def green; "\033[32m#{self}\033[0m" end | |
end | |
desc "Run erb syntax checks." | |
task :test => [ | |
'test:erb', | |
] | |
namespace :test do | |
desc 'Checks that erb files can be rendered by the erb parser.' | |
task :erb do | |
require 'erb' | |
successes = [] | |
failures = [] | |
Dir.glob("**/*.erb").each do |t_file| | |
puts "Checking syntax for #{t_file}" | |
# Run syntax checker in subprocess. | |
system("erb -P -x -T '-' #{t_file} | ruby -c > /dev/null") | |
# Keep track of the results. | |
if $?.success? | |
successes << t_file | |
else | |
failures << t_file | |
end | |
end | |
# Print the results. | |
total_t = successes.count + failures.count | |
puts "#{successes.count} templates syntax valid over #{total_t}.".green | |
if failures.count > 0 | |
puts "#{failures.count} files failed:".red | |
puts | |
failures.each{ |filename| puts filename.red } | |
fail("#{failures.count} files failed syntax check.") | |
else | |
puts "#{failures.count} failed templates syntax check.".green | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment