diff mbox series

[2/2] tests/qapi-schema: Make test-qapi.py -u work when files are absent

Message ID 20210922125619.670673-3-armbru@redhat.com (mailing list archive)
State New, archived
Headers show
Series tests/qapi-schema: Minor tooling improvements | expand

Commit Message

Markus Armbruster Sept. 22, 2021, 12:56 p.m. UTC
test-qapi.py -u updates the expected files.  Since it fails when they
are absent, users have to create them manually before they can use
test-qapi.py to fill in the contents, say for a new test.  Silly.
Improve -u to create them.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 tests/qapi-schema/test-qapi.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Comments

John Snow Sept. 22, 2021, 6:11 p.m. UTC | #1
On Wed, Sep 22, 2021 at 8:56 AM Markus Armbruster <armbru@redhat.com> wrote:

> test-qapi.py -u updates the expected files.  Since it fails when they
> are absent, users have to create them manually before they can use
> test-qapi.py to fill in the contents, say for a new test.  Silly.
> Improve -u to create them.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  tests/qapi-schema/test-qapi.py | 16 +++++++++++++---
>  1 file changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/tests/qapi-schema/test-qapi.py
> b/tests/qapi-schema/test-qapi.py
> index 2e384f5efd..c717a7a90b 100755
> --- a/tests/qapi-schema/test-qapi.py
> +++ b/tests/qapi-schema/test-qapi.py
> @@ -132,6 +132,17 @@ def test_frontend(fname):
>              print('    section=%s\n%s' % (section.name, section.text))
>
>
> +def open_test_result(dir_name, file_name, update):
> +    mode = 'r+' if update else 'r'
> +    try:
> +        fp = open(os.path.join(dir_name, file_name), mode)
> +    except FileNotFoundError:
> +        if not update:
> +            raise
> +        fp = open(os.path.join(dir_name, file_name), 'w+')
> +    return fp
> +
> +
>  def test_and_diff(test_name, dir_name, update):
>      sys.stdout = StringIO()
>      try:
> @@ -148,10 +159,9 @@ def test_and_diff(test_name, dir_name, update):
>          sys.stdout.close()
>          sys.stdout = sys.__stdout__
>
> -    mode = 'r+' if update else 'r'
>      try:
> -        outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
> -        errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
> +        outfp = open_test_result(dir_name, test_name + '.out', update)
> +        errfp = open_test_result(dir_name, test_name + '.err', update)
>          expected_out = outfp.readlines()
>          expected_err = errfp.readlines()
>      except OSError as err:
> --
> 2.31.1
>
>
Sure boss, why not?

Reviewed-by: John Snow <jsnow@redhat.com>
diff mbox series

Patch

diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 2e384f5efd..c717a7a90b 100755
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -132,6 +132,17 @@  def test_frontend(fname):
             print('    section=%s\n%s' % (section.name, section.text))
 
 
+def open_test_result(dir_name, file_name, update):
+    mode = 'r+' if update else 'r'
+    try:
+        fp = open(os.path.join(dir_name, file_name), mode)
+    except FileNotFoundError:
+        if not update:
+            raise
+        fp = open(os.path.join(dir_name, file_name), 'w+')
+    return fp
+
+
 def test_and_diff(test_name, dir_name, update):
     sys.stdout = StringIO()
     try:
@@ -148,10 +159,9 @@  def test_and_diff(test_name, dir_name, update):
         sys.stdout.close()
         sys.stdout = sys.__stdout__
 
-    mode = 'r+' if update else 'r'
     try:
-        outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
-        errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
+        outfp = open_test_result(dir_name, test_name + '.out', update)
+        errfp = open_test_result(dir_name, test_name + '.err', update)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
     except OSError as err: