Merge branch 'fix-mkenums-genmarshal-test-windows' into 'master'

GObject: Fix mkenums.py and genmarshal.py tests on Windows

See merge request GNOME/glib!948
This commit is contained in:
Philip Withnall 2019-06-24 16:47:30 +00:00
commit c411d0aa6d
2 changed files with 42 additions and 12 deletions

View File

@ -24,6 +24,7 @@ import collections
import os import os
import shutil import shutil
import subprocess import subprocess
import sys
import tempfile import tempfile
from textwrap import dedent from textwrap import dedent
import unittest import unittest
@ -46,9 +47,13 @@ class TestGenmarshal(unittest.TestCase):
parsing and generation code out into a library and unit test that, and parsing and generation code out into a library and unit test that, and
convert this test to just check command line behaviour. convert this test to just check command line behaviour.
""" """
# Track the cwd, we want to back out to that to clean up our tempdir
cwd = ''
def setUp(self): def setUp(self):
self.timeout_seconds = 10 # seconds per test self.timeout_seconds = 10 # seconds per test
self.tmpdir = tempfile.TemporaryDirectory() self.tmpdir = tempfile.TemporaryDirectory()
self.cwd = os.getcwd()
os.chdir(self.tmpdir.name) os.chdir(self.tmpdir.name)
print('tmpdir:', self.tmpdir.name) print('tmpdir:', self.tmpdir.name)
if 'G_TEST_BUILDDIR' in os.environ: if 'G_TEST_BUILDDIR' in os.environ:
@ -60,10 +65,17 @@ class TestGenmarshal(unittest.TestCase):
print('genmarshal:', self.__genmarshal) print('genmarshal:', self.__genmarshal)
def tearDown(self): def tearDown(self):
os.chdir(self.cwd)
self.tmpdir.cleanup() self.tmpdir.cleanup()
def runGenmarshal(self, *args): def runGenmarshal(self, *args):
argv = [self.__genmarshal] argv = [self.__genmarshal]
# shebang lines are not supported on native
# Windows consoles
if os.name == 'nt':
argv.insert(0, sys.executable)
argv.extend(args) argv.extend(args)
print('Running:', argv) print('Running:', argv)
@ -71,13 +83,15 @@ class TestGenmarshal(unittest.TestCase):
env['LC_ALL'] = 'C.UTF-8' env['LC_ALL'] = 'C.UTF-8'
print('Environment:', env) print('Environment:', env)
# We want to ensure consistent line endings...
info = subprocess.run(argv, timeout=self.timeout_seconds, info = subprocess.run(argv, timeout=self.timeout_seconds,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
env=env) env=env,
universal_newlines=True)
info.check_returncode() info.check_returncode()
out = info.stdout.decode('utf-8').strip() out = info.stdout.strip()
err = info.stderr.decode('utf-8').strip() err = info.stderr.strip()
# Known substitutions for standard boilerplate # Known substitutions for standard boilerplate
subs = { subs = {
@ -156,7 +170,8 @@ class TestGenmarshal(unittest.TestCase):
def runGenmarshalWithList(self, list_contents, *args): def runGenmarshalWithList(self, list_contents, *args):
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.list') as list_file: suffix='.list',
delete=False) as list_file:
# Write out the list. # Write out the list.
list_file.write(list_contents.encode('utf-8')) list_file.write(list_contents.encode('utf-8'))
print(list_file.name + ':', list_contents) print(list_file.name + ':', list_contents)

View File

@ -24,6 +24,7 @@ import collections
import os import os
import shutil import shutil
import subprocess import subprocess
import sys
import tempfile import tempfile
import textwrap import textwrap
import unittest import unittest
@ -46,11 +47,14 @@ class TestMkenums(unittest.TestCase):
parsing and generation code out into a library and unit test that, and parsing and generation code out into a library and unit test that, and
convert this test to just check command line behaviour. convert this test to just check command line behaviour.
""" """
# Track the cwd, we want to back out to that to clean up our tempdir
cwd = ''
rspfile = False rspfile = False
def setUp(self): def setUp(self):
self.timeout_seconds = 10 # seconds per test self.timeout_seconds = 10 # seconds per test
self.tmpdir = tempfile.TemporaryDirectory() self.tmpdir = tempfile.TemporaryDirectory()
self.cwd = os.getcwd()
os.chdir(self.tmpdir.name) os.chdir(self.tmpdir.name)
print('tmpdir:', self.tmpdir.name) print('tmpdir:', self.tmpdir.name)
if 'G_TEST_BUILDDIR' in os.environ: if 'G_TEST_BUILDDIR' in os.environ:
@ -62,6 +66,7 @@ class TestMkenums(unittest.TestCase):
print('rspfile: {}, mkenums:'.format(self.rspfile), self.__mkenums) print('rspfile: {}, mkenums:'.format(self.rspfile), self.__mkenums)
def tearDown(self): def tearDown(self):
os.chdir(self.cwd)
self.tmpdir.cleanup() self.tmpdir.cleanup()
def _write_rspfile(self, argv): def _write_rspfile(self, argv):
@ -72,13 +77,19 @@ class TestMkenums(unittest.TestCase):
print('Response file contains:', contents) print('Response file contains:', contents)
f.write(contents) f.write(contents)
f.flush() f.flush()
return f.name return f.name
def runMkenums(self, *args): def runMkenums(self, *args):
if self.rspfile: if self.rspfile:
rspfile = self._write_rspfile(args) rspfile = self._write_rspfile(args)
args = ['@' + rspfile] args = ['@' + rspfile]
argv = [self.__mkenums] argv = [self.__mkenums]
# shebang lines are not supported on native
# Windows consoles
if os.name == 'nt':
argv.insert(0, sys.executable)
argv.extend(args) argv.extend(args)
print('Running:', argv) print('Running:', argv)
@ -86,13 +97,15 @@ class TestMkenums(unittest.TestCase):
env['LC_ALL'] = 'C.UTF-8' env['LC_ALL'] = 'C.UTF-8'
print('Environment:', env) print('Environment:', env)
# We want to ensure consistent line endings...
info = subprocess.run(argv, timeout=self.timeout_seconds, info = subprocess.run(argv, timeout=self.timeout_seconds,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
env=env) env=env,
universal_newlines=True)
info.check_returncode() info.check_returncode()
out = info.stdout.decode('utf-8').strip() out = info.stdout.strip()
err = info.stderr.decode('utf-8').strip() err = info.stderr.strip()
# Known substitutions for standard boilerplate # Known substitutions for standard boilerplate
subs = { subs = {
@ -111,7 +124,8 @@ class TestMkenums(unittest.TestCase):
def runMkenumsWithTemplate(self, template_contents, *args): def runMkenumsWithTemplate(self, template_contents, *args):
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.template') as template_file: suffix='.template',
delete=False) as template_file:
# Write out the template. # Write out the template.
template_file.write(template_contents.encode('utf-8')) template_file.write(template_contents.encode('utf-8'))
print(template_file.name + ':', template_contents) print(template_file.name + ':', template_contents)
@ -191,7 +205,8 @@ file-tail
def runMkenumsWithHeader(self, h_contents, encoding='utf-8'): def runMkenumsWithHeader(self, h_contents, encoding='utf-8'):
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='.h') as h_file: suffix='.h',
delete=False) as h_file:
# 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)
@ -381,9 +396,9 @@ comment: {standard_bottom_comment}
''' '''
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name, with tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='1.h') as h_file1, \ suffix='1.h', delete=False) as h_file1, \
tempfile.NamedTemporaryFile(dir=self.tmpdir.name, tempfile.NamedTemporaryFile(dir=self.tmpdir.name,
suffix='2.h') as h_file2: suffix='2.h', delete=False) as h_file2:
# Write out the headers. # Write out the headers.
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'))