glib-mkenums: don't support @filename@/@basename@ in fhead/ftail

As discussed in
https://gitlab.gnome.org/GNOME/glib/merge_requests/135#note_253986 it
doesn't really make sense to support these outside the templates for any
particular header file. Leave them unsubstituted, with a warning.
This commit is contained in:
Will Thompson 2018-08-10 16:53:23 +01:00
parent cbd335c8c9
commit 7928fee2a8
No known key found for this signature in database
GPG Key ID: 3422DC0D7AD482A7
2 changed files with 91 additions and 20 deletions

View File

@ -415,12 +415,17 @@ def replace_specials(prod):
prod = prod.rstrip()
return prod
def warn_if_filename_basename_used(section, prod):
for substitution in ('\u0040filename\u0040',
'\u0040basename\u0040'):
if substitution in prod:
print_warning('{} used in {} section.'.format(substitution,
section))
if len(fhead) > 0:
prod = fhead
base = os.path.basename(options.args[0])
prod = prod.replace('\u0040filename\u0040', options.args[0])
prod = prod.replace('\u0040basename\u0040', base)
warn_if_filename_basename_used('file-header', prod)
prod = replace_specials(prod)
write_output(prod)
@ -712,10 +717,7 @@ for fname in sorted(options.args):
if len(ftail) > 0:
prod = ftail
base = os.path.basename(options.args[-1]) # FIXME, wrong
prod = prod.replace('\u0040filename\u0040', 'ARGV') # wrong too
prod = prod.replace('\u0040basename\u0040', base)
warn_if_filename_basename_used('file-tail', prod)
prod = replace_specials(prod)
write_output(prod)

View File

@ -24,6 +24,7 @@ import collections
import os
import subprocess
import tempfile
import textwrap
import unittest
import taptestrunner
@ -108,8 +109,6 @@ class TestMkenums(unittest.TestCase):
template_contents = '''
/*** BEGIN file-header ***/
file-header
filename: @filename@
basename: @basename@
/*** END file-header ***/
/*** BEGIN file-production ***/
@ -171,8 +170,6 @@ comment: @comment@
/*** BEGIN file-tail ***/
file-tail
filename: @filename@
basename: @basename@
/*** END file-tail ***/
'''
return self.runMkenumsWithTemplate(template_contents, *args)
@ -222,8 +219,6 @@ comment: {standard_top_comment}
file-header
filename: {filename}
basename: {basename}
file-production
filename: {filename}
basename: {basename}
@ -262,8 +257,6 @@ type: {type_lower}
Type: {type_camel}
TYPE: {type_upper}
file-tail
filename: ARGV
basename: {basename}
comment
comment: {standard_bottom_comment}
@ -274,6 +267,42 @@ comment: {standard_bottom_comment}
result = self.runMkenums('--help')
self.assertIn('usage: glib-mkenums', result.out)
def test_no_args(self):
"""Test running with no arguments at all."""
result = self.runMkenums()
self.assertEqual('', result.err)
self.assertEquals('''/* {standard_top_comment} */
/* {standard_bottom_comment} */'''.format(**result.subs),
result.out.strip())
def test_empty_template(self):
"""Test running with an empty template and no header files."""
result = self.runMkenumsWithTemplate('')
self.assertEqual('', result.err)
self.assertEquals('''/* {standard_top_comment} */
/* {standard_bottom_comment} */'''.format(**result.subs),
result.out.strip())
def test_no_headers(self):
"""Test running with a complete template, but no header files."""
result = self.runMkenumsWithAllSubstitutions()
self.assertEqual('', result.err)
self.assertEquals('''
comment
comment: {standard_top_comment}
file-header
file-tail
comment
comment: {standard_bottom_comment}
'''.format(**result.subs).strip(), result.out)
def test_empty_header(self):
"""Test an empty header."""
result = self.runMkenumsWithHeader('')
@ -284,11 +313,7 @@ comment: {standard_top_comment}
file-header
filename: {filename}
basename: {basename}
file-tail
filename: ARGV
basename: {basename}
comment
comment: {standard_bottom_comment}
@ -378,6 +403,50 @@ comment: {standard_bottom_comment}
'SAMPLER_TYPE', 'GEGL', 'enum', 'Enum',
'ENUM', 'GEGL_SAMPLER_NEAREST', 'nearest', '0')
def test_filename_basename_in_fhead_ftail(self):
template_contents = '''
/*** BEGIN file-header ***/
file-header
filename: @filename@
basename: @basename@
/*** END file-header ***/
/*** BEGIN comment ***/
comment
comment: @comment@
/*** END comment ***/
/*** BEGIN file-tail ***/
file-tail
filename: @filename@
basename: @basename@
/*** END file-tail ***/'''
result = self.runMkenumsWithTemplate(template_contents)
self.assertEqual(
textwrap.dedent(
'''
WARNING: @filename@ used in file-header section.
WARNING: @basename@ used in file-header section.
WARNING: @filename@ used in file-tail section.
WARNING: @basename@ used in file-tail section.
''').strip(),
result.err)
self.assertEqual('''
comment
comment: {standard_top_comment}
file-header
filename: @filename@
basename: @basename@
file-tail
filename: @filename@
basename: @basename@
comment
comment: {standard_bottom_comment}
'''.format(**result.subs).strip(), result.out)
if __name__ == '__main__':
unittest.main(testRunner=taptestrunner.TAPTestRunner())