Bug 1289949 - Make mozpack.path properly handle paths on mingw python. r=gps
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 28 Jul 2016 18:50:57 +0900
changeset 347467 b9adb0648dba49e02092a473de7b89167662e4c8
parent 347466 70e0031ebf1c1a07ff07c1b528336886731a5f7e
child 347468 ff43d3e4c4fc8732b460ded61155c586e9e32573
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1289949
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1289949 - Make mozpack.path properly handle paths on mingw python. r=gps Mingw python has a different os.path setup from native python, and has os.sep and os.altsep reversed. In that case, the normsep function was doing the wrong thing, leading to all sorts of problems. While fixing this, also ensure the corresponding unit test covers this peculiarity, even when running under the native win32 python.
python/mozbuild/mozpack/path.py
python/mozbuild/mozpack/test/test_path.py
--- a/python/mozbuild/mozpack/path.py
+++ b/python/mozbuild/mozpack/path.py
@@ -17,16 +17,18 @@ Also contains a few additional utilities
 
 def normsep(path):
     '''
     Normalize path separators, by using forward slashes instead of whatever
     os.sep is.
     '''
     if os.sep != '/':
         path = path.replace(os.sep, '/')
+    if os.altsep and os.altsep != '/':
+        path = path.replace(os.altsep, '/')
     return path
 
 
 def relpath(path, start):
     rel = normsep(os.path.relpath(path, start))
     return '' if rel == '.' else rel
 
 
--- a/python/mozbuild/mozpack/test/test_path.py
+++ b/python/mozbuild/mozpack/test/test_path.py
@@ -16,67 +16,69 @@ from mozpack.path import (
     rebase,
 )
 import unittest
 import mozunit
 import os
 
 
 class TestPath(unittest.TestCase):
+    SEP = os.sep
+
     def test_relpath(self):
         self.assertEqual(relpath('foo', 'foo'), '')
-        self.assertEqual(relpath(os.path.join('foo', 'bar'), 'foo/bar'), '')
-        self.assertEqual(relpath(os.path.join('foo', 'bar'), 'foo'), 'bar')
-        self.assertEqual(relpath(os.path.join('foo', 'bar', 'baz'), 'foo'),
+        self.assertEqual(relpath(self.SEP.join(('foo', 'bar')), 'foo/bar'), '')
+        self.assertEqual(relpath(self.SEP.join(('foo', 'bar')), 'foo'), 'bar')
+        self.assertEqual(relpath(self.SEP.join(('foo', 'bar', 'baz')), 'foo'),
                          'bar/baz')
-        self.assertEqual(relpath(os.path.join('foo', 'bar'), 'foo/bar/baz'),
+        self.assertEqual(relpath(self.SEP.join(('foo', 'bar')), 'foo/bar/baz'),
                          '..')
-        self.assertEqual(relpath(os.path.join('foo', 'bar'), 'foo/baz'),
+        self.assertEqual(relpath(self.SEP.join(('foo', 'bar')), 'foo/baz'),
                          '../bar')
         self.assertEqual(relpath('foo/', 'foo'), '')
         self.assertEqual(relpath('foo/bar/', 'foo'), 'bar')
 
     def test_join(self):
         self.assertEqual(join('foo', 'bar', 'baz'), 'foo/bar/baz')
         self.assertEqual(join('foo', '', 'bar'), 'foo/bar')
         self.assertEqual(join('', 'foo', 'bar'), 'foo/bar')
         self.assertEqual(join('', 'foo', '/bar'), '/bar')
 
     def test_normpath(self):
-        self.assertEqual(normpath(os.path.join('foo', 'bar', 'baz',
-                                               '..', 'qux')), 'foo/bar/qux')
+        self.assertEqual(normpath(self.SEP.join(('foo', 'bar', 'baz',
+                                                 '..', 'qux'))), 'foo/bar/qux')
 
     def test_dirname(self):
         self.assertEqual(dirname('foo/bar/baz'), 'foo/bar')
         self.assertEqual(dirname('foo/bar'), 'foo')
         self.assertEqual(dirname('foo'), '')
         self.assertEqual(dirname('foo/bar/'), 'foo/bar')
 
     def test_commonprefix(self):
-        self.assertEqual(commonprefix([os.path.join('foo', 'bar', 'baz'),
+        self.assertEqual(commonprefix([self.SEP.join(('foo', 'bar', 'baz')),
                                        'foo/qux', 'foo/baz/qux']), 'foo/')
-        self.assertEqual(commonprefix([os.path.join('foo', 'bar', 'baz'),
+        self.assertEqual(commonprefix([self.SEP.join(('foo', 'bar', 'baz')),
                                        'foo/qux', 'baz/qux']), '')
 
     def test_basename(self):
         self.assertEqual(basename('foo/bar/baz'), 'baz')
         self.assertEqual(basename('foo/bar'), 'bar')
         self.assertEqual(basename('foo'), 'foo')
         self.assertEqual(basename('foo/bar/'), '')
 
     def test_split(self):
-        self.assertEqual(split(os.path.join('foo', 'bar', 'baz')),
+        self.assertEqual(split(self.SEP.join(('foo', 'bar', 'baz'))),
                          ['foo', 'bar', 'baz'])
 
     def test_splitext(self):
-        self.assertEqual(splitext(os.path.join('foo', 'bar', 'baz.qux')),
+        self.assertEqual(splitext(self.SEP.join(('foo', 'bar', 'baz.qux'))),
                          ('foo/bar/baz', '.qux'))
 
     def test_basedir(self):
-        foobarbaz = os.path.join('foo', 'bar', 'baz')
+        foobarbaz = self.SEP.join(('foo', 'bar', 'baz'))
         self.assertEqual(basedir(foobarbaz, ['foo', 'bar', 'baz']), 'foo')
         self.assertEqual(basedir(foobarbaz, ['foo', 'foo/bar', 'baz']),
                          'foo/bar')
         self.assertEqual(basedir(foobarbaz, ['foo/bar', 'foo', 'baz']),
                          'foo/bar')
         self.assertEqual(basedir(foobarbaz, ['foo', 'bar', '']), 'foo')
         self.assertEqual(basedir(foobarbaz, ['bar', 'baz', '']), '')
 
@@ -114,10 +116,28 @@ class TestPath(unittest.TestCase):
         self.assertFalse(match('foo/nobar/baz.qux', 'foo/**/bar/**'))
         self.assertTrue(match('foo/bar', 'foo/**/bar/**'))
 
     def test_rebase(self):
         self.assertEqual(rebase('foo', 'foo/bar', 'bar/baz'), 'baz')
         self.assertEqual(rebase('foo', 'foo', 'bar/baz'), 'bar/baz')
         self.assertEqual(rebase('foo/bar', 'foo', 'baz'), 'bar/baz')
 
+
+if os.altsep:
+    class TestAltPath(TestPath):
+        SEP = os.altsep
+
+    class TestReverseAltPath(TestPath):
+        def setUp(self):
+            sep = os.sep
+            os.sep = os.altsep
+            os.altsep = sep
+
+        def tearDown(self):
+            self.setUp()
+
+    class TestAltReverseAltPath(TestReverseAltPath):
+        SEP = os.altsep
+
+
 if __name__ == '__main__':
     mozunit.main()