class PreCommit::Checks::Yaml

The Yaml check will read and parse YAML files to ensure they have valid syntax.

Public Class Methods

description() click to toggle source
# File lib/plugins/pre_commit/checks/yaml.rb, line 13
def self.description
  'Runs yaml to detect errors.'
end

Public Instance Methods

call(staged_files) click to toggle source
# File lib/plugins/pre_commit/checks/yaml.rb, line 17
def call(staged_files)
  staged_files = staged_files.grep(/\.(yml|yaml)$/)
  return if staged_files.empty?

  errors = staged_files.map {|file| load_file(file)}.compact

  errors.join("\n") + "\n" unless errors.empty?
end

Private Instance Methods

load_file(file) click to toggle source
# File lib/plugins/pre_commit/checks/yaml.rb, line 28
def load_file(file)
  if YAML.respond_to?(:safe_load)
    safe_load_file(file)
  else
    normal_load_file(file)
  end

rescue Psych::SyntaxError => e
  e.message
end
normal_load_file(file) click to toggle source
# File lib/plugins/pre_commit/checks/yaml.rb, line 47
def normal_load_file(file)
  YAML.load_file(file)

  nil
rescue ArgumentError
  $stdout.puts "Warning: Skipping '#{file}' because it contains serialized ruby objects."
end
safe_load_file(file) click to toggle source
# File lib/plugins/pre_commit/checks/yaml.rb, line 39
def safe_load_file(file)
  YAML.safe_load(File.read(file), [::Symbol], [], true, file)

  nil
rescue Psych::DisallowedClass
  $stdout.puts "Warning: Skipping '#{file}' because it contains serialized ruby objects."
end