Skip to content

Commit

Permalink
Fix list command names from importlib (#189)
Browse files Browse the repository at this point in the history
* Fix list command names from importlib

The compatibility code for importlib.metadata
returned names was not correct.

Closes: #188

* Add test for pifpaf list command

This adds a test that the pifpaf list
command exites successfully and contains
atleast one driver name.
  • Loading branch information
tobias-urdin authored Oct 11, 2024
1 parent 019aeac commit 022a041
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pifpaf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def _format_multiple_exceptions(e, debug=False):
DAEMONS = importlib.metadata.entry_points()['pifpaf.daemons']


def _daemons_names():
names = []
if hasattr(DAEMONS, 'names'):
names = DAEMONS.names
else:
for i in DAEMONS:
names.append(i.name)
return names


@click.group()
@click.option('--verbose/--quiet', help="Print mode details.")
@click.option('--debug', help="Show tracebacks on errors.", is_flag=True)
Expand Down Expand Up @@ -119,14 +129,14 @@ def main(ctx, verbose=False, debug=False, log_file=None,

@main.command(name="list")
def drivers_list():
for n in DAEMONS.keys():
for n in _daemons_names():
click.echo(n)


class RunGroup(click.MultiCommand):
@staticmethod
def list_commands(ctx):
return DAEMONS.keys()
return _daemons_names()

def get_command(self, ctx, name):
params = [click.Argument(["command"], nargs=-1)]
Expand Down
8 changes: 8 additions & 0 deletions pifpaf/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def test_global_urls_variable_old_format(self):
b"\"memcached://localhost:11217;memcached://localhost:11218\";",
env[b"export PIFPAF_URLS"])

def test_list_command(self):
c = subprocess.Popen(["pifpaf", "list"],
bufsize=0,
stdout=subprocess.PIPE)
(stdout, stderr) = c.communicate()
self.assertEqual(0, c.wait())
self.assertIn(b'memcached', stdout)

def test_non_existing_command(self):
# Keep PATH to just the one set by tox to run pifpaf
self.useFixture(fixtures.EnvironmentVariable(
Expand Down

0 comments on commit 022a041

Please sign in to comment.