1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-23 21:34:54 +03:00
libvirt/tests/test-wrap-argv.pl
Daniel P. Berrange 8afd34f2d8 tests: redo test argv file line wrapping
Back in

  commit bd6c46fa0c
  Author: Juerg Haefliger <juerg.haefliger@hp.com>
  Date:   Mon Jan 31 06:42:57 2011 -0500

    tests: handle backspace-newline pairs in test input files

all the test argv files were line wrapped so that the args
were less than 80 characters.

The way the line wrapping was done turns out to be quite
undesirable, because it often leaves multiple parameters
on the same line. If we later need to add or remove
individual parameters, then it leaves us having to redo
line wrapping.

This commit changes the line wrapping so that every
single "-param value" is one its own new line. If the
"value" is still too long, then we break on ',' or ':'
or ' ' as needed.

This means that when we come to add / remove parameters
from the test files line, the patch diffs will only
ever show a single line added/removed which will greatly
simplify review work.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2015-11-09 15:50:39 +00:00

129 lines
3.7 KiB
Perl

#!/usr/bin/perl
#
# Copyright (C) 2015 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
#
# This script is intended to be passed a list of .args files, used
# to store command line ARGV for the test suites. It will reformat
# them such that there is at most one '-param value' on each line
# of the file. Parameter values that are longer than 80 chars will
# also be split.
#
foreach my $file (@ARGV) {
&rewrap($file);
}
sub rewrap {
my $file = shift;
# Read the original file
open FILE, "<", $file or die "cannot read $file: $!";
my @lines;
while (<FILE>) {
# If there is a trailing '\' then kill the new line
if (/\\$/) {
chomp;
$_ =~ s/\\$//;
}
push @lines, $_;
}
# Skip empty files
return unless @lines;
# Kill the last new line in the file
chomp @lines[$#lines];
close FILE;
# Reconstruct the master data by joining all lines
# and then split again based on the real desired
# newlines
@lines = split /\n/, join('', @lines);
# Now each @lines represents a single command, we
# can process them
foreach my $line (@lines) {
my @bits = split / /, join('', $line);
# @bits contains env vars, then the command line
# and then the arguments
my @env;
my $cmd;
my @args;
if ($bits[0] !~ /=/) {
$cmd = shift @bits;
}
foreach my $bit (@bits) {
# If no command is defined yet, we must still
# have env vars
if (!defined $cmd) {
# Look for leading / to indicate command name
if ($bit =~ m,^/,) {
$cmd = $bit;
} else {
push @env, $bit;
}
} else {
# If there's a leading '-' then this is a new
# parameter, otherwise its a value for the prev
# parameter.
if ($bit =~ m,^-,) {
push @args, $bit;
} else {
$args[$#args] .= " " . $bit;
}
}
}
# Print env + command first
print join(" \\\n", @env, $cmd), " \\\n";
# We might have to split line argument values...
for (my $i = 0; $i <= $#args; $i++) {
my $arg = $args[$i];
while (length($arg) > 80) {
my $split = rindex $arg, ",", 80;
if ($split == -1) {
$split = rindex $arg, ":", 80;
}
if ($split == -1) {
$split = rindex $arg, " ", 80;
}
if ($split == -1) {
warn "$file: cannot find nice place to split '$arg' below 80 chars\n";
$split = 79;
}
$split++;
my $head = substr $arg, 0, $split;
$arg = substr $arg, $split;
print $head, "\\\n";
}
print $arg;
if ($i != $#args) {
print " \\\n";
} else {
print "\n";
}
}
}
}