Skip to content

Commit

Permalink
Merge pull request #56 from wteuber/appease_rubocop_and_mutant
Browse files Browse the repository at this point in the history
Appease Rubocop and Mutant, Update README
  • Loading branch information
chrisbarber86 authored Sep 19, 2024
2 parents 3148706 + abd1330 commit 65d352a
Show file tree
Hide file tree
Showing 36 changed files with 714 additions and 641 deletions.
10 changes: 10 additions & 0 deletions .mutant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
includes:
- lib
requires:
- yaml_normalizer
integration:
name: rspec
matcher:
subjects:
- YamlNormalizer*
8 changes: 3 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require:
- rubocop-performance
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.2

# Commonly used screens these days easily fit more than 80 characters.
Metrics/LineLength:
Max: 120
TargetRubyVersion: 3.0
NewCops: enable
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@ source 'https://rubygems.org'
ruby '>= 3.0'

gemspec

gem 'flog'
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'inch'
gem 'mutant-license'
gem 'mutant-rspec'
gem 'pry-byebug'
gem 'pry-doc'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rspec'
gem 'simplecov'
gem 'yard'
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# NOTE: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
Expand Down
107 changes: 61 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,30 @@ Yaml Normalizer can be used in frameworks like Rails, Sinatra or Cuba, but
it runs stand-alone as well.

## Installation
$ gem install yaml_normalizer
gem install yaml_normalizer

## Usage
### Use executable files
Yaml Normalizer provides you with two executables: `yaml_normalize` and `yaml_check`.
This is how you run them in your terminal:
To check and normalize ´a_yaml_file.yml´, run:

$ yaml_check my_yaml_file.yml
$ yaml_normalize my_yaml_file.yml
yaml_check a_yaml_file.yml
yaml_normalize a_yaml_file.yml

To check the current version of yaml_normalizer type:
$ yaml_check --version
Print the installed version of Yaml Normalizer:

or
yaml_check --version

$ yaml_normalize -v
Print help messages for ´yaml_check´ and `yaml_normalize`:

To see the help message for yaml_normalizer type:
$ yaml_check --help

or

$ yaml_normalize -h
yaml_check --help
yaml_normalize --help

### Include Yaml Normalizer rake tasks
In your Gemfile, add

gem 'yaml_normalizer', require: false

In a Rails context, you might want to only add it to `:development` and `:test` groups.

In your Rakefile, add
Expand All @@ -77,52 +73,71 @@ This will give you two additional rake task (`rake -T`)
rake yaml:check # Check if configured YAML files are normalized
rake yaml:normalize # Normalize configured YAML files


## Development

In order to start developing with Yaml Normalizer you need to install the following dependencies:
Start developing Yaml Normalizer by installing the following dependencies:
* [git](https://git-scm.com/downloads) (version control)
* [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (programming language)
* [RubyGems](https://rubygems.org/pages/download) (package manager for Ruby)
* [Bundler](http://bundler.io/) (dependency manager for RubyGems)

##### Set up Yaml Normalizer
$ git clone [email protected]:Sage/yaml_normalizer.git
$ cd yaml_normalizer/
$ bundle install
$ rake
### Set up Yaml Normalizer
git clone [email protected]:Sage/yaml_normalizer.git
cd yaml_normalizer/
bundle install

### List available rake tasks
bundle exec rake -T
```
rake ci # Continuous integration test suite (DEFAULT)
rake flog # Analyze for code complexity in: lib
rake inch # Check documentation coverage
rake mutant # Mutation testing to check mutation coverage of current RSpec test suite
rake rubocop # Run RuboCop
rake rubocop:autocorrect # Autocorrect RuboCop offenses (only when it's safe)
rake rubocop:autocorrect_all # Autocorrect RuboCop offenses (safe and unsafe)
rake spec # Run RSpec code examples
rake yard # Generate YARD Documentation
```

### Generate documentation locally using Yard
bundle exec rake yard
After generating yard documentation, open generated index.html located in `doc/` in your browser.

### Test documentation quality using Inch
bundle exec rake inch
inch list --all
This task applies static code analysis to measure documentation quality. Inch also suggests improvements.

#### Generate Documentation
##### Using Yard
$ bundle exec rake yard
After generating yard documentation, open index.html at doc/ in your browser.
### Run RSpec
bundle exec rake spec
rspec
[RSpec](https://rubygems.org/gems/rspec) is a testing framework for Ruby. Running this task executes all tests located in `spec/`.

#### Test Implementation
##### Run continuous integration test suite
$ bundle exec rake
Running this task executes the following tasks: `inch` `rubocop` `ci_flog` `ci_spec` `mutant`
### Check and Correct static code metrics using Rubocop
bundle exec rake rubocop
rubocop

#### Test Documentation using Inch
$ bundle exec rake inch
This task applies static code analysis to measure documentation quality. Inch also suggests improvements.
bundle exec rake rubocop:autocorrect
rubocop -a

##### Run Guard
$ bundle exec guard
Guard keeps track of file changes and automatically runs the unit tests related to changed files.
bundle exec rake rubocop:autocorrect_all
rubocop -A
[Rubocop](https://rubygems.org/gems/rubocop) is a static code analyzer that checks for code quality and style issues.

##### Run RSpec
$ bundle exec rake spec
This task runs the full unit test suite based on RSpec.
### Run Flog (check complexity)
bundle exec rake flog
[Flog](https://rubygems.org/gems/flog) is a static code analyzer that checks for code complexity.

#### Test Tests using mutant
$ bundle exec rake mutant
### Run Guard
bundle exec guard
[Guard](https://rubygems.org/gems/guard) keeps track of file changes and automatically runs tests related to updated files.

#### Check and Correct static code metrics using Rubocop
$ bundle exec rake rubocop
$ bundle exec rake rubocop:auto_correct
### Run continuous integration test suite
bundle exec rake
Running this task executes the following tasks: ´ci_spec´ ´inch´ ´ci_flog´ ´rubocop`

#### Run Flog (check complexity)
$ bundle exec flog -a lib
### Test Tests using mutant (requires mutant-license)
bundle exec rake mutant

## Contributing
Bug reports and pull requests are welcome on GitHub at
Expand Down
12 changes: 4 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,18 @@ end
task :ci_spec do
ci_task('CI spec', 'bundle exec rspec 2>&1') do |out, success|
out_lines = out.split("\n")
success &&= out_lines[-3].match?(/ 0 failures/)
success &&= out_lines[-3].include?(' 0 failures')
success &&= out_lines[-1].match?(/LOC \(100\.0%\) covered\.$/)
success
end
end

desc 'Mutation testing to check mutation coverage of current RSpec test suite'
task :mutant do
mutant_sh = 'bundle exec mutant \
--since 0baa016e8a89b81b35e74ff988594dc11fbd48f5 \
--include lib \
--require yaml_normalizer \
--use rspec YamlNormalizer* 2>&1'
mutant_sh = 'bundle exec mutant run 2>&1'

ci_task('mutant', mutant_sh) do |out, success|
success && out.split("\n").any? { |l| l == 'Coverage: 100.00%' }
success && out.split("\n").any?('Coverage: 100.00%')
end
end

Expand All @@ -66,6 +62,6 @@ FlogTask.new do |config|
end

desc 'Continuous integration test suite (DEFAULT)'
task ci: %i[inch rubocop ci_flog ci_spec mutant]
task ci: %i[ci_spec inch ci_flog rubocop]

task default: :ci
2 changes: 1 addition & 1 deletion lib/yaml_normalizer/ext/sort_by_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module SortByKey
# => {:a=>nil, :b=>{:x=>10, :y=>{:a=>2, :b=>1}, :z=>20}}
# @param recursive [Boolean] defines if sort_by_key is called on child
# nodes, defaults to true
def sort_by_key(recursive = true)
def sort_by_key(recursive: true)
keys.sort_by(&:to_s).each_with_object({}) do |key, seed|
value = seed[key] = fetch(key)
seed[key] = value.extend(SortByKey).sort_by_key if recursive && value.instance_of?(Hash)
Expand Down
8 changes: 5 additions & 3 deletions lib/yaml_normalizer/helpers/normalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ module Normalize
# @return [String] normalized YAML string
def normalize_yaml(yaml)
hashes = parse(yaml).transform
hashes.each { |hash| hash.extend(Ext::SortByKey) }
hashes.map(&:sort_by_key).map(&:to_yaml).join
hashes.map do |hash|
hash.extend(Ext::SortByKey)
hash.sort_by_key.to_yaml
end.join
end

private
Expand All @@ -39,7 +41,7 @@ def read(file)

def relative_path_for(file)
realpath = Pathname.new(file).realpath
realpath.relative_path_from(Pathname.new(Dir.pwd))
realpath.relative_path_from(Dir.pwd)
end
end
end
Expand Down
20 changes: 8 additions & 12 deletions lib/yaml_normalizer/helpers/param_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,30 @@ module ParamParser
def parse_params(*args)
OptionParser.new do |opts|
opts.banner = "Usage: #{program_name} [options] file1, file2..."
opts.on('-v', '--version', 'Prints the yaml_normalizer version') { print_version }
opts.on('-h', '--help', 'Prints this help') { print_help(opts) }
opts.on('-v', '--version', 'Prints the yaml_normalizer version') { print_version }
end.parse(args)
end

# Print current version of the tool
def print_version
print("#{YamlNormalizer::VERSION}\n")
exit_success
end

# Print current version of the tool
# @param [Option] opts - options of opt_parser object
# @return nil
def print_help(opts)
print(opts)
exit_success
exit
end

# Print current version of the tool
def print_version
print("#{VERSION}\n")
exit
end

private

def program_name
$PROGRAM_NAME.split('/').last
end

def exit_success
exit unless ENV['ENV'] == 'test'
end
end
end
end
31 changes: 15 additions & 16 deletions lib/yaml_normalizer/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@
module YamlNormalizer
# Provides Rake task integration
class RakeTask < ::Rake::TaskLib
# The name of the task
# @return [String] name of the Rake task
# The name of the rake task
# @return [String] name of the rake task
attr_accessor :name

# The YAML files to process.
# @example Task files assignment
# @example
# YamlNormalizer::RakeTask.new do |task|
# task.files = ['config/locale/*.yml', 'config/*.yml']
# end
# @return [Array<String>] a list of file globing Strings
attr_accessor :files

# Create a YamlNormalizer rake task object.
# Use this to
# @example
# In your Rakefile, add:
# YamlNormalizer::RakeTask.new
# Add YAML Normalizer rake tasks to your project by adding the following line to your Rakefile:
# YamlNormalizer::RakeTask.new
#
# To be more specific, configure YAML Normalizer's mode and files like so:
# YamlNormalizer::RakeTask.new do |config|
# config.files = Dir[File.join(File.dirname(__FILE__), 'include.yml')]
# end
# This gives you the following tasks (run rake -T)
# rake yaml:check # Check if configured YAML files are normalized
# rake yaml:normalize # Normalize configured YAML files
#
# @example Configure YAML Normalizer's rake task
# YamlNormalizer::RakeTask.new do |config|
# config.files = Dir[File.join(File.dirname(__FILE__), 'include.yml')]
# end
#
# This gives you the following tasks (run rake -T)
# rake yaml:check # Check if given YAML are normalized
# rake yaml:normalize # Normalize given YAML files
# @param name [String] name of the Rake task
# @param name [String] name of the rake task
# @param &block [Proc] optional, evaluated inside the task definition
def initialize(name = 'yaml', &block)
super()
yield(self) if block

desc 'Check if configured YAML files are normalized'
Expand Down
3 changes: 2 additions & 1 deletion lib/yaml_normalizer/services/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Check < Base
# more Strings that are interpreted as file glob pattern.
# @param *args [Array<String>] a list of file glob patterns
def initialize(*args)
super
parse_params(*args)
files = args.each_with_object([]) { |a, o| o << Dir[a.to_s] }
@files = files.flatten.sort.uniq
Expand All @@ -37,7 +38,7 @@ def call
def process(file)
return true if IsYaml.call(file) && normalized?(file)

$stderr.print "#{file} not a YAML file\n"
$stderr.print "#{file} is not a YAML file\n"
nil
end

Expand Down
1 change: 1 addition & 0 deletions lib/yaml_normalizer/services/is_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class IsYaml < Base
# String.
# @param file [String] file path to be regarded
def initialize(file)
super
@file = file.to_s
end

Expand Down
Loading

0 comments on commit 65d352a

Please sign in to comment.