lib/onceover/codequality/syntax.rb in onceover-codequality-0.4.2 vs lib/onceover/codequality/syntax.rb in onceover-codequality-0.5.0

- old
+ new

@@ -1,24 +1,62 @@ -# puppet-syntax does everything we want so we just need a handy way to run -# it and check the result +require 'open3' class Onceover module CodeQuality module Syntax + def self.puppet status = true - if File.exist?("Puppetfile") - status &= system("r10k puppetfile check") + + # + # puppet-lint + # + + logger.info("Checking syntax using puppet-syntax rake task...") + # puppet-syntax seems to assign $stdout/$stderr internally in ways that + # prevent capturing output. As a nasty hack, run it as inline ruby and + # capture the output from the process... + inline_ruby = "require 'puppet-syntax/tasks/puppet-syntax' ; Rake::Task['syntax'].invoke" + output, s = Open3.capture2e("ruby", "-e", inline_ruby) + ok = s.exitstatus.zero? + status &= ok + + if ok + logger.info("...ok") else - logger.warn("No Puppetfile found... continuing") + logger.error("puppet-syntax validation failed: #{output}") end - require 'puppet-syntax/tasks/puppet-syntax' - begin - Rake::Task['syntax'].invoke - rescue SystemExit => e - logger.error("Invalid syntax") - status &= e.status + # + # python yaml + # + + # Python gives us "better" validation of YAML data then ruby, eg: + # ```yaml + # foo: bar + # baz: clive + # ``` + # + # would parse only the foo key in ruby, throwing away the baz key due to + # a perceived negative indent, whereas python would tell you to fix the + # file and make it consistent. This is yaml implementation dependent but + # users would be advised to fix the file, so lets _also_ validate yaml + # files with python if available on our path... + if system("python --version", :err => File::NULL) + logger.info("Running additional python YAML validation") + script = File.join(File.dirname(File.expand_path(__FILE__)), "../../../res/validate_yaml.py") + output, s = Open3.capture2e("python #{script}") + ok = s.exitstatus.zero? + status &= ok + + if ok + logger.info("...ok") + else + logger.error("Puppetfile validation failed: #{output}") + end + else + logger.warn("Please install python and add it to your path for enhanced YAML validation") end + status end end end end