Bug 1253203 - Normalize path separators in MozbuildObject
The upcoming move of the configure.py initialization to sandboxed
moz.configure changes the path separators for topsrcdir and topobjdir
from native to always use forward-slashes, which confuses the hell out
of the test_base.py test. Settle the issue by declaring that
MozbuildObject will always use forward-slashed paths for topsrcdir
and topobjdir.
--- a/python/mozbuild/mozbuild/base.py
+++ b/python/mozbuild/mozbuild/base.py
@@ -75,24 +75,24 @@ class MozbuildObject(ProcessExecutionMix
"""
def __init__(self, topsrcdir, settings, log_manager, topobjdir=None):
"""Create a new Mozbuild object instance.
Instances are bound to a source directory, a ConfigSettings instance,
and a LogManager instance. The topobjdir may be passed in as well. If
it isn't, it will be calculated from the active mozconfig.
"""
- self.topsrcdir = topsrcdir
+ self.topsrcdir = mozpath.normsep(topsrcdir)
self.settings = settings
self.populate_logger()
self.log_manager = log_manager
self._make = None
- self._topobjdir = topobjdir
+ self._topobjdir = mozpath.normsep(topobjdir) if topobjdir else topobjdir
self._mozconfig = None
self._config_guess_output = None
self._config_environment = None
self._virtualenv_manager = None
@classmethod
def from_environment(cls, cwd=None, detect_virtualenv_mozinfo=True):
"""Create a MozbuildObject by detecting the proper one from the env.
@@ -179,19 +179,20 @@ class MozbuildObject(ProcessExecutionMix
if topobjdir and config_topobjdir:
if current_project:
config_topobjdir = os.path.join(config_topobjdir, current_project)
_config_topobjdir = config_topobjdir
if not samepath(topobjdir, _config_topobjdir):
raise ObjdirMismatchException(topobjdir, _config_topobjdir)
+ topsrcdir = mozpath.normsep(topsrcdir)
topobjdir = topobjdir or config_topobjdir
if topobjdir:
- topobjdir = os.path.normpath(topobjdir)
+ topobjdir = mozpath.normsep(os.path.normpath(topobjdir))
if topsrcdir == topobjdir:
raise BadEnvironmentException('The object directory appears '
'to be the same as your source directory (%s). This build '
'configuration is not supported.' % topsrcdir)
# If we can't resolve topobjdir, oh well. The constructor will figure
# it out via config.guess.
@@ -205,17 +206,17 @@ class MozbuildObject(ProcessExecutionMix
if '@CONFIG_GUESS@' in topobjdir:
topobjdir = topobjdir.replace('@CONFIG_GUESS@',
MozbuildObject.resolve_config_guess(mozconfig, topsrcdir))
if not os.path.isabs(topobjdir):
topobjdir = os.path.abspath(os.path.join(topsrcdir, topobjdir))
- return os.path.normpath(topobjdir)
+ return mozpath.normsep(os.path.normpath(topobjdir))
@property
def topobjdir(self):
if self._topobjdir is None:
self._topobjdir = MozbuildObject.resolve_mozconfig_topobjdir(
self.topsrcdir, self.mozconfig, default='obj-@CONFIG_GUESS@')
return self._topobjdir
--- a/python/mozbuild/mozbuild/test/test_base.py
+++ b/python/mozbuild/mozbuild/test/test_base.py
@@ -23,16 +23,17 @@ from mozbuild.base import (
MachCommandBase,
MozbuildObject,
ObjdirMismatchException,
PathArgument,
)
from mozbuild.backend.configenvironment import ConfigEnvironment
from buildconfig import topsrcdir, topobjdir
+import mozpack.path as mozpath
curdir = os.path.dirname(__file__)
log_manager = LoggingManager()
class TestMozbuildObject(unittest.TestCase):
def setUp(self):
@@ -54,28 +55,29 @@ class TestMozbuildObject(unittest.TestCa
with NamedTemporaryFile() as mozconfig:
os.environ[b'MOZCONFIG'] = mozconfig.name
self.assertIsNotNone(base.topobjdir)
self.assertEqual(len(base.topobjdir.split()), 1)
self.assertTrue(base.topobjdir.endswith(base._config_guess))
self.assertTrue(os.path.isabs(base.topobjdir))
- self.assertTrue(base.topobjdir.startswith(topsrcdir))
+ print(base.topobjdir, base.topsrcdir)
+ self.assertTrue(base.topobjdir.startswith(base.topsrcdir))
def test_objdir_trailing_slash(self):
"""Trailing slashes in topobjdir should be removed."""
base = self.get_base()
with NamedTemporaryFile() as mozconfig:
mozconfig.write('mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/foo/')
mozconfig.flush()
os.environ[b'MOZCONFIG'] = mozconfig.name
- self.assertEqual(base.topobjdir, os.path.join(base.topsrcdir,
+ self.assertEqual(base.topobjdir, mozpath.join(base.topsrcdir,
'foo'))
self.assertTrue(base.topobjdir.endswith('foo'))
def test_objdir_config_status(self):
"""Ensure @CONFIG_GUESS@ is handled when loading mozconfig."""
base = self.get_base()
cmd = base._normalize_command(
[os.path.join(topsrcdir, 'build', 'autoconf', 'config.guess')],
@@ -108,30 +110,30 @@ class TestMozbuildObject(unittest.TestCa
), fh)
os.environ[b'MOZCONFIG'] = mozconfig.encode('utf-8')
os.chdir(topobjdir)
obj = MozbuildObject.from_environment(
detect_virtualenv_mozinfo=False)
- self.assertEqual(obj.topobjdir, topobjdir)
+ self.assertEqual(obj.topobjdir, mozpath.normsep(topobjdir))
finally:
os.chdir(self._old_cwd)
shutil.rmtree(d)
def test_relative_objdir(self):
"""Relative defined objdirs are loaded properly."""
d = os.path.realpath(tempfile.mkdtemp())
try:
mozconfig = os.path.join(d, 'mozconfig')
with open(mozconfig, 'wt') as fh:
fh.write('mk_add_options MOZ_OBJDIR=./objdir')
- topobjdir = os.path.join(d, 'objdir')
+ topobjdir = mozpath.join(d, 'objdir')
os.mkdir(topobjdir)
mozinfo = os.path.join(topobjdir, 'mozinfo.json')
with open(mozinfo, 'wt') as fh:
json.dump(dict(
topsrcdir=d,
mozconfig=mozconfig,
), fh)
@@ -211,18 +213,18 @@ class TestMozbuildObject(unittest.TestCa
context.cwd = topobjdir
context.topdir = topsrcdir
context.settings = None
context.log_manager = None
context.detect_virtualenv_mozinfo=False
o = MachCommandBase(context)
- self.assertEqual(o.topobjdir, topobjdir)
- self.assertEqual(o.topsrcdir, topsrcdir)
+ self.assertEqual(o.topobjdir, mozpath.normsep(topobjdir))
+ self.assertEqual(o.topsrcdir, mozpath.normsep(topsrcdir))
finally:
os.chdir(self._old_cwd)
shutil.rmtree(d)
def test_objdir_is_srcdir_rejected(self):
"""Ensure the srcdir configurations are rejected."""
d = os.path.realpath(tempfile.mkdtemp())