[cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list' 3.25
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 29 Mar 2017 11:14:53 +0200
branch3.25
changeset 12120 707139a9eb00
parent 12119 c4ed3f6463f6
child 12121 86ec847835cd
[cwctl] Drop 'cubicweb_' prefix when displaying cubes with 'cubicweb-ctl list' consistently with newcube / create commands. This is an alternate implementation to backed out changeset fe995d56c949. Closes #17054738
cubicweb/cwctl.py
cubicweb/test/unittest_cwctl.py
--- a/cubicweb/cwctl.py	Tue Mar 28 17:12:47 2017 +0200
+++ b/cubicweb/cwctl.py	Wed Mar 29 11:14:53 2017 +0200
@@ -89,6 +89,18 @@
     return modes
 
 
+def available_cube_names(cwcfg):
+    """Return a list of available cube names, with 'cubicweb_' prefix dropped.
+    """
+    def drop_prefix(cube):
+        prefix = 'cubicweb_'
+        if cube.startswith(prefix):
+            cube = cube[len(prefix):]
+        return cube
+
+    return [drop_prefix(cube) for cube in cwcfg.available_cubes()]
+
+
 class InstanceCommand(Command):
     """base class for command taking 0 to n instance id as arguments
     (0 meaning all registered instances)
@@ -220,14 +232,15 @@
             cfgpb = ConfigurationProblem(cwcfg)
             try:
                 cubesdir = pathsep.join(cwcfg.cubes_search_path())
-                namesize = max(len(x) for x in cwcfg.available_cubes())
+                cube_names = available_cube_names(cwcfg)
+                namesize = max(len(x) for x in cube_names)
             except ConfigurationError as ex:
                 print('No cubes available:', ex)
             except ValueError:
                 print('No cubes available in %s' % cubesdir)
             else:
                 print('Available cubes (%s):' % cubesdir)
-                for cube in cwcfg.available_cubes():
+                for cube in cube_names:
                     try:
                         tinfo = cwcfg.cube_pkginfo(cube)
                         tversion = tinfo.version
@@ -360,7 +373,7 @@
         except ConfigurationError as ex:
             print(ex)
             print('\navailable cubes:', end=' ')
-            print(', '.join(cwcfg.available_cubes()))
+            print(', '.join(available_cube_names(cwcfg)))
             return
         # create the registry directory for this instance
         print('\n'+underline_title('Creating the instance %s' % appid))
--- a/cubicweb/test/unittest_cwctl.py	Tue Mar 28 17:12:47 2017 +0200
+++ b/cubicweb/test/unittest_cwctl.py	Wed Mar 29 11:14:53 2017 +0200
@@ -23,14 +23,21 @@
 
 from six import PY2
 
+from mock import patch
+
 from cubicweb.cwconfig import CubicWebConfiguration
 from cubicweb.cwctl import ListCommand
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.server.migractions import ServerMigrationHelper
 
+import unittest_cwconfig
+
 
 class CubicWebCtlTC(unittest.TestCase):
 
+    setUpClass = unittest_cwconfig.CubicWebConfigurationTC.setUpClass
+    tearDownClass = unittest_cwconfig.CubicWebConfigurationTC.tearDownClass
+
     def setUp(self):
         self.stream = BytesIO() if PY2 else StringIO()
         sys.stdout = self.stream
@@ -38,8 +45,12 @@
     def tearDown(self):
         sys.stdout = sys.__stdout__
 
-    def test_list(self):
+    @patch('pkg_resources.iter_entry_points', side_effect=unittest_cwconfig.iter_entry_points)
+    def test_list(self, mock_iter_entry_points):
         ListCommand(None).run([])
+        self.assertNotIn('cubicweb_', self.stream.getvalue())
+        mock_iter_entry_points.assert_called_once_with(
+            group='cubicweb.cubes', name=None)
 
     def test_list_configurations(self):
         ListCommand(None).run(['configurations'])