mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-12 15:36:17 +01:00
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:
parent
0d271223d8
commit
12a2a984f2
@ -20,6 +20,7 @@
|
||||
|
||||
"""Integration tests for glib-mkenums utility."""
|
||||
|
||||
import collections
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
@ -28,6 +29,9 @@ import unittest
|
||||
import taptestrunner
|
||||
|
||||
|
||||
Result = collections.namedtuple('Result', ('info', 'out', 'err', 'subs'))
|
||||
|
||||
|
||||
class TestMkenums(unittest.TestCase):
|
||||
"""Integration test for running glib-mkenums.
|
||||
|
||||
@ -70,10 +74,37 @@ class TestMkenums(unittest.TestCase):
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
env=env)
|
||||
print('Output:', info.stdout.decode('utf-8'))
|
||||
return info
|
||||
info.check_returncode()
|
||||
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 = '''
|
||||
/*** BEGIN file-header ***/
|
||||
file-header
|
||||
@ -144,44 +175,28 @@ filename: @filename@
|
||||
basename: @basename@
|
||||
/*** END file-tail ***/
|
||||
'''
|
||||
return self.runMkenumsWithTemplate(template_contents, *args)
|
||||
|
||||
def runMkenumsWithHeader(self, h_contents, encoding='utf-8'):
|
||||
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
|
||||
suffix='.template') as template_file, \
|
||||
tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
|
||||
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.
|
||||
h_file.write(h_contents.encode(encoding))
|
||||
print(h_file.name + ':', h_contents)
|
||||
|
||||
template_file.flush()
|
||||
h_file.flush()
|
||||
|
||||
# Run glib-mkenums with a template which outputs all substitutions.
|
||||
info = self.runMkenums('--template', template_file.name,
|
||||
h_file.name)
|
||||
info.check_returncode()
|
||||
out = info.stdout.decode('utf-8').strip()
|
||||
err = info.stderr.decode('utf-8').strip()
|
||||
result = self.runMkenumsWithAllSubstitutions(h_file.name)
|
||||
|
||||
# Known substitutions for generated filenames.
|
||||
subs = {
|
||||
result.subs.update({
|
||||
'filename': 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,
|
||||
type_lower, type_camel, type_upper,
|
||||
value_name, value_nick, value_num):
|
||||
@ -199,7 +214,7 @@ basename: @basename@
|
||||
'value_name': value_name,
|
||||
'value_nick': value_nick,
|
||||
'value_num': value_num,
|
||||
}, **subs)
|
||||
}, **result.subs)
|
||||
|
||||
self.assertEqual('''
|
||||
comment
|
||||
@ -252,20 +267,17 @@ basename: {basename}
|
||||
|
||||
comment
|
||||
comment: {standard_bottom_comment}
|
||||
'''.format(**subs).strip(), out)
|
||||
'''.format(**subs).strip(), result.out)
|
||||
|
||||
def test_help(self):
|
||||
"""Test the --help argument."""
|
||||
info = self.runMkenums('--help')
|
||||
info.check_returncode()
|
||||
|
||||
out = info.stdout.decode('utf-8').strip()
|
||||
self.assertIn('usage: glib-mkenums', out)
|
||||
result = self.runMkenums('--help')
|
||||
self.assertIn('usage: glib-mkenums', result.out)
|
||||
|
||||
def test_empty_header(self):
|
||||
"""Test an empty header."""
|
||||
(info, out, err, subs) = self.runMkenumsWithHeader('')
|
||||
self.assertEqual('', err)
|
||||
result = self.runMkenumsWithHeader('')
|
||||
self.assertEqual('', result.err)
|
||||
self.assertEqual('''
|
||||
comment
|
||||
comment: {standard_top_comment}
|
||||
@ -280,7 +292,7 @@ basename: {basename}
|
||||
|
||||
comment
|
||||
comment: {standard_bottom_comment}
|
||||
'''.format(**subs).strip(), out)
|
||||
'''.format(**result.subs).strip(), result.out)
|
||||
|
||||
def test_enum_name(self):
|
||||
"""Test typedefs with an enum and a typedef name. Bug #794506."""
|
||||
@ -289,9 +301,9 @@ comment: {standard_bottom_comment}
|
||||
ENUM_VALUE
|
||||
} SomeEnumIdentifier;
|
||||
'''
|
||||
(info, out, err, subs) = self.runMkenumsWithHeader(h_contents)
|
||||
self.assertEqual('', err)
|
||||
self.assertSingleEnum(out, subs, 'SomeEnumIdentifier',
|
||||
result = self.runMkenumsWithHeader(h_contents)
|
||||
self.assertEqual('', result.err)
|
||||
self.assertSingleEnum(result, 'SomeEnumIdentifier',
|
||||
'some_enum_identifier', 'SOME_ENUM_IDENTIFIER',
|
||||
'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum',
|
||||
'ENUM', 'ENUM_VALUE', 'value', '0')
|
||||
@ -304,10 +316,9 @@ comment: {standard_bottom_comment}
|
||||
ENUM_VALUE
|
||||
} SomeEnumIdentifier;
|
||||
'''
|
||||
(info, out, err, subs) = \
|
||||
self.runMkenumsWithHeader(h_contents, encoding='iso-8859-1')
|
||||
self.assertIn('WARNING: UnicodeWarning: ', err)
|
||||
self.assertSingleEnum(out, subs, 'SomeEnumIdentifier',
|
||||
result = self.runMkenumsWithHeader(h_contents, encoding='iso-8859-1')
|
||||
self.assertIn('WARNING: UnicodeWarning: ', result.err)
|
||||
self.assertSingleEnum(result, 'SomeEnumIdentifier',
|
||||
'some_enum_identifier', 'SOME_ENUM_IDENTIFIER',
|
||||
'ENUM_IDENTIFIER', 'SOME', 'enum', 'Enum',
|
||||
'ENUM', 'ENUM_VALUE', 'value', '0')
|
||||
@ -315,6 +326,8 @@ comment: {standard_bottom_comment}
|
||||
def test_reproducible(self):
|
||||
"""Test builds are reproducible regardless of file ordering.
|
||||
Bug #691436."""
|
||||
template_contents = 'template'
|
||||
|
||||
h_contents1 = '''
|
||||
typedef enum {
|
||||
FIRST,
|
||||
@ -328,36 +341,28 @@ comment: {standard_bottom_comment}
|
||||
'''
|
||||
|
||||
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
|
||||
suffix='.template') as template_file, \
|
||||
tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
|
||||
suffix='1.h') as h_file1, \
|
||||
tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
|
||||
suffix='2.h') as h_file2:
|
||||
# Write out the template and headers.
|
||||
template_file.write('template'.encode('utf-8'))
|
||||
# Write out the headers.
|
||||
h_file1.write(h_contents1.encode('utf-8'))
|
||||
h_file2.write(h_contents2.encode('utf-8'))
|
||||
|
||||
template_file.flush()
|
||||
h_file1.flush()
|
||||
h_file2.flush()
|
||||
|
||||
# Run glib-mkenums with the headers in one order, and then again
|
||||
# in another order.
|
||||
info1 = self.runMkenums('--template', template_file.name,
|
||||
h_file1.name, h_file2.name)
|
||||
info1.check_returncode()
|
||||
out1 = info1.stdout.decode('utf-8').strip()
|
||||
self.assertEqual('', info1.stderr.decode('utf-8').strip())
|
||||
result1 = self.runMkenumsWithTemplate(template_contents,
|
||||
h_file1.name, h_file2.name)
|
||||
self.assertEqual('', result1.err)
|
||||
|
||||
info2 = self.runMkenums('--template', template_file.name,
|
||||
h_file2.name, h_file1.name)
|
||||
info2.check_returncode()
|
||||
out2 = info2.stdout.decode('utf-8').strip()
|
||||
self.assertEqual('', info2.stderr.decode('utf-8').strip())
|
||||
result2 = self.runMkenumsWithTemplate(template_contents,
|
||||
h_file2.name, h_file1.name)
|
||||
self.assertEqual('', result2.err)
|
||||
|
||||
# The output should be the same.
|
||||
self.assertEqual(out1, out2)
|
||||
self.assertEqual(result1.out, result2.out)
|
||||
|
||||
def test_no_nick(self):
|
||||
"""Test trigraphs with a desc but no nick. Issue #1360."""
|
||||
@ -366,9 +371,9 @@ comment: {standard_bottom_comment}
|
||||
GEGL_SAMPLER_NEAREST = 0, /*< desc="nearest" >*/
|
||||
} GeglSamplerType;
|
||||
'''
|
||||
(info, out, err, subs) = self.runMkenumsWithHeader(h_contents)
|
||||
self.assertEqual('', err)
|
||||
self.assertSingleEnum(out, subs, 'GeglSamplerType',
|
||||
result = self.runMkenumsWithHeader(h_contents)
|
||||
self.assertEqual('', result.err)
|
||||
self.assertSingleEnum(result, 'GeglSamplerType',
|
||||
'gegl_sampler_type', 'GEGL_SAMPLER_TYPE',
|
||||
'SAMPLER_TYPE', 'GEGL', 'enum', 'Enum',
|
||||
'ENUM', 'GEGL_SAMPLER_NEAREST', 'nearest', '0')
|
||||
|
Loading…
Reference in New Issue
Block a user