Skip to content

Commit b19a8c0

Browse files
committed
Merge branch 'jk/test-body-in-here-doc'
The test framework learned to take the test body not as a single string but as a here-document. * jk/test-body-in-here-doc: t/.gitattributes: ignore whitespace in chainlint expect files t: convert some here-doc test bodies test-lib: allow test snippets as here-docs chainlint.pl: add tests for test body in heredoc chainlint.pl: recognize test bodies defined via heredoc chainlint.pl: check line numbers in expected output chainlint.pl: force CRLF conversion when opening input files chainlint.pl: do not spawn more threads than we have scripts chainlint.pl: only start threads if jobs > 1 chainlint.pl: add test_expect_success call to test snippets
2 parents 6da44da + 55fe615 commit b19a8c0

File tree

160 files changed

+1286
-1026
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+1286
-1026
lines changed

t/.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
t[0-9][0-9][0-9][0-9]/* -whitespace
2-
/chainlint/*.expect eol=lf
2+
/chainlint/*.expect eol=lf -whitespace
33
/t0110/url-* binary
44
/t3206/* eol=lf
55
/t3900/*.txt eol=lf

t/Makefile

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,8 @@ clean-chainlint:
108108

109109
check-chainlint:
110110
@mkdir -p '$(CHAINLINTTMP_SQ)' && \
111-
for i in $(CHAINLINTTESTS); do \
112-
echo "test_expect_success '$$i' '" && \
113-
sed -e '/^# LINT: /d' chainlint/$$i.test && \
114-
echo "'"; \
115-
done >'$(CHAINLINTTMP_SQ)'/tests && \
116-
{ \
117-
echo "# chainlint: $(CHAINLINTTMP_SQ)/tests" && \
118-
for i in $(CHAINLINTTESTS); do \
119-
echo "# chainlint: $$i" && \
120-
cat chainlint/$$i.expect; \
121-
done \
122-
} >'$(CHAINLINTTMP_SQ)'/expect && \
123-
$(CHAINLINT) --emit-all '$(CHAINLINTTMP_SQ)'/tests | \
124-
sed -e 's/^[1-9][0-9]* //' >'$(CHAINLINTTMP_SQ)'/actual && \
111+
'$(PERL_PATH_SQ)' chainlint-cat.pl '$(CHAINLINTTMP_SQ)' $(CHAINLINTTESTS) && \
112+
{ $(CHAINLINT) --emit-all '$(CHAINLINTTMP_SQ)'/tests >'$(CHAINLINTTMP_SQ)'/actual || true; } && \
125113
diff -u '$(CHAINLINTTMP_SQ)'/expect '$(CHAINLINTTMP_SQ)'/actual
126114

127115
test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax \

t/README

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,14 @@ see test-lib-functions.sh for the full list and their options.
882882
'git-write-tree should be able to write an empty tree.' \
883883
'tree=$(git-write-tree)'
884884

885+
If <script> is `-` (a single dash), then the script to run is read
886+
from stdin. This lets you more easily use single quotes within the
887+
script by using a here-doc. For example:
888+
889+
test_expect_success 'output contains expected string' - <<\EOT
890+
grep "this string has 'quotes' in it" output
891+
EOT
892+
885893
If you supply three parameters the first will be taken to be a
886894
prerequisite; see the test_set_prereq and test_have_prereq
887895
documentation below:

t/chainlint-cat.pl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env perl
2+
3+
my $outdir = shift;
4+
open(my $tests, '>', "$outdir/tests")
5+
or die "unable to open $outdir/tests: $!";
6+
open(my $expect, '>', "$outdir/expect")
7+
or die "unable to open $outdir/expect: $!";
8+
9+
print $expect "# chainlint: $outdir/tests\n";
10+
11+
my $offset = 0;
12+
for my $script (@ARGV) {
13+
print $expect "# chainlint: $script\n";
14+
15+
open(my $expect_in, '<', "chainlint/$script.expect")
16+
or die "unable to open chainlint/$script.expect: $!";
17+
while (<$expect_in>) {
18+
s/^\d+/$& + $offset/e;
19+
print $expect $_;
20+
}
21+
22+
open(my $test_in, '<', "chainlint/$script.test")
23+
or die "unable to open chainlint/$script.test: $!";
24+
while (<$test_in>) {
25+
/^# LINT: / and next;
26+
print $tests $_;
27+
$offset++;
28+
}
29+
}

t/chainlint.pl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ sub swallow_heredocs {
174174
$$b =~ /(?:\G|\n)$indent\Q$$tag[0]\E(?:\n|\z)/gc;
175175
if (pos($$b) > $start) {
176176
my $body = substr($$b, $start, pos($$b) - $start);
177+
$self->{parser}->{heredocs}->{$$tag[0]} = {
178+
content => substr($body, 0, length($body) - length($&)),
179+
start_line => $self->{lineno},
180+
};
177181
$self->{lineno} += () = $body =~ /\n/sg;
178182
next;
179183
}
@@ -232,7 +236,8 @@ sub new {
232236
my $self = bless {
233237
buff => [],
234238
stop => [],
235-
output => []
239+
output => [],
240+
heredocs => {},
236241
} => $class;
237242
$self->{lexer} = Lexer->new($self, $s);
238243
return $self;
@@ -616,14 +621,21 @@ sub unwrap {
616621

617622
sub check_test {
618623
my $self = shift @_;
619-
my ($title, $body) = map(unwrap, @_);
624+
my $title = unwrap(shift @_);
625+
my $body = shift @_;
626+
my $lineno = $body->[3];
627+
$body = unwrap($body);
628+
if ($body eq '-') {
629+
my $herebody = shift @_;
630+
$body = $herebody->{content};
631+
$lineno = $herebody->{start_line};
632+
}
620633
$self->{ntests}++;
621634
my $parser = TestParser->new(\$body);
622635
my @tokens = $parser->parse();
623636
my $problems = $parser->{problems};
624637
return unless $emit_all || @$problems;
625638
my $c = main::fd_colors(1);
626-
my $lineno = $_[1]->[3];
627639
my $start = 0;
628640
my $checked = '';
629641
for (sort {$a->[1]->[2] <=> $b->[1]->[2]} @$problems) {
@@ -649,8 +661,13 @@ sub parse_cmd {
649661
return @tokens unless @tokens && $tokens[0]->[0] =~ /^test_expect_(?:success|failure)$/;
650662
my $n = $#tokens;
651663
$n-- while $n >= 0 && $tokens[$n]->[0] =~ /^(?:[;&\n|]|&&|\|\|)$/;
652-
$self->check_test($tokens[1], $tokens[2]) if $n == 2; # title body
653-
$self->check_test($tokens[2], $tokens[3]) if $n > 2; # prereq title body
664+
my $herebody;
665+
if ($n >= 2 && $tokens[$n-1]->[0] eq '-' && $tokens[$n]->[0] =~ /^<<-?(.+)$/) {
666+
$herebody = $self->{heredocs}->{$1};
667+
$n--;
668+
}
669+
$self->check_test($tokens[1], $tokens[2], $herebody) if $n == 2; # title body
670+
$self->check_test($tokens[2], $tokens[3], $herebody) if $n > 2; # prereq title body
654671
return @tokens;
655672
}
656673

@@ -762,7 +779,7 @@ sub check_script {
762779
while (my $path = $next_script->()) {
763780
$nscripts++;
764781
my $fh;
765-
unless (open($fh, "<", $path)) {
782+
unless (open($fh, "<:unix:crlf", $path)) {
766783
$emit->("?!ERR?! $path: $!\n");
767784
next;
768785
}
@@ -806,8 +823,10 @@ sub exit_code {
806823
show_stats($start_time, \@stats) if $show_stats;
807824
exit;
808825
}
826+
$jobs = @scripts if @scripts < $jobs;
809827

810-
unless ($Config{useithreads} && eval {
828+
unless ($jobs > 1 &&
829+
$Config{useithreads} && eval {
811830
require threads; threads->import();
812831
require Thread::Queue; Thread::Queue->import();
813832
1;
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
(
2-
foo &&
3-
bar=$((42 + 1)) &&
4-
baz
5-
) &&
6-
(
7-
bar=$((42 + 1)) ?!AMP?!
8-
baz
9-
)
1+
2 (
2+
3 foo &&
3+
4 bar=$((42 + 1)) &&
4+
5 baz
5+
6 ) &&
6+
7 (
7+
8 bar=$((42 + 1)) ?!AMP?!
8+
9 baz
9+
10 )

t/chainlint/arithmetic-expansion.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'arithmetic-expansion' '
12
(
23
foo &&
34
# LINT: closing ")" of $((...)) not misinterpreted as subshell-closing ")"
@@ -9,3 +10,4 @@
910
bar=$((42 + 1))
1011
baz
1112
)
13+
'

t/chainlint/bash-array.expect

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
(
2-
foo &&
3-
bar=(gumbo stumbo wumbo) &&
4-
baz
5-
) &&
6-
(
7-
foo &&
8-
bar=${#bar[@]} &&
9-
baz
10-
)
1+
2 (
2+
3 foo &&
3+
4 bar=(gumbo stumbo wumbo) &&
4+
5 baz
5+
6 ) &&
6+
7 (
7+
8 foo &&
8+
9 bar=${#bar[@]} &&
9+
10 baz
10+
11 )

t/chainlint/bash-array.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'bash-array' '
12
(
23
foo &&
34
# LINT: ")" in Bash array assignment not misinterpreted as subshell-closing ")"
@@ -10,3 +11,4 @@
1011
bar=${#bar[@]} &&
1112
baz
1213
)
14+
'
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
test_done () {
2-
case "$test_failure" in
3-
0)
4-
test_at_end_hook_
5-
6-
exit 0 ;;
7-
8-
*)
9-
if test $test_external_has_tap -eq 0
10-
then
11-
say_color error "# failed $test_failure among $msg"
12-
say "1..$test_count"
13-
fi
14-
15-
exit 1 ;;
16-
17-
esac
18-
}
1+
2 test_done () {
2+
3 case "$test_failure" in
3+
4 0)
4+
5 test_at_end_hook_
5+
6
6+
7 exit 0 ;;
7+
8
8+
9 *)
9+
10 if test $test_external_has_tap -eq 0
10+
11 then
11+
12 say_color error "# failed $test_failure among $msg"
12+
13 say "1..$test_count"
13+
14 fi
14+
15
15+
16 exit 1 ;;
16+
17
17+
18 esac
18+
19 }

t/chainlint/blank-line-before-esac.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'blank-line-before-esac' '
12
# LINT: blank line before "esac"
23
test_done () {
34
case "$test_failure" in
@@ -17,3 +18,4 @@ test_done () {
1718

1819
esac
1920
}
21+
'

t/chainlint/blank-line.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
(
2-
3-
nothing &&
4-
5-
something
6-
7-
8-
)
1+
2 (
2+
3
3+
4 nothing &&
4+
5
5+
6 something
6+
7
7+
8
8+
9 )

t/chainlint/blank-line.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'blank-line' '
12
(
23

34
nothing &&
@@ -8,3 +9,4 @@
89

910

1011
)
12+
'

t/chainlint/block-comment.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
(
2-
{
3-
# show a
4-
echo a &&
5-
# show b
6-
echo b
7-
}
8-
)
1+
2 (
2+
3 {
3+
4 # show a
4+
5 echo a &&
5+
6 # show b
6+
7 echo b
7+
8 }
8+
9 )

t/chainlint/block-comment.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'block-comment' '
12
(
23
{
34
# show a
@@ -6,3 +7,4 @@
67
echo b
78
}
89
)
10+
'

t/chainlint/block.expect

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
(
2-
foo &&
3-
{
4-
echo a ?!AMP?!
5-
echo b
6-
} &&
7-
bar &&
8-
{
9-
echo c
10-
} ?!AMP?!
11-
baz
12-
) &&
13-
14-
{
15-
echo a; ?!AMP?! echo b
16-
} &&
17-
{ echo a; ?!AMP?! echo b; } &&
18-
19-
{
20-
echo "${var}9" &&
21-
echo "done"
22-
} &&
23-
finis
1+
2 (
2+
3 foo &&
3+
4 {
4+
5 echo a ?!AMP?!
5+
6 echo b
6+
7 } &&
7+
8 bar &&
8+
9 {
9+
10 echo c
10+
11 } ?!AMP?!
11+
12 baz
12+
13 ) &&
13+
14
14+
15 {
15+
16 echo a; ?!AMP?! echo b
16+
17 } &&
17+
18 { echo a; ?!AMP?! echo b; } &&
18+
19
19+
20 {
20+
21 echo "${var}9" &&
21+
22 echo "done"
22+
23 } &&
23+
24 finis

t/chainlint/block.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'block' '
12
(
23
# LINT: missing "&&" after first "echo"
34
foo &&
@@ -25,3 +26,4 @@
2526
echo "done"
2627
} &&
2728
finis
29+
'

t/chainlint/broken-chain.expect

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
(
2-
foo &&
3-
bar ?!AMP?!
4-
baz &&
5-
wop
6-
)
1+
2 (
2+
3 foo &&
3+
4 bar ?!AMP?!
4+
5 baz &&
5+
6 wop
6+
7 )

t/chainlint/broken-chain.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
test_expect_success 'broken-chain' '
12
(
23
foo &&
34
# LINT: missing "&&" from "bar"
@@ -6,3 +7,4 @@
67
# LINT: final statement before closing ")" legitimately lacks "&&"
78
wop
89
)
10+
'

0 commit comments

Comments
 (0)