Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

[34] Code coverage config in spec helper #42

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# master

* Add default configuration for simplecov in spec_helper.rb

* Display an error message if bundler failed to run or is not present in the system

* Juan Manuel Ramallo*
*Juan Manuel Ramallo*

# 0.1.3

Expand Down
1 change: 1 addition & 0 deletions lib/rsgem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'rsgem/tasks/bundle_dependencies'
require 'rsgem/tasks/run_rubocop'
require 'rsgem/tasks/set_bundled_files'
require 'rsgem/tasks/simple_cov_post_install'
require 'rsgem/dependencies/base'
require 'rsgem/dependencies/rake'
require 'rsgem/dependencies/reek'
Expand Down
4 changes: 4 additions & 0 deletions lib/rsgem/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ def gitignore_path
def rakefile_path
"#{folder_path}/Rakefile"
end

def spec_helper_path
"#{folder_path}/spec/spec_helper.rb"
end
end
end
16 changes: 10 additions & 6 deletions lib/rsgem/dependencies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
module RSGem
module Dependencies
class Base
attr_reader :config_file_destination, :config_file_source, :mode, :name, :version
attr_reader :config_file_destination, :config_file_source, :mode, :name, :post_install_task,
:version

def initialize(config_file_source: nil, config_file_destination: nil, mode: :development,
name:, version: nil)
@config_file_source = config_file_source
@config_file_destination = config_file_destination
@mode = mode # Either `development' or `runtime'
def initialize(name:, **args)
@config_file_source = args[:config_file_source]
@config_file_destination = args[:config_file_destination]
@mode = args[:mode] || 'development' # Either `development' or `runtime'
@name = name
@post_install_task = args[:post_install_task]
version = args[:version]
@version = version ? "'#{version}'" : nil
end

Expand All @@ -21,6 +23,8 @@ def install(context)
end
end

post_install_task&.new(context: context)&.perform

puts "\t#{name.capitalize} installed"
end

Expand Down
3 changes: 2 additions & 1 deletion lib/rsgem/dependencies/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module RSGem
module Dependencies
# This is the latest stable version working correctly with codeclimate
# 0.18+ does not work currently https://github.com/codeclimate/test-reporter/issues/413
Simplecov = Base.new(name: 'simplecov', version: '~> 0.17.1')
Simplecov = Base.new(name: 'simplecov', version: '~> 0.17.1',
post_install_task: RSGem::Tasks::SimpleCovPostInstall)
end
end
2 changes: 1 addition & 1 deletion lib/rsgem/tasks/create_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def message

def shell_command
[
"bundle gem #{context.gem_name}",
"bundle gem #{context.gem_name} --test=rspec --coc --mit",
bundler_options
].compact.join(' ')
end
Expand Down
37 changes: 37 additions & 0 deletions lib/rsgem/tasks/simple_cov_post_install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module RSGem
module Tasks
class SimpleCovPostInstall < Base
def perform
spec_helper.sub!("require \"#{gem_name}\"", <<~RUBY)
require 'simplecov'

SimpleCov.start do
add_filter '/spec/'
end

require '#{gem_name}'
RUBY

write
end

private

def gem_name
context.gem_name
end

def spec_helper
@spec_helper ||= File.read(context.spec_helper_path)
end

def write
File.open(context.spec_helper_path, 'w') do |file|
file.puts spec_helper
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
let(:rakefile) { File.read("./#{gem_name}/Rakefile") }
let(:travis) { File.read("./#{gem_name}/.travis.yml") }
let(:expected_travis) { File.read('./lib/rsgem/support/travis.yml') }
let(:spec_helper) { File.read("./#{gem_name}/spec/spec_helper.rb") }

it 'creates a new folder' do
expect(list_files).to include gem_name
Expand Down Expand Up @@ -96,6 +97,10 @@
expect(File.exist?("./#{gem_name}/.github/workflows")).to eq false
end

it 'adds simplecov configuration in spec helper file' do
expect(spec_helper).to include 'SimpleCov.start do'
end

context 'running inside the new gem' do
before { Dir.chdir(gem_name) }
after { Dir.chdir('../') }
Expand Down