File tree Expand file tree Collapse file tree 8 files changed +92
-1
lines changed
fixtures/workspaces/heredoc Expand file tree Collapse file tree 8 files changed +92
-1
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,17 @@ def indent(offset)
33 return self unless offset . positive?
44
55 indentation = ' ' * offset
6- map { |line | "#{ indentation } #{ line } " }
6+ heredoc_marker = nil
7+
8+ map do |line |
9+ if heredoc_marker
10+ heredoc_marker = nil if /^#{ heredoc_marker } \n ?$/ . match? ( line )
11+ line
12+ else
13+ heredoc_marker = $1 if line =~ /<<-?(\w +)\n ?$/
14+ "#{ indentation } #{ line } "
15+ end
16+ end
717 end
818
919 def nonuniq
Original file line number Diff line number Diff line change 1+ + bundle exec bashly generate
2+ creating user files in src
3+ skipped src/root_command.sh (exists)
4+ created ./cli
5+ run ./cli --help to test your bash script
6+ + ./cli
7+ unindented
8+ multiline
9+ heredoc text
10+ also unindented
11+ 1 indentation
12+ 2 indentations
13+ 3 indentations
14+
15+ 3 indentations with empty line above
16+ also unindented
Original file line number Diff line number Diff line change 1111 expect ( subject . indent ( 0 ) ) . to eq subject
1212 end
1313 end
14+
15+ context 'when the string contains heredoc block' do
16+ subject do
17+ result = <<~SUBJECT
18+ function() {
19+ cat <<-SOME_EOF_MARKER
20+ not-indented
21+ indented-once
22+ indented-twice
23+ SOME_EOF_MARKER
24+ } # indented with function() start
25+ SUBJECT
26+
27+ result . lines
28+ end
29+
30+ let ( :expected ) do
31+ result = <<~SUBJECT
32+ function() {
33+ cat <<-SOME_EOF_MARKER
34+ not-indented
35+ indented-once
36+ indented-twice
37+ SOME_EOF_MARKER
38+ } # indented with function() start
39+ SUBJECT
40+
41+ result . lines
42+ end
43+
44+ it 'does not indent it but indents everything else' do
45+ expect ( subject . indent 2 ) . to eq expected
46+ end
47+ end
1448 end
1549
1650 describe '#nonuniq' do
Original file line number Diff line number Diff line change 1+ cli
Original file line number Diff line number Diff line change 1+ This fixture tests that heredoc blocks are kept unindented
Original file line number Diff line number Diff line change 1+ name : cli
2+ help : Sample application to test heredoc
3+ version : 0.1.0
Original file line number Diff line number Diff line change 1+ echo unindented
2+
3+ cat << -FIRST_BLOCK
4+ multiline
5+ heredoc text
6+ FIRST_BLOCK
7+
8+ echo also unindented
9+
10+ cat << -SECOND_BLOCK
11+ 1 indentation
12+ 2 indentations
13+ 3 indentations
14+
15+ 3 indentations with empty line above
16+ SECOND_BLOCK
17+
18+ echo also unindented
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+
3+ set -x
4+
5+ bundle exec bashly generate
6+
7+ ./cli
8+
You can’t perform that action at this time.
0 commit comments