Commits

Antoine Pitrou committed 7376f65

Add the basename property.

Comments (0)

Files changed (2)

         basename = basename.lstrip('.')
         return ['.' + suffix for suffix in basename.split('.')[1:]]
 
+    @property
+    def basename(self):
+        """
+        The final path component, minus its last suffix.
+        """
+        basename = self.name
+        suffix = self.suffix
+        if not suffix:
+            return basename
+        return basename[:-len(suffix)]
+
     def relative(self):
         """Return a new path without any drive and root.
         """
         self.assertEqual(P('a/Some name. Ending with a dot.').suffixes, [])
         self.assertEqual(P('/a/Some name. Ending with a dot.').suffixes, [])
 
+    def test_basename_common(self):
+        P = self.cls
+        self.assertEqual(P('').basename, '')
+        self.assertEqual(P('.').basename, '')
+        self.assertEqual(P('..').basename, '..')
+        self.assertEqual(P('/').basename, '')
+        self.assertEqual(P('a/b').basename, 'b')
+        self.assertEqual(P('a/b.py').basename, 'b')
+        self.assertEqual(P('a/.hgrc').basename, '.hgrc')
+        self.assertEqual(P('a/.hg.rc').basename, '.hg')
+        self.assertEqual(P('a/b.tar.gz').basename, 'b.tar')
+        self.assertEqual(P('a/Some name. Ending with a dot.').basename,
+                         'Some name. Ending with a dot.')
+
     def test_relative_common(self):
         P = self.cls
         p = P('a/b')
         self.assertEqual(P('c:a/Some name. Ending with a dot.').suffixes, [])
         self.assertEqual(P('c:/a/Some name. Ending with a dot.').suffixes, [])
 
+    def test_basename_common(self):
+        P = self.cls
+        self.assertEqual(P('c:').basename, '')
+        self.assertEqual(P('c:.').basename, '')
+        self.assertEqual(P('c:..').basename, '..')
+        self.assertEqual(P('c:/').basename, '')
+        self.assertEqual(P('c:a/b').basename, 'b')
+        self.assertEqual(P('c:a/b.py').basename, 'b')
+        self.assertEqual(P('c:a/.hgrc').basename, '.hgrc')
+        self.assertEqual(P('c:a/.hg.rc').basename, '.hg')
+        self.assertEqual(P('c:a/b.tar.gz').basename, 'b.tar')
+        self.assertEqual(P('c:a/Some name. Ending with a dot.').basename,
+                         'Some name. Ending with a dot.')
+
     def test_relative(self):
         P = self.cls
         p = P('c:a/b')