tests: refactor running glib-mkenums

Part of runMkenumsWithHeader() was duplicated in test_reproducible(),
and would otherwise need to be duplicated again in upcoming tests.  Many
places duplicated decoding stdout/stderr and checking the exit code.

Introduce a named tuple for the returned fields; and factor out writing
a template file to pass with --template.
This commit is contained in:
Will Thompson 2018-08-10 15:38:53 +01:00
parent 0d271223d8
commit 12a2a984f2
No known key found for this signature in database
GPG Key ID: 3422DC0D7AD482A7

View File

@ -20,6 +20,7 @@
"""Integration tests for glib-mkenums utility.""" """Integration tests for glib-mkenums utility."""
import collections
import os import os
import subprocess import subprocess
import tempfile import tempfile
@ -28,6 +29,9 @@ import unittest
import taptestrunner import taptestrunner
Result = collections.namedtuple('Result', ('info', 'out', 'err', 'subs'))
class TestMkenums(unittest.TestCase): class TestMkenums(unittest.TestCase):
"""Integration test for running glib-mkenums. """Integration test for running glib-mkenums.
@ -70,10 +74,37 @@ class TestMkenums(unittest.TestCase):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
env=env) env=env)
print('Output:', info.stdout.decode('utf-8')) info.check_returncode()
return info out = info.stdout.decode('utf-8').strip()
err = info.stderr.decode('utf-8').strip()
def runMkenumsWithHeader(self, h_contents, encoding='utf-8', *args): # Known substitutions for standard boilerplate
subs = {
'standard_top_comment':
'This file is generated by glib-mkenums, do not modify '
'it. This code is licensed under the same license as the '
'containing project. Note that it links to GLib, so must '
'comply with the LGPL linking clauses.',
'standard_bottom_comment': 'Generated data ends here'
}
result = Result(info, out, err, subs)
print('Output:', result.out)
return result
def runMkenumsWithTemplate(self, template_contents, *args):
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.template') as template_file:
# Write out the template.
template_file.write(template_contents.encode('utf-8'))
print(template_file.name + ':', template_contents)
template_file.flush()
return self.runMkenums('--template', template_file.name, *args)
def runMkenumsWithAllSubstitutions(self, *args):
'''Run glib-mkenums with a template which outputs all substitutions.'''
template_contents = ''' template_contents = '''
/*** BEGIN file-header ***/ /*** BEGIN file-header ***/
file-header file-header
@ -144,44 +175,28 @@ filename: @filename@
basename: @basename@ basename: @basename@
/*** END file-tail ***/ /*** END file-tail ***/
''' '''
return self.runMkenumsWithTemplate(template_contents, *args)
def runMkenumsWithHeader(self, h_contents, encoding='utf-8'):
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.template') as template_file, \
tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.h') as h_file: suffix='.h') as h_file:
# Write out the template.
template_file.write(template_contents.encode('utf-8'))
print(template_file.name + ':', template_contents)
# Write out the header to be scanned. # Write out the header to be scanned.
h_file.write(h_contents.encode(encoding)) h_file.write(h_contents.encode(encoding))
print(h_file.name + ':', h_contents) print(h_file.name + ':', h_contents)
template_file.flush()
h_file.flush() h_file.flush()
# Run glib-mkenums with a template which outputs all substitutions. # Run glib-mkenums with a template which outputs all substitutions.
info = self.runMkenums('--template', template_file.name, result = self.runMkenumsWithAllSubstitutions(h_file.name)
h_file.name)
info.check_returncode()
out = info.stdout.decode('utf-8').strip()
err = info.stderr.decode('utf-8').strip()
# Known substitutions for generated filenames. # Known substitutions for generated filenames.
subs = { result.subs.update({
'filename': h_file.name, 'filename': h_file.name,
'basename': os.path.basename(h_file.name), 'basename': os.path.basename(h_file.name),
'standard_top_comment': })
'This file is generated by glib-mkenums, do not modify '
'it. This code is licensed under the same license as the '
'containing project. Note that it links to GLib, so must '
'comply with the LGPL linking clauses.',
'standard_bottom_comment': 'Generated data ends here'
}
return (info, out, err, subs) return result
def assertSingleEnum(self, out, subs, enum_name_camel, enum_name_lower, def assertSingleEnum(self, result, enum_name_camel, enum_name_lower,
enum_name_upper, enum_name_short, enum_prefix, enum_name_upper, enum_name_short, enum_prefix,
type_lower, type_camel, type_upper, type_lower, type_camel, type_upper,
value_name, value_nick, value_num): value_name, value_nick, value_num):
@ -199,7 +214,7 @@ basename: @basename@
'value_name': value_name, 'value_name': value_name,
'value_nick': value_nick, 'value_nick': value_nick,
'value_num': value_num, 'value_num': value_num,
}, **subs) }, **result.subs)
self.assertEqual(''' self.assertEqual('''
comment comment
@ -252,20 +267,17 @@ basename: {basename}
comment comment
comment: {standard_bottom_comment} comment: {standard_bottom_comment}
'''.format(**subs).strip(), out) '''.format(**subs).strip(), result.out)
def test_help(self): def test_help(self):
"""Test the --help argument.""" """Test the --help argument."""
info = self.runMkenums('--help') result = self.runMkenums('--help')
info.check_returncode() self.assertIn('usage: glib-mkenums', result.out)
out = info.stdout.decode('utf-8').strip()
self.assertIn('usage: glib-mkenums', out)
def test_empty_header(self): def test_empty_header(self):
"""Test an empty header.""" """Test an empty header."""
(info, out, err, subs) = self.runMkenumsWithHeader('') result = self.runMkenumsWithHeader('')
self.assertEqual('', err) self.assertEqual('', result.err)
self.assertEqual(''' self.assertEqual('''
comment comment
comment: {standard_top_comment} comment: {standard_top_comment}
@ -280,7 +292,7 @@ basename: {basename}
comment comment
comment: {standard_bottom_comment} comment: {standard_bottom_comment}
'''.format(**subs).strip(), out) '''.format(**result.subs).strip(), result.out)
def test_enum_name(self): def test_enum_name(self):
"""Test typedefs with an enum and a typedef name. Bug #794506.""" """Test typedefs with an enum and a typedef name. Bug #794506."""
@ -289,9 +301,9 @@ comment: {standard_bottom_comment}
ENUM_VALUE ENUM_VALUE
} SomeEnumIdentifier; } SomeEnumIdentifier;
''' '''
(info, out, err, subs) = self.runMkenumsWithHeader(h_contents) result = self.runMkenumsWithHeader(h_contents)
self.assertEqual('', err) self.assertEqual('', result.err)
self.assertSingleEnum(out, subs, 'SomeEnumIdentifier', self.assertSingleEnum(result, 'SomeEnumIdentifier',
'some_enum_identifier', 'SOME_ENUM_IDENTIFIER', 'some_enum_identifier', 'SOME_ENUM_IDENTIFIER',
'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum', 'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum',
'ENUM', 'ENUM_VALUE', 'value', '0') 'ENUM', 'ENUM_VALUE', 'value', '0')
@ -304,10 +316,9 @@ comment: {standard_bottom_comment}
ENUM_VALUE ENUM_VALUE
} SomeEnumIdentifier; } SomeEnumIdentifier;
''' '''
(info, out, err, subs) = \ result = self.runMkenumsWithHeader(h_contents, encoding='iso-8859-1')
self.runMkenumsWithHeader(h_contents, encoding='iso-8859-1') self.assertIn('WARNING: UnicodeWarning: ', result.err)
self.assertIn('WARNING: UnicodeWarning: ', err) self.assertSingleEnum(result, 'SomeEnumIdentifier',
self.assertSingleEnum(out, subs, 'SomeEnumIdentifier',
'some_enum_identifier', 'SOME_ENUM_IDENTIFIER', 'some_enum_identifier', 'SOME_ENUM_IDENTIFIER',
'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum', 'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum',
'ENUM', 'ENUM_VALUE', 'value', '0') 'ENUM', 'ENUM_VALUE', 'value', '0')
@ -315,6 +326,8 @@ comment: {standard_bottom_comment}
def test_reproducible(self): def test_reproducible(self):
"""Test builds are reproducible regardless of file ordering. """Test builds are reproducible regardless of file ordering.
Bug #691436.""" Bug #691436."""
template_contents = 'template'
h_contents1 = ''' h_contents1 = '''
typedef enum { typedef enum {
FIRST, FIRST,
@ -328,36 +341,28 @@ comment: {standard_bottom_comment}
''' '''
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.template') as template_file, \
tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='1.h') as h_file1, \ suffix='1.h') as h_file1, \
tempfile.NamedTemporaryFile(dir=self.tmpdir.name, tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='2.h') as h_file2: suffix='2.h') as h_file2:
# Write out the template and headers. # Write out the headers.
template_file.write('template'.encode('utf-8'))
h_file1.write(h_contents1.encode('utf-8')) h_file1.write(h_contents1.encode('utf-8'))
h_file2.write(h_contents2.encode('utf-8')) h_file2.write(h_contents2.encode('utf-8'))
template_file.flush()
h_file1.flush() h_file1.flush()
h_file2.flush() h_file2.flush()
# Run glib-mkenums with the headers in one order, and then again # Run glib-mkenums with the headers in one order, and then again
# in another order. # in another order.
info1 = self.runMkenums('--template', template_file.name, result1 = self.runMkenumsWithTemplate(template_contents,
h_file1.name, h_file2.name) h_file1.name, h_file2.name)
info1.check_returncode() self.assertEqual('', result1.err)
out1 = info1.stdout.decode('utf-8').strip()
self.assertEqual('', info1.stderr.decode('utf-8').strip())
info2 = self.runMkenums('--template', template_file.name, result2 = self.runMkenumsWithTemplate(template_contents,
h_file2.name, h_file1.name) h_file2.name, h_file1.name)
info2.check_returncode() self.assertEqual('', result2.err)
out2 = info2.stdout.decode('utf-8').strip()
self.assertEqual('', info2.stderr.decode('utf-8').strip())
# The output should be the same. # The output should be the same.
self.assertEqual(out1, out2) self.assertEqual(result1.out, result2.out)
def test_no_nick(self): def test_no_nick(self):
"""Test trigraphs with a desc but no nick. Issue #1360.""" """Test trigraphs with a desc but no nick. Issue #1360."""
@ -366,9 +371,9 @@ comment: {standard_bottom_comment}
GEGL_SAMPLER_NEAREST = 0, /*< desc="nearest" >*/ GEGL_SAMPLER_NEAREST = 0, /*< desc="nearest" >*/
} GeglSamplerType; } GeglSamplerType;
''' '''
(info, out, err, subs) = self.runMkenumsWithHeader(h_contents) result = self.runMkenumsWithHeader(h_contents)
self.assertEqual('', err) self.assertEqual('', result.err)
self.assertSingleEnum(out, subs, 'GeglSamplerType', self.assertSingleEnum(result, 'GeglSamplerType',
'gegl_sampler_type', 'GEGL_SAMPLER_TYPE', 'gegl_sampler_type', 'GEGL_SAMPLER_TYPE',
'SAMPLER_TYPE', 'GEGL', 'enum', 'Enum', 'SAMPLER_TYPE', 'GEGL', 'enum', 'Enum',
'ENUM', 'GEGL_SAMPLER_NEAREST', 'nearest', '0') 'ENUM', 'GEGL_SAMPLER_NEAREST', 'nearest', '0')