Sha256: 8c3fd83071e67869053f32617ef3f5394cd55a9e0148acb2209d012b0114e66b

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# frozen-string-literal: true

require 'yaml'
require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    ##
    # The Yaml check will read and parse YAML files to ensure they have valid
    # syntax.
    #
    class Yaml < Plugin
      def self.description
        'Runs yaml to detect errors.'
      end

      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

      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

      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

      def normal_load_file(file)
        YAML.load_file(file)

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pre-commit-0.40.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.39.0 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.38.1 lib/plugins/pre_commit/checks/yaml.rb
pre-commit-0.38.0 lib/plugins/pre_commit/checks/yaml.rb