]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
test-wrap-argv: add --in-place parameter
authorJán Tomko <jtomko@redhat.com>
Tue, 31 May 2016 08:19:04 +0000 (10:19 +0200)
committerJán Tomko <jtomko@redhat.com>
Tue, 21 Jun 2016 16:13:07 +0000 (18:13 +0200)
If --in-place is supplied as the first argument to the script,
replace the file in-place instead of printing to stdout.

tests/test-wrap-argv.pl
tests/testutils.c

index 96f998a09549bd27436289775a162c62651da57b..97f6903e85b0cd117069d7c6367303c7a3770c78 100755 (executable)
 # of the file. Parameter values that are longer than 80 chars will
 # also be split.
 #
+# If --in-place is supplied as the first parameter of this script,
+# the files will be changed in place.
+# Otherwise the rewrapped files are printed to the standard output.
 
+$in_place = 0;
+
+if (@ARGV[0] eq "--in-place") {
+    $in_place = 1;
+    shift @ARGV;
+}
 
 foreach my $file (@ARGV) {
     &rewrap($file);
@@ -57,10 +66,19 @@ sub rewrap {
 
     # Now each @lines represents a single command, we
     # can process them
-    foreach my $line (@lines) {
-        print &rewrap_line ($line);
-    }
+    @lines = map { &rewrap_line($_) } @lines;
 
+    if ($in_place) {
+        open FILE, ">", $file or die "cannot write $file: $!";
+        foreach my $line (@lines) {
+            print FILE $line;
+        }
+        close FILE;
+    } else {
+        foreach my $line (@lines) {
+            print $line;
+        }
+    }
 }
 
 sub rewrap_line {
index 21b3bc66da6687c709be47aad215c2df77ea3566..8859ed50ff55919d27de5b8b674681946f55b5e3 100644 (file)
@@ -438,26 +438,20 @@ static int
 virTestRewrapFile(const char *filename)
 {
     int ret = -1;
-    char *outbuf = NULL;
     char *script = NULL;
     virCommandPtr cmd = NULL;
 
     if (virAsprintf(&script, "%s/test-wrap-argv.pl", abs_srcdir) < 0)
         goto cleanup;
 
-    cmd = virCommandNewArgList(script, filename, NULL);
-    virCommandSetOutputBuffer(cmd, &outbuf);
+    cmd = virCommandNewArgList(script, "--in-place", filename, NULL);
     if (virCommandRun(cmd, NULL) < 0)
         goto cleanup;
 
-    if (virFileWriteStr(filename, outbuf, 0666) < 0)
-        goto cleanup;
-
     ret = 0;
  cleanup:
     VIR_FREE(script);
     virCommandFree(cmd);
-    VIR_FREE(outbuf);
     return ret;
 }