Skip to content

Commit

Permalink
Merge pull request #48 from jamiemccarthy/jm-fix-rubocop-issues
Browse files Browse the repository at this point in the history
Fix rubocop issues
  • Loading branch information
shortdudey123 authored Jun 1, 2022
2 parents 7d9b271 + edb4003 commit 0f34496
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 52 deletions.
17 changes: 14 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ AllCops:
DisplayCopNames: true

Metrics/AbcSize:
Max: 36
Max: 37

Metrics/BlockLength:
Exclude:
- spec/*.rb

Metrics/CyclomaticComplexity:
Max: 9
Max: 11

Metrics/MethodLength:
Max: 26
Max: 27

Metrics/ParameterLists:
Max: 5
MaxOptionalParameters: 4

Metrics/PerceivedComplexity:
Max: 9

Style/FrozenStringLiteralComment:
Include:
- lib/**/*.rb
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This file is used to list changes made in each version of the YamlLint gem.
- **[PR #45](https://github.com/shortdudey123/yamllint/pull/45)** - Rescue Psych exceptions
- **[PR #46](https://github.com/shortdudey123/yamllint/pull/46)** - Add three valid test files
- **[PR #47](https://github.com/shortdudey123/yamllint/pull/47)** - Fix readme typo
- **[PR #48](https://github.com/shortdudey123/yamllint/pull/48)** - Fix rubocop issues

## v0.0.9 (2016-09-16)
- **[PR #24](https://github.com/shortdudey123/yamllint/pull/24)** - Update RSpec raise_error to be more specific
Expand Down
36 changes: 18 additions & 18 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,52 @@ end

desc 'rubocop compliancy checks'
RuboCop::RakeTask.new(:rubocop) do |t|
t.patterns = %w( lib/**/*.rb lib/*.rb spec/*.rb )
t.patterns = %w[lib/**/*.rb lib/*.rb spec/*.rb]
end

desc 'yamllint rake test'
YamlLint::RakeTask.new do |t|
t.paths = %w( spec/data/valid* )
t.paths = %w[spec/data/valid*]
end

desc 'yamllint rake test with exclude_paths'
YamlLint::RakeTask.new(:yamllint_exclude_paths) do |t|
t.paths = %w(
t.paths = %w[
spec/data/*
)
t.exclude_paths = %w(
]
t.exclude_paths = %w[
spec/data/custom_extension.eyaml
spec/data/invalid.yaml
spec/data/overlapping_keys.yaml
spec/data/overlapping_keys_deep.yaml
spec/data/wrong_extension.txt
)
]
end

desc 'yamllint rake test disabled file ext check'
YamlLint::RakeTask.new(:yamllint_disable_ext_check) do |t|
t.paths = %w( spec/data/wrong_extension.txt )
t.paths = %w[spec/data/wrong_extension.txt]
t.disable_ext_check = true
end

desc 'yamllint rake test disabled file ext check'
YamlLint::RakeTask.new(:yamllint_custom_ext) do |t|
t.paths = %w( spec/data/custom_extension.eyaml )
t.extensions = %w( eyaml )
t.paths = %w[spec/data/custom_extension.eyaml]
t.extensions = %w[eyaml]
end

desc 'yamllint rake test disabled file ext check'
YamlLint::RakeTask.new(:yamllint_debug_logging) do |t|
t.paths = %w( spec/data/valid.yaml )
t.paths = %w[spec/data/valid.yaml]
t.debug = true
end

task default: [
:rubocop,
:yamllint,
:yamllint_exclude_paths,
:yamllint_disable_ext_check,
:yamllint_custom_ext,
:yamllint_debug_logging,
:spec
task default: %i[
rubocop
yamllint
yamllint_exclude_paths
yamllint_disable_ext_check
yamllint_custom_ext
yamllint_debug_logging
spec
]
2 changes: 1 addition & 1 deletion bin/yamllint
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.expand_path('../../lib', __FILE__)
$LOAD_PATH << File.expand_path('../lib', __dir__)

require 'yamllint'
require 'yamllint/cli'
Expand Down
4 changes: 3 additions & 1 deletion lib/yamllint.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'logger'

require 'yamllint/version'
Expand All @@ -8,7 +10,7 @@
# YamlLint checks YAML files for correct syntax
module YamlLint
def self.logger
@logger ||= Logger.new(STDOUT).tap do |l|
@logger ||= Logger.new($stdout).tap do |l|
l.level = Logger::INFO
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/yamllint/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'logger'
require 'optimist'

Expand All @@ -9,7 +11,7 @@ class CLI
attr_reader :opts

# setup CLI options
def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR,
def initialize(argv, stdin = $stdin, stdout = $stdout, stderr = $stderr,
kernel = Kernel)
@argv = argv
@stdin = stdin
Expand Down Expand Up @@ -41,6 +43,7 @@ def lint(files_to_check)

puts 'YamlLint found no errors' unless linter.errors?
return unless linter.errors?

linter.display_errors
puts "YAML lint found #{linter.errors_count} errors"
@kernel.exit(1)
Expand All @@ -55,7 +58,7 @@ def lint_files(files_to_check)
begin
puts "Checking #{files_to_check.flatten.length} files"
linter.check_all(files_to_check)
rescue => e
rescue StandardError => e
@stderr.puts e.message
exit(1)
end
Expand All @@ -66,8 +69,8 @@ def lint_files(files_to_check)
def lint_stream
linter = YamlLint::Linter.new
begin
linter.check_stream(STDIN)
rescue => e
linter.check_stream($stdin)
rescue StandardError => e
@stderr.puts e.message
exit(1)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/yamllint/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module YamlLint
class FileNotFoundError < StandardError; end
end
21 changes: 10 additions & 11 deletions lib/yamllint/linter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'set'
require 'yaml'

Expand All @@ -8,10 +10,7 @@ module YamlLint
# Runs the actual linting
#
class Linter
attr_reader :disable_extension_check
attr_reader :errors
attr_reader :extensions
attr_reader :valid_extensions
attr_reader :disable_extension_check, :errors, :extensions, :valid_extensions

# Initilize the linter
# Params:
Expand All @@ -36,11 +35,9 @@ def check(path)
raise FileNotFoundError, "#{path}: no such file" unless File.exist?(path)

valid = false
unless disable_extension_check
unless check_filename(path)
errors[path] = ['File extension must be .yaml or .yml']
return valid
end
if !disable_extension_check && !check_filename(path)
errors[path] = ['File extension must be .yaml or .yml']
return valid
end

File.open(path, 'r') do |f|
Expand Down Expand Up @@ -89,6 +86,7 @@ def display_errors
def check_filename(filename)
extension = filename.split('.').last
return true if valid_extensions.include?(extension)

false
end

Expand All @@ -112,7 +110,7 @@ def check_syntax_valid?(yaml_data, errors_array)
end
# rubocop:enable Security/YAMLLoad
true
rescue YAML::SyntaxError, Psych::Exception => e
rescue Psych::Exception => e
errors_array << e.message
false
end
Expand Down Expand Up @@ -142,7 +140,7 @@ def parse(psych_parse_data)
private

# Recusively check for duplicate keys
def parse_recurse(psych_parse_data, is_sequence = false)
def parse_recurse(psych_parse_data, is_sequence = false) # rubocop:disable Style/OptionalBooleanParameter
return if psych_parse_data.nil?

is_key = false
Expand Down Expand Up @@ -234,6 +232,7 @@ def check_for_overlap!
YamlLint.logger.debug { "Checking #{full_key.join('.')} for overlap" }

return if @seen_keys.add?(full_key)

YamlLint.logger.debug { "Overlapping key #{full_key.join('.')}" }
@overlapping_keys << full_key
end
Expand Down
12 changes: 5 additions & 7 deletions lib/yamllint/rake_task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rake'
require 'rake/tasklib'

Expand All @@ -8,15 +10,11 @@ module YamlLint
# RakeTast execution
#
class RakeTask < Rake::TaskLib
attr_accessor :name
attr_accessor :debug
attr_accessor :disable_ext_check
attr_accessor :exclude_paths
attr_accessor :extensions
attr_accessor :fail_on_error
attr_accessor :paths
attr_accessor :name, :debug, :disable_ext_check, :exclude_paths, :extensions, :fail_on_error, :paths

def initialize(name = :yamllint)
super()

@debug = false
@disable_ext_check = false
@exclude_paths = []
Expand Down
4 changes: 3 additions & 1 deletion lib/yamllint/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

###
#
# YamlLint checks YAML files for correct syntax
module YamlLint
VERSION = '0.0.9'.freeze
VERSION = '0.0.9'
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def yamllint(args = nil)
end

def yamllint_bin
File.expand_path('../../bin/yamllint', __FILE__)
File.expand_path('../bin/yamllint', __dir__)
end
end

Expand Down
11 changes: 6 additions & 5 deletions yamllint.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'yamllint/version'

Expand All @@ -13,18 +12,20 @@ Gem::Specification.new do |spec|
spec.license = 'MIT'
spec.homepage = ''

spec.required_ruby_version = '>= 2.4'

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'optimist'

spec.add_development_dependency 'aruba', '~> 0.12'
spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'aruba', '~> 0.12'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'coveralls'
end

0 comments on commit 0f34496

Please sign in to comment.