Class: PreCommit::Checks::Yaml

Inherits:
Plugin
  • Object
show all
Defined in:
lib/plugins/pre_commit/checks/yaml.rb

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize, #name

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.descriptionObject



43
44
45
# File 'lib/plugins/pre_commit/checks/yaml.rb', line 43

def self.description
  'Runs yaml to detect errors.'
end

Instance Method Details

#call(staged_files) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/plugins/pre_commit/checks/yaml.rb', line 7

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

#load_file(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/plugins/pre_commit/checks/yaml.rb', line 16

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) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/plugins/pre_commit/checks/yaml.rb', line 35

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) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/plugins/pre_commit/checks/yaml.rb', line 27

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