[libvirt] [PATCH 7/8] test-wrap-argv: add --in-place parameter

Ján Tomko jtomko at redhat.com
Wed Jun 15 16:39:34 UTC 2016


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 | 24 +++++++++++++++++++++---
 tests/testutils.c       |  8 +-------
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/tests/test-wrap-argv.pl b/tests/test-wrap-argv.pl
index 96f998a..97f6903 100755
--- a/tests/test-wrap-argv.pl
+++ b/tests/test-wrap-argv.pl
@@ -22,7 +22,16 @@
 # 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 {
diff --git a/tests/testutils.c b/tests/testutils.c
index 54adab2..1d503c1 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -440,26 +440,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;
 }
 
-- 
2.7.3




More information about the libvir-list mailing list