From 88642a9856a55fe6585a5025fdd694046ca9defe Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Wed, 21 Feb 2018 00:14:55 +0000 Subject: [PATCH 1/2] lexer: Recognize commands split across several lines with backslashes --- lib/bashcov/lexer.rb | 8 +++++++- spec/support/test_app.rb | 1 + spec/test_app/scripts/multiline3.sh | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100755 spec/test_app/scripts/multiline3.sh diff --git a/lib/bashcov/lexer.rb b/lib/bashcov/lexer.rb index 6c8ceb8..b053b39 100644 --- a/lib/bashcov/lexer.rb +++ b/lib/bashcov/lexer.rb @@ -27,7 +27,7 @@ def initialize(filename, coverage) # Process and complete initial coverage. # @return [void] - def complete_coverage + def complete_coverage # rubocop:disable Metrics/MethodLength lines = IO.read(@filename).encode("utf-8", invalid: :replace).lines lines.each_with_index do |line, lineno| @@ -43,6 +43,12 @@ def complete_coverage /\A[^\n]+\\$(\s*['"][^'"]*['"]\s*\\$){1,}\s*['"][^'"]*['"]\s*$/ ) + # simple line continuations with backslashes + mark_multiline( + lines, lineno, + /\A([^\n&|;]*[^\\&|;](\\\\)*\\\n)+[^\n&|;]*[^\n\\&|;](\\\\)*$/ + ) + # multiline string concatenated with newlines %w[' "].each do |char| mark_multiline( diff --git a/spec/support/test_app.rb b/spec/support/test_app.rb index 3eb0f47..07527f0 100644 --- a/spec/support/test_app.rb +++ b/spec/support/test_app.rb @@ -31,6 +31,7 @@ def expected_coverage # rubocop:disable Metrics/MethodLength "#{test_app}/scripts/unicode.sh" => [nil, nil, nil, 1, nil, 1], "#{test_app}/scripts/multiline.sh" => [nil, nil, nil, 1, nil, 0, nil, 1, nil, 1, nil, 0, nil, nil, 1, 2, 1, 1, 0, nil, nil, 2, nil, nil, 1, 1, 1, 1, nil, 1, 1, 1, 1, 1, nil, 1, 1, 1, 1, nil, 1], "#{test_app}/scripts/multiline2.sh" => [nil, nil, 1, 1, 1, 1, nil, 1, 1, 1, 1, nil, 1, 1, 1, 1, 1, 1, nil, 1, 1, 1, 1, 1, 1, 1, 1, nil, 1, 1, 1, nil, 1], + "#{test_app}/scripts/multiline3.sh" => [nil, nil, 1, 1, 1, 1], "#{test_app}/scripts/executable" => [nil, nil, 1], "#{test_app}/scripts/exit_non_zero.sh" => [nil, nil, 1], "#{test_app}/scripts/no_shebang.sh" => [nil, nil, nil, nil, 0], diff --git a/spec/test_app/scripts/multiline3.sh b/spec/test_app/scripts/multiline3.sh new file mode 100755 index 0000000..487d87f --- /dev/null +++ b/spec/test_app/scripts/multiline3.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +echo 1 \ + 2 \ + 3 \ + 4 From bab65d83711baf62b261a5d5d936d631abd7c779 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Wed, 21 Feb 2018 00:51:49 +0000 Subject: [PATCH 2/2] lexer: Recognize multi-line arrays --- lib/bashcov/lexer.rb | 7 +++++++ spec/support/test_app.rb | 1 + spec/test_app/scripts/array.sh | 8 ++++++++ 3 files changed, 16 insertions(+) create mode 100755 spec/test_app/scripts/array.sh diff --git a/lib/bashcov/lexer.rb b/lib/bashcov/lexer.rb index b053b39..649fec3 100644 --- a/lib/bashcov/lexer.rb +++ b/lib/bashcov/lexer.rb @@ -31,6 +31,13 @@ def complete_coverage # rubocop:disable Metrics/MethodLength lines = IO.read(@filename).encode("utf-8", invalid: :replace).lines lines.each_with_index do |line, lineno| + # multi-line arrays + mark_multiline( + lines, lineno, + /\A[^\n]*\b=\([^()]*\)/, + forward: false + ) + # heredoc mark_multiline( lines, lineno, diff --git a/spec/support/test_app.rb b/spec/support/test_app.rb index 07527f0..4738e08 100644 --- a/spec/support/test_app.rb +++ b/spec/support/test_app.rb @@ -15,6 +15,7 @@ def uncovered_files def expected_coverage # rubocop:disable Metrics/MethodLength { "#{test_app}/never_called.sh" => [nil, nil, 0], + "#{test_app}/scripts/array.sh" => [nil, nil, 1, 1, 1, 1, 1, 1], "#{test_app}/scripts/case.sh" => [nil, nil, nil, 2, nil, 1, nil, nil, 0, 1, nil, nil, nil, 1, 1], "#{test_app}/scripts/cd.sh" => [nil, nil, 1, 2, nil, 3, 1, 3, nil, 2, nil, nil, 1, nil, 3, nil, 6, nil, 1, nil, 1], "#{test_app}/scripts/delete.sh" => [nil, nil, 1, 1, 1, 1, nil, 1, 1], diff --git a/spec/test_app/scripts/array.sh b/spec/test_app/scripts/array.sh new file mode 100755 index 0000000..3e23bf7 --- /dev/null +++ b/spec/test_app/scripts/array.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +arr=( + foo + bar + baz + quux +)