Skip to content

allow to redefine root directory, merge suites and report to external systems #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 27 additions & 2 deletions bin/bashcov
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,33 @@ coverage = runner.result

require "simplecov"

SimpleCov.command_name Bashcov.name
SimpleCov.coverage_dir(ENV["COVERAGE_DIR"]) if ENV["COVERAGE_DIR"]

if ENV["COVERAGE_NAME"]
SimpleCov.command_name ENV["COVERAGE_NAME"]
else
SimpleCov.command_name Bashcov.name
end

reports = Bashcov.options.reports
unless reports.empty?
formatters = [
SimpleCov::Formatter::HTMLFormatter
]
reports.each do |r|
require r[:require]
formatters.push(r[:formatter].split("::").inject(Object) { |a, e| a.const_get e })
end
SimpleCov.formatters = formatters
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to reimplement that in Bashcov. I just tried this and it builds fine on Travis. However it doesn't seem to push coverage results even with your original code. So I must have misconfigured Coveralls somehow. Could you try something along the lines of this in your own repo and see if that works?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #13 (diff) redefine all formatters. That's why for example doesn't work. If it's possible I would like to have both approach. git-ignore needs .simplecov because of use of test suite sharness, but in general people should use bashcov as another bash script. Anyway, I can keep this on my own fork till it's mature. I understand your are reluctant to add this

SimpleCov.root Bashcov.root_directory
SimpleCov::Result.new(coverage).format!

result = SimpleCov::Result.new(coverage)
if SimpleCov.use_merging
SimpleCov::ResultMerger.store_result(result) if result
result = SimpleCov::ResultMerger.merged_result
end

result.format!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to add that into an at_exit block in your .simplecov file: https://github.com/colszowka/simplecov#customizing-exit-behaviour


exit status.exitstatus
13 changes: 11 additions & 2 deletions lib/bashcov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class << self
attr_reader :options

# @return [String] The project's root directory
def root_directory
@root_directory ||= Dir.pwd
def root_directory(root = nil)
return @root_directory if defined?(@root_directory) && root.nil?
@root_directory = root || Dir.pwd
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that seems overly complicated - what about attr_writter :root_directory?

end

# Sets default options overriding any existing ones.
Expand All @@ -24,6 +25,7 @@ def set_default_options!
@options ||= OpenStruct.new
@options.skip_uncovered = false
@options.mute = false
@options.reports = []
end

# Parses the given CLI arguments and sets +options+.
Expand Down Expand Up @@ -72,6 +74,13 @@ def option_parser
opts.on("-m", "--mute", "Do not print script output") do |m|
@options.mute = m
end
opts.on("-r rep", "--report rep", String, "Report system integration. <require>:<formatter>") do |rep|
sp = rep.split(":", 2)
report = {}
report[:require] = sp[0]
report[:formatter] = sp[1]
@options.reports.push(report)
end

opts.separator "\nCommon options:"

Expand Down
15 changes: 15 additions & 0 deletions lib/bashcov/xtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ def read
@files[filename] ||= []
@files[filename][lineno] ||= 0
@files[filename][lineno] += 1
line = File.readlines(filename)[lineno]
ishere = line.match("cat.*<<-?\s*[\"']?\(.*\)[\"']?")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use heredoc with something else than cat (see http://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash for example) but I like that idea, it just needs to be more generic.

next unless ishere
lineno += 1
line = File.readlines(filename)[lineno]
until line.nil? || line.match("^\s*#{ishere[1]}")
@files[filename][lineno] ||= 0
@files[filename][lineno] += 1
lineno += 1
line = File.readlines(filename)[lineno]
end
unless line.nil?
@files[filename][lineno] ||= 0
@files[filename][lineno] += 1
end
end

@files
Expand Down
9 changes: 9 additions & 0 deletions spec/bashcov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
}
end
end

context "with the --report option" do
before { @args << "--report" << "req:Module::Formatter" }

it "sets it properly" do
subject
expect(Bashcov.options.reports).to eq([{ require: "req", formatter: "Module::Formatter" }])
end
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/support/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def expected_coverage
{
"#{test_app}/never_called.sh" => [nil, nil, 0],
"#{test_app}/scripts/case.sh" => [nil, nil, nil, 6, 1, nil, 0, 0, 1, nil, nil, nil, 1, 1],
"#{test_app}/scripts/delete.sh" => [nil, nil, 1, nil, 0, 0, nil, 1, 1],
"#{test_app}/scripts/delete.sh" => [nil, nil, 1, 1, 1, 1, 1, 2, 2],
"#{test_app}/scripts/function.sh" => [nil, nil, nil, 2, nil, nil, nil, 1, 1, nil, nil, nil, nil, 1, nil, nil, 1, 1, 1],
"#{test_app}/scripts/heredocuments.sh" => [nil, nil, 1, 1, 1, nil, 1, 1, 1, 1, 1, 1, nil, 1, 1, 1, 1, 1],
"#{test_app}/scripts/long_line.sh" => [nil, nil, 1, 1, 1, 0],
"#{test_app}/scripts/nested/simple.sh" => [nil, nil, nil, nil, 1, 1, nil, 0, nil, nil, 1],
"#{test_app}/scripts/one_liner.sh" => [nil, nil, 2, nil, 1, nil, 0],
Expand Down
19 changes: 19 additions & 0 deletions spec/test_app/scripts/heredocuments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

cat << EOF
Showing text
EOF

cat <<- END
Another couple
of lines
- list 1
- list 2
END

cat <<- FINISH
Complex
important
message
FINISH