Skip to content

Commit

Permalink
Speed things up by not using commands in PS4
Browse files Browse the repository at this point in the history
This was way slower and was messing with the number of hits
  • Loading branch information
infertux committed Mar 12, 2013
1 parent b75b4d0 commit 4130874
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/bashcov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
# Bashcov default module
# @note Keep it short!
module Bashcov
# [String] The project's root directory
ROOT_DIRECTORY = Dir.getwd

class << self

# @return [OpenStruct] Bashcov settings
attr_reader :options

# @return [String] The project's root directory inferred from the command
def root_directory
@root_directory ||= File.dirname @options.command
end

# Sets default options overriding any existing ones.
# @return [void]
def set_default_options!
Expand Down
2 changes: 1 addition & 1 deletion lib/bashcov/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def inject_xtrace_flag!
def find_bash_files!
return if Bashcov.options.skip_uncovered

Dir["#{Bashcov::ROOT_DIRECTORY}/**/*.sh"].each do |file|
Dir["#{Bashcov.root_directory}/**/*.sh"].each do |file|
@coverage[file] ||= [] # empty coverage array
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/bashcov/xtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Xtrace
# @see http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
# @note We use a forward slash as delimiter since it's the only forbidden
# character in filenames on Unix and Windows.
PS4 = %Q{#{PREFIX}$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")/${LINENO} BASHCOV: }
PS4 = %Q{#{PREFIX}${BASH_SOURCE[0]}/${LINENO}: }

# Regexp to match xtrace elements.
LINE_REGEXP = /\A#{Regexp.escape(PREFIX[0])}+#{PREFIX[1..-1]}(?<filename>.+)\/(?<lineno>\d+) BASHCOV: /
LINE_REGEXP = /\A#{Regexp.escape(PREFIX[0])}+#{PREFIX[1..-1]}(?<filename>.+)\/(?<lineno>\d+): /

# @return [Hash] Coverage of executed files
attr_reader :coverage
Expand Down Expand Up @@ -49,7 +49,7 @@ def read
match = line.match(LINE_REGEXP)
next if match.nil? # garbage line from multiline instruction

filename = File.expand_path(match[:filename], Bashcov::ROOT_DIRECTORY)
filename = File.expand_path(match[:filename], Bashcov.root_directory)

lineno = match[:lineno].to_i - 1
@files[filename] ||= []
Expand Down
6 changes: 6 additions & 0 deletions spec/bashcov/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
describe Bashcov::Runner do
let(:runner) { Bashcov::Runner.new test_suite }

before do
# 'Bashcov.options.command' is normally set through 'Bashcov.parse_options!'
# so we need to stub it
Bashcov.options.stub(:command).and_return(test_suite)
end

describe "#run" do
it "finds commands in $PATH" do
Bashcov::Runner.new('ls -l').run.should be_success
Expand Down
2 changes: 1 addition & 1 deletion spec/support/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def uncovered_files
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/case.sh" => [nil, nil, nil, 2, 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/function.sh" => [nil, nil, nil, 2, nil, nil, nil, 1, 1, nil, nil, 1, 1],
"#{test_app}/scripts/long_line.sh" => [nil, nil, 1, 1, 1, 0],
Expand Down

0 comments on commit 4130874

Please sign in to comment.