Skip to content

Commit

Permalink
Merge b14a921 into 16ddc4b
Browse files Browse the repository at this point in the history
  • Loading branch information
tomeon committed Oct 22, 2018
2 parents 16ddc4b + b14a921 commit 5c58afe
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 20

Metrics/ModuleLength:
Max: 150

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## Unreleased ([changes](https://github.com/infertux/bashcov/compare/v1.8.2...master))

* TBD
* [FEATURE] Add `--prefilter` flag and corresponding `Bashcov.prefilter`
setting. Using this flag causes Bashcov to omit from the coverage
results any uncovered files that match one or more of the filters
in `SimpleCov.filters`.
* [FEATURE] Ensure that files matching the `SimpleCov.tracked_files` glob
pattern are included in the coverage results, regardless of
whether `Bashcov.skip_uncovered` is enabled.

## v1.8.2, 2018-03-27 ([changes](https://github.com/infertux/bashcov/compare/v1.8.1...v1.8.2))

Expand Down
6 changes: 5 additions & 1 deletion lib/bashcov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Bashcov

# A +Struct+ to store Bashcov configuration
Options = Struct.new(
*%i[skip_uncovered mute bash_path root_directory command command_name]
*%i[skip_uncovered prefilter mute bash_path root_directory command command_name]
)

class << self
Expand Down Expand Up @@ -75,6 +75,7 @@ def set_default_options!
@options = Options.new

@options.skip_uncovered = false
@options.prefilter = false
@options.mute = false
@options.bash_path = "/bin/bash"
@options.root_directory = Dir.getwd
Expand Down Expand Up @@ -115,6 +116,9 @@ def option_parser # rubocop:disable Metrics/MethodLength
opts.on("-s", "--skip-uncovered", "Do not report uncovered files") do |s|
options.skip_uncovered = s
end
opts.on("-p", "--prefilter", "Exclude uncovered files that match SimpleCov filters") do |s|
options.prefilter = s
end
opts.on("-m", "--mute", "Do not print script output") do |m|
options.mute = m
end
Expand Down
35 changes: 32 additions & 3 deletions lib/bashcov/runner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "simplecov"

require "bashcov/detective"
require "bashcov/errors"
require "bashcov/field_stream"
Expand Down Expand Up @@ -126,15 +128,42 @@ def with_xtrace_flag
# Add files which have not been executed at all (i.e. with no coverage)
# @return [void]
def find_bash_files!
return if Bashcov.skip_uncovered

Pathname.new(Bashcov.root_directory).find do |filename|
filtered_files.each do |filename|
if !@coverage.include?(filename) && @detective.shellscript?(filename)
@coverage[filename] = []
end
end
end

# @return [Array<Pathname>] the list of files that should be included in
# coverage results, unless filtered by one or more SimpleCov filters
def tracked_files
return @tracked_files if defined? @tracked_files

mandatory = SimpleCov.tracked_files ? Pathname.glob(SimpleCov.tracked_files) : []
under_root = Bashcov.skip_uncovered ? [] : Pathname.new(Bashcov.root_directory).find.to_a

@tracked_files = mandatory | under_root
end

# @return [Array<Pathname>] the list of files that should be included in
# coverage results
def filtered_files
return @filtered_files if defined? @filtered_files

return @filtered_files = tracked_files unless Bashcov.prefilter

source_files = tracked_files.map do |file|
SimpleCov::SourceFile.new(file.to_s, @coverage.fetch(file, []))
end

source_file_to_tracked_file = Hash[source_files.zip(tracked_files)]

@filtered_files = SimpleCov.filtered(source_files).map do |source_file|
source_file_to_tracked_file[source_file]
end
end

# @return [void]
def expunge_invalid_files!
@coverage.each_key do |filename|
Expand Down
34 changes: 34 additions & 0 deletions spec/bashcov/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@
runner.run
expect(runner.result.keys & uncovered_files).to be_empty
end

context "when SimpleCov.tracked_files is defined" do
it "includes matching files even if they are uncovered" do
expect(SimpleCov).to receive(:tracked_files).at_least(:once).and_return(uncovered_files.first)
runner.run
expect(runner.result.keys & uncovered_files).to contain_exactly(*uncovered_files.first)
end
end
end

context "with mute = true" do
Expand All @@ -219,5 +227,31 @@
runner.run
end
end

context "with prefilter = true" do
before do
Bashcov.prefilter = true
end

around do |example|
orig_filters = SimpleCov.filters

SimpleCov.filters = []

SimpleCov.configure do
expected_omitted.each_key { |filter| add_filter(filter) }
end

example.run

SimpleCov.filters = orig_filters
end

it "omits files matching one or more SimpleCov filters from the results hash" do
runner.run
result = runner.result
expect(result.keys).to contain_exactly(*(expected_coverage.keys - expected_omitted.values.flatten))
end
end
end
end
3 changes: 2 additions & 1 deletion spec/bashcov/xtrace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def case_result

context "when shell expansion triggers subshell execution" do
it "causes extra hits to be reported" do
allow(Bashcov).to receive(:skip_uncovered).at_least(:once).and_return(true)

result_without_subshell = case_result

allow(Bashcov).to receive(:skip_uncovered).and_return(true)
allow(Bashcov::Xtrace).to receive(:ps4).and_return(subshell_ps4)

result_with_subshell = case_result
Expand Down
11 changes: 10 additions & 1 deletion spec/bashcov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

describe Bashcov do
it "preserves the exit status" do
system("./bin/bashcov ./spec/test_app/scripts/exit_non_zero.sh")
system("./bin/bashcov --root ./spec/test_app ./spec/test_app/scripts/exit_non_zero.sh")
expect($?.exitstatus).not_to eq(0)
end

Expand Down Expand Up @@ -67,6 +67,15 @@
end
end

context "with the --prefilter flag" do
before { @args << "--prefilter" }

it "sets it properly" do
subject
expect(Bashcov.prefilter).to be true
end
end

context "with the --mute flag" do
before { @args << "--mute" }

Expand Down
20 changes: 20 additions & 0 deletions spec/support/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,23 @@ def expected_missing
"#{test_app}/scripts/no_extension/usr_local_bin_gawk",
]
end

def expected_omitted
{
->(source_file) { File.basename(File.dirname(source_file.project_filename)) != "scripts" } => [
"#{test_app}/never_called.sh",
],
/multiline(?:\d)*/ => [
"#{test_app}/multiline.sh",
"#{test_app}/multiline2.sh",
"#{test_app}/multiline3.sh",
],
"/no_extension/" => [
"#{test_app}/scripts/no_extension/bin_bash",
"#{test_app}/scripts/no_extension/bin_bash_with_args",
"#{test_app}/scripts/no_extension/bin_dash",
"#{test_app}/scripts/no_extension/bin_sh",
"#{test_app}/scripts/no_extension/usr_bin_env_bash",
],
}
end

0 comments on commit 5c58afe

Please sign in to comment.