Skip to content

Commit

Permalink
Merge pull request #50 from Sage/versioning_info
Browse files Browse the repository at this point in the history
[yaml_normalizer] Added terminal methods for more informative CLI access
  • Loading branch information
JanTaras29 authored Aug 8, 2019
2 parents b0cf0cd + 518ef25 commit d764909
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ require:

AllCops:
TargetRubyVersion: 2.5

# Commonly used screens these days easily fit more than 80 characters.
Metrics/LineLength:
Max: 120
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Removed

## [1.2.0] - 2019-08-08
### Added
- Versioning and help messages for CLI

### Changed
- Extended allowed line length to 120
- Change the mutant to only scan the latest commits


## [1.1.0] - 2019-05-21
### Added
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ This is how you run them in your terminal:
$ yaml_check my_yaml_file.yml
$ yaml_normalize my_yaml_file.yml

To check the current version of yaml_normalizer type:
$ yaml_check --version

or

$ yaml_normalize -v

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

or

$ yaml_normalize -h

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

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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'
Expand Down
4 changes: 1 addition & 3 deletions lib/yaml_normalizer/ext/sort_by_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ module SortByKey
def sort_by_key(recursive = true)
keys.sort_by(&:to_s).each_with_object({}) do |key, seed|
value = seed[key] = fetch(key)
if recursive && value.instance_of?(Hash)
seed[key] = value.extend(SortByKey).sort_by_key
end
seed[key] = value.extend(SortByKey).sort_by_key if recursive && value.instance_of?(Hash)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/yaml_normalizer/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ module Helpers
end

require 'yaml_normalizer/helpers/normalize'
require 'yaml_normalizer/helpers/param_parser'
45 changes: 45 additions & 0 deletions lib/yaml_normalizer/helpers/param_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'optparse'

module YamlNormalizer
module Helpers
# Methods handling passing of additional params from CLI
module ParamParser
# Parse the params provided to the service
# @param [Array] args - params passed to the service
# @return nil
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) }
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
end

private

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

def exit_success
exit unless ENV['ENV'] == 'test'
end
end
end
end
2 changes: 2 additions & 0 deletions lib/yaml_normalizer/services/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Services
# result = check.call
class Check < Base
include Helpers::Normalize
include Helpers::ParamParser

# files is a sorted array of file path Strings
attr_reader :files
Expand All @@ -19,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)
parse_params(*args)
files = args.each_with_object([]) { |a, o| o << Dir[a.to_s] }
@files = files.flatten.sort.uniq
end
Expand Down
2 changes: 2 additions & 0 deletions lib/yaml_normalizer/services/normalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Services
# result = normalize.call
class Normalize < Base
include Helpers::Normalize
include Helpers::ParamParser

# files is a sorted array of file path Strings
attr_reader :files
Expand All @@ -19,6 +20,7 @@ class Normalize < Base
# more String that are interpreted as file glob pattern.
# @param *args [Array<String>] a list of file glob patterns
def initialize(*args)
parse_params(*args)
files = args.each_with_object([]) { |a, o| o << Dir[a.to_s] }
@files = files.flatten.sort.uniq
end
Expand Down
2 changes: 1 addition & 1 deletion lib/yaml_normalizer/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module YamlNormalizer
# The current Yaml Normalizer version
VERSION = '1.1.0'
VERSION = '1.2.0'
end
2 changes: 2 additions & 0 deletions spec/services/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
let(:path) { "#{SpecConfig.data_path}#{File::SEPARATOR}" }
let(:args) { ["#{path}#{name}"] }

include_examples :args_receving_service

context 'parially invalid globbing inputs' do
subject { described_class.new(*args) }
let(:args) { ["#{path}*.1", :invalid, "#{path}1.*"] }
Expand Down
2 changes: 2 additions & 0 deletions spec/services/normalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
let(:path) { "#{SpecConfig.data_path}#{File::SEPARATOR}" }
let(:args) { ["#{path}#{file}"] }

include_examples :args_receving_service

context 'invalid args, no arg matches file' do
subject { described_class.new(*args).call }
let(:args) { ['lol', :foo, nil] }
Expand Down
32 changes: 32 additions & 0 deletions spec/shared_examples/param_parser_shared_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

shared_examples :args_receving_service do
describe 'param triggered methods' do
subject { described_class.new(*args) }

context 'when presented with version param' do
['-v', '--version'].each do |param|
let(:args) { [param] }

it 'prints the version of software' do
expect do
subject.call
end.to output("#{YamlNormalizer::VERSION}\n").to_stdout
end
end
end

context 'when presented with help param' do
['-h', '--help'].each do |param|
let(:args) { [param] }
it 'prints the help message' do
expect do
subject.call
end.to output("Usage: rspec [options] file1, file2...\n"\
" -v, --version Prints the yaml_normalizer version\n"\
" -h, --help Prints this help\n").to_stdout
end
end
end
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require './spec/ci_helper'

ENV['ENV'] = 'test'
unless defined?(Mutant)
require 'simplecov'
SimpleCov.start do
Expand All @@ -13,6 +14,12 @@

require 'yaml_normalizer'

Dir.foreach(File.join(File.dirname(__FILE__), 'shared_examples')) do |f|
next if ['.', '..'].include?(f)

require File.join(File.dirname(__FILE__), 'shared_examples', f)
end

# Configure unit test suite
module SpecConfig
module_function
Expand Down

0 comments on commit d764909

Please sign in to comment.