Skip to content

Commit

Permalink
Pretty output (#230)
Browse files Browse the repository at this point in the history
* colorize CLI

* unit tests fix, rubocop

* cucumber features fix

* changed approach to colorized string

* private methods changes
  • Loading branch information
alexey-voronenko committed Jul 3, 2017
1 parent dc9557e commit 9e48cbf
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 93 deletions.
13 changes: 7 additions & 6 deletions bin/howitzer
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
require 'colorized_string'
require 'gli'
require_relative '../lib/howitzer/version'

Expand Down Expand Up @@ -26,9 +27,9 @@ module HowitzerCli
validate_options(options)
load_generators
path_to_dir = File.join(Dir.pwd, args.first)
puts ' * New project directory creation ...'
puts ColorizedString.new(' * New project directory creation ...').light_cyan
Dir.mkdir(path_to_dir)
puts " Created new './#{args.first}' folder"
puts " #{ColorizedString.new('Created').light_green} './#{args.first}' folder"
Dir.chdir(path_to_dir)
Howitzer::ConfigGenerator.new(options)
Howitzer::WebGenerator.new(options)
Expand All @@ -43,9 +44,9 @@ module HowitzerCli
elsif options['turnip']
Howitzer::TurnipGenerator.new(options)
end
puts '[WARN] Extra parameters were skipped' if args.size > 1
puts ColorizedString.new('[WARN] Extra parameters were skipped').yellow if args.size > 1
elsif args.size.zero?
exit_now!('Please specify <PROJECT NAME>', 64)
exit_now!(ColorizedString.new('Please specify <PROJECT NAME>').red, 64)
end
end
end
Expand Down Expand Up @@ -80,11 +81,11 @@ module HowitzerCli
def validate_options(options)
return if [options[:cucumber], options[:rspec], options[:turnip]].count { |el| el } == 1

exit_now!('Provide --cucumber, --rspec or --turnip option', 64)
exit_now!(ColorizedString.new('Provide --cucumber, --rspec or --turnip option').red, 64)
end

def check_project_presence
exit_now!('Current directory is not Howitzer project', 126) unless howitzer_project?
exit_now!(ColorizedString.new('Current directory is not Howitzer project').red, 126) unless howitzer_project?
end

def howitzer_project?
Expand Down
8 changes: 4 additions & 4 deletions features/cli_new.feature
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Feature: Howitzer CLI New Project Creation
Then the output should contain exactly:
"""
* New project directory creation ...
Created new './test_automation' folder
Created './test_automation' folder
* Config files generation ...
Added 'config/boot.rb' file
Added 'config/custom.yml' file
Expand Down Expand Up @@ -115,7 +115,7 @@ Feature: Howitzer CLI New Project Creation
Then the output should contain exactly:
"""
* New project directory creation ...
Created new './test_automation' folder
Created './test_automation' folder
* Config files generation ...
Added 'config/boot.rb' file
Added 'config/custom.yml' file
Expand Down Expand Up @@ -183,7 +183,7 @@ Feature: Howitzer CLI New Project Creation
Then the output should contain exactly:
"""
* New project directory creation ...
Created new './test_automation' folder
Created './test_automation' folder
* Config files generation ...
Added 'config/boot.rb' file
Added 'config/custom.yml' file
Expand Down Expand Up @@ -293,7 +293,7 @@ Feature: Howitzer CLI New Project Creation
Then the output should contain exactly:
"""
* New project directory creation ...
Created new './test_automation' folder
Created './test_automation' folder
* Config files generation ...
Added 'config/boot.rb' file
Added 'config/custom.yml' file
Expand Down
43 changes: 28 additions & 15 deletions generators/base_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'colorized_string'
require 'fileutils'
require 'erb'
require 'ostruct'
Expand Down Expand Up @@ -29,7 +30,7 @@ def destination
end

def print_banner
logger.puts banner unless banner.empty?
logger.puts ColorizedString.new(banner.chomp).light_cyan unless banner.empty?
end

def print_info(data)
Expand All @@ -41,7 +42,7 @@ def puts_info(data)
end

def puts_error(data)
logger.puts " ERROR: #{data}"
logger.puts ColorizedString.new(" ERROR: #{data}").red
end
end

Expand Down Expand Up @@ -88,7 +89,8 @@ def copy_templates(list)
copy_templates_file_exist(data, destination_path, source_path)
else
write_template(destination_path, source_path)
puts_info "Added template '#{data[:source]}' with params '#{@options}' to destination '#{data[:destination]}'"
puts_info "#{ColorizedString.new('Added').light_green} template '#{data[:source]}' with"\
" params '#{@options}' to destination '#{data[:destination]}'"
end
end
end
Expand All @@ -103,14 +105,13 @@ def dest_path(path)
end

def copy_with_path(data)
src = source_path(data[:source])
dst = dest_path(data[:destination])
src, dst = get_source_and_destination(data)
FileUtils.mkdir_p(File.dirname(dst))
if File.exist?(dst)
copy_with_path_file_exist(data, src, dst)
else
FileUtils.cp(src, dst)
puts_info("Added '#{data[:destination]}' file")
puts_info("#{ColorizedString.new('Added').light_green} '#{data[:destination]}' file")
end
rescue => e
puts_error("Impossible to create '#{data[:destination]}' file. Reason: #{e.message}")
Expand All @@ -124,18 +125,30 @@ def write_template(dest_path, source_path)

private

def get_source_and_destination(data)
[source_path(data[:source]), dest_path(data[:destination])]
end

def conflict_file_msg(data)
ColorizedString.new("Conflict with '#{data[:destination]}' file").yellow
end

def overwrite_file_msg(data)
ColorizedString.new(" Overwrite '#{data[:destination]}' file? [Yn]:").yellow
end

def copy_templates_file_exist(data, destination_path, source_path)
puts_info("Conflict with '#{data[:destination]}' template")
print_info(" Overwrite '#{data[:destination]}' template? [Yn]:")
puts_info(ColorizedString.new("Conflict with '#{data[:destination]}' template").yellow)
print_info(ColorizedString.new(" Overwrite '#{data[:destination]}' template? [Yn]:").yellow)
copy_templates_overwrite(gets.strip.downcase, data, destination_path, source_path)
end

def copy_with_path_file_exist(data, source, destination)
if FileUtils.identical?(source, destination)
puts_info("Identical '#{data[:destination]}' file")
puts_info("#{ColorizedString.new('Identical').light_green} '#{data[:destination]}' file")
else
puts_info("Conflict with '#{data[:destination]}' file")
print_info(" Overwrite '#{data[:destination]}' file? [Yn]:")
puts_info(conflict_file_msg(data))
print_info(overwrite_file_msg(data))
copy_with_path_overwrite(gets.strip.downcase, data, source, destination)
end
end
Expand All @@ -144,9 +157,9 @@ def copy_templates_overwrite(answer, data, destination_path, source_path)
case answer
when 'y'
write_template(destination_path, source_path)
puts_info(" Forced '#{data[:destination]}' template")
puts_info(" #{ColorizedString.new('Forced').light_green} '#{data[:destination]}' template")
when 'n'
puts_info(" Skipped '#{data[:destination]}' template")
puts_info(" #{ColorizedString.new('Skipped').light_black} '#{data[:destination]}' template")
else nil
end
end
Expand All @@ -155,9 +168,9 @@ def copy_with_path_overwrite(answer, data, source, destination)
case answer
when 'y'
FileUtils.cp(source, destination)
puts_info(" Forced '#{data[:destination]}' file")
puts_info(" #{ColorizedString.new('Forced').light_green} '#{data[:destination]}' file")
when 'n' then
puts_info(" Skipped '#{data[:destination]}' file")
puts_info(" #{ColorizedString.new('Skipped').light_black} '#{data[:destination]}' file")
else nil
end
end
Expand Down
1 change: 1 addition & 0 deletions howitzer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'rspec-wait'
gem.add_runtime_dependency 'selenium-webdriver', ['>= 3.4.1', '< 4.0']
gem.add_runtime_dependency 'sexy_settings'
gem.add_runtime_dependency 'colorize'

gem.add_development_dependency('aruba')
gem.add_development_dependency('ffaker')
Expand Down
35 changes: 23 additions & 12 deletions spec/unit/generators/base_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@
after { subject }
context 'when destination file exists' do
before { allow(File).to receive(:exist?).with(destination_path) { true } }
it { expect(generator).to receive(:puts_info).with("Conflict with '#{list.first[:destination]}' template").once }
it do
expect(generator).to receive(:puts_info).with(
ColorizedString.new("Conflict with '#{list.first[:destination]}' template").yellow
).once
end
it do
expect(generator).to receive(:print_info).with(
" Overwrite '#{list.first[:destination]}' template? [Yn]:"
ColorizedString.new(" Overwrite '#{list.first[:destination]}' template? [Yn]:").yellow
).once
end
context 'and answer is yes' do
Expand Down Expand Up @@ -152,7 +156,7 @@
after { subject }
context 'when banner present' do
let(:banner) { 'banner' }
it { expect(described_class.logger).to receive(:puts).with(banner).twice }
it { expect(described_class.logger).to receive(:puts).with(ColorizedString.new(banner.chomp).light_cyan).twice }
end
context 'when banner blank' do
let(:banner) { '' }
Expand All @@ -178,7 +182,7 @@
subject { described_class.new({}).send(:puts_error, 'data') }
before { allow_any_instance_of(described_class).to receive(:print_banner) { nil } }
after { subject }
it { expect(described_class.logger).to receive(:puts).with(' ERROR: data') }
it { expect(described_class.logger).to receive(:puts).with(ColorizedString.new(' ERROR: data').red) }
end

describe '#source_path' do
Expand Down Expand Up @@ -212,39 +216,46 @@
before { allow(File).to receive(:exist?).with(dst) { true } }
context 'when identical with source file' do
before { allow(FileUtils).to receive(:identical?).with(src, dst) { true } }
it { expect(generator).to receive(:puts_info).with("Identical 'd.txt' file").once }
it do
expect(generator).to receive(:puts_info).with("#{ColorizedString.new('Identical').light_green}"\
" 'd.txt' file").once
end
end
context 'when not identical with source file' do
before do
allow(FileUtils).to receive(:identical?).with(src, dst) { false }
expect(generator).to receive(:puts_info).with("Conflict with 'd.txt' file")
expect(generator).to receive(:print_info).with(" Overwrite 'd.txt' file? [Yn]:")
expect(generator).to receive(:puts_info).with(ColorizedString.new("Conflict with 'd.txt' file").yellow)
expect(generator).to receive(:print_info).with(ColorizedString.new(" Overwrite 'd.txt' file? [Yn]:").yellow)
end
context 'when user typed Y' do
before { allow(generator).to receive(:gets) { 'Y' } }
it do
expect(FileUtils).to receive(:cp).with(src, dst) { nil }.once
expect(generator).to receive(:puts_info).with(" Forced 'd.txt' file")
expect(generator).to receive(:puts_info).with(" #{ColorizedString.new('Forced').light_green}"\
" 'd.txt' file")
end
end
context 'when user typed y' do
before { allow(generator).to receive(:gets) { 'y' } }
it do
expect(FileUtils).to receive(:cp).with(src, dst) { nil }.once
expect(generator).to receive(:puts_info).with(" Forced 'd.txt' file")
expect(generator).to receive(:puts_info).with(" #{ColorizedString.new('Forced').light_green}"\
" 'd.txt' file")
end
end
context 'when user typed N' do
before { allow(generator).to receive(:gets) { 'N' } }
it do
expect(generator).to receive(:puts_info).with(" Skipped 'd.txt' file")
expect(generator).to receive(:puts_info).with(" #{ColorizedString.new('Skipped').light_black}"\
" 'd.txt' file")
expect(FileUtils).not_to receive(:cp)
end
end
context 'when user typed n' do
before { allow(generator).to receive(:gets) { 'n' } }
it do
expect(generator).to receive(:puts_info).with(" Skipped 'd.txt' file")
expect(generator).to receive(:puts_info).with(" #{ColorizedString.new('Skipped').light_black}"\
" 'd.txt' file")
expect(FileUtils).not_to receive(:cp)
end
end
Expand All @@ -260,7 +271,7 @@
context 'when destination file missing' do
before { allow(File).to receive(:exist?).with(dst) { false } }
it do
expect(generator).to receive(:puts_info).with("Added 'd.txt' file")
expect(generator).to receive(:puts_info).with("#{ColorizedString.new('Added').light_green} 'd.txt' file")
expect(FileUtils).to receive(:cp).with(src, dst).once
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/generators/config_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
it { is_expected.to eql(expected_result) }
describe 'output' do
let(:expected_output) do
" * Config files generation ...
Added 'config/boot.rb' file
Added 'config/custom.yml' file
Added 'config/capybara.rb' file
Added 'config/default.yml' file\n"
"#{ColorizedString.new(' * Config files generation ...').light_cyan}
#{ColorizedString.new('Added').light_green} 'config/boot.rb' file
#{ColorizedString.new('Added').light_green} 'config/custom.yml' file
#{ColorizedString.new('Added').light_green} 'config/capybara.rb' file
#{ColorizedString.new('Added').light_green} 'config/default.yml' file\n"
end
subject { output.string }
it { is_expected.to eql(expected_output) }
Expand Down
16 changes: 8 additions & 8 deletions spec/unit/generators/cucumber_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
it { is_expected.to eql(expected_result) }
describe 'output' do
let(:expected_output) do
" * Cucumber integration to the framework ...
Added 'features/step_definitions/common_steps.rb' file
Added 'features/support/env.rb' file
Added 'features/support/hooks.rb' file
Added 'features/support/transformers.rb' file
Added 'features/example.feature' file
Added 'tasks/cucumber.rake' file
Added 'tasks/cuke_sniffer.rake' file\n"
"#{ColorizedString.new(' * Cucumber integration to the framework ...').light_cyan}
#{ColorizedString.new('Added').light_green} 'features/step_definitions/common_steps.rb' file
#{ColorizedString.new('Added').light_green} 'features/support/env.rb' file
#{ColorizedString.new('Added').light_green} 'features/support/hooks.rb' file
#{ColorizedString.new('Added').light_green} 'features/support/transformers.rb' file
#{ColorizedString.new('Added').light_green} 'features/example.feature' file
#{ColorizedString.new('Added').light_green} 'tasks/cucumber.rake' file
#{ColorizedString.new('Added').light_green} 'tasks/cuke_sniffer.rake' file\n"
end
subject { output.string }
it { is_expected.to eql(expected_output) }
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/generators/emails_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
it { is_expected.to eql(expected_result) }
describe 'output' do
let(:expected_output) do
" * Email example generation ...
Added '/emails/example_email.rb' file\n"
"#{ColorizedString.new(' * Email example generation ...').light_cyan}
#{ColorizedString.new('Added').light_green} '/emails/example_email.rb' file\n"
end
subject { output.string }
it { is_expected.to eql(expected_output) }
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/generators/prerequisites_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
it { is_expected.to eql(expected_result) }
describe 'output' do
let(:expected_output) do
" * Pre-requisites integration to the framework ...
Added 'prerequisites/factory_girl.rb' file
Added 'prerequisites/factories/users.rb' file
Added 'prerequisites/models/base.rb' file
Added 'prerequisites/models/user.rb' file\n"
"#{ColorizedString.new(' * Pre-requisites integration to the framework ...').light_cyan}
#{ColorizedString.new('Added').light_green} 'prerequisites/factory_girl.rb' file
#{ColorizedString.new('Added').light_green} 'prerequisites/factories/users.rb' file
#{ColorizedString.new('Added').light_green} 'prerequisites/models/base.rb' file
#{ColorizedString.new('Added').light_green} 'prerequisites/models/user.rb' file\n"
end
subject { output.string }
it { is_expected.to eql(expected_output) }
Expand Down
Loading

0 comments on commit 9e48cbf

Please sign in to comment.