Skip to content

Commit

Permalink
Fixed tests with usage of capfd instead of stdout from Popen
Browse files Browse the repository at this point in the history
  • Loading branch information
edyan committed May 22, 2021
1 parent d132d0e commit 59a04bc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ before_script:
- ./cc-test-reporter before-build

# command to run tests
script: py.test --no-cov --capture=no
script: py.test

after_script:
- coverage xml
Expand Down
32 changes: 16 additions & 16 deletions stakkr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
Give all options to manage services to be launched, stopped, etc.
"""

import sys
import click
from os import getcwd
from sys import argv, exit, stdout, stderr
from click.core import Context
from stakkr.docker_actions import get_running_containers_names
from stakkr.aliases import get_aliases
Expand Down Expand Up @@ -86,7 +86,7 @@ def exec_cmd(ctx: Context, user: str, container: str, command: tuple, tty: bool,
@click.pass_context
def restart(ctx: Context, container: str, pull: bool, recreate: bool, proxy: bool):
"""See command Help."""
print(click.style('[RESTARTING]', fg='green') + ' your stakkr services')
stdout.write(click.style('[RESTARTING]', fg='green') + ' your stakkr services\n')
try:
ctx.invoke(stop, container=container, proxy=proxy)
except Exception:
Expand All @@ -104,8 +104,8 @@ def services(ctx):

from stakkr.stakkr_compose import get_available_services

print('Available services usable in stakkr.yml ', end='')
print('({} = disabled) : '.format(click.style('✘', fg='red')))
stdout.write('Available services usable in stakkr.yml ')
stdout.write('({} = disabled) : \n'.format(click.style('✘', fg='red')))

svcs = ctx.obj['STAKKR'].get_config()['services']
enabled_svcs = [svc for svc, opts in svcs.items() if opts['enabled'] is True]
Expand All @@ -116,7 +116,7 @@ def services(ctx):
version = svcs[available_svc]['version']
sign = click.style(str(version), fg='green')

print(' - {} ({})'.format(available_svc, sign))
stdout.write(' - {} ({})\n'.format(available_svc, sign))


@stakkr.command(help="""Download a pack of services from github (see github) containing services.
Expand All @@ -136,7 +136,7 @@ def services_add(ctx: Context, package: str, name: str):
success, message = install(services_dir, package, name)
if success is False:
click.echo(click.style(message, fg='red'))
sys.exit(1)
exit(1)

stdout_msg = 'Package "' + click.style(package, fg='green') + '" installed successfully'
if message is not None:
Expand All @@ -155,7 +155,7 @@ def services_update(ctx):
project_dir = _get_project_dir(ctx.obj['CONFIG'])
services_dir = '{}/services'.format(project_dir)
update_all(services_dir)
print(click.style('Packages updated', fg='green'))
stdout.write(click.style('Packages updated\n', fg='green'))


@stakkr.command(help="Start all (or a single as CONTAINER) container(s) defined in compose.ini")
Expand All @@ -166,7 +166,7 @@ def services_update(ctx):
@click.pass_context
def start(ctx: Context, container: str, pull: bool, recreate: bool, proxy: bool):
"""See command Help."""
print(click.style('[STARTING]', fg='green') + ' your stakkr services')
stdout.write(click.style('[STARTING]', fg='green') + ' your stakkr services\n')

ctx.obj['STAKKR'].start(container, pull, recreate, proxy)
_show_status(ctx)
Expand All @@ -185,7 +185,7 @@ def status(ctx):
@click.pass_context
def stop(ctx: Context, container: str, proxy: bool):
"""See command Help."""
print(click.style('[STOPPING]', fg='yellow') + ' your stakkr services')
stdout.write(click.style('[STOPPING]', fg='yellow') + ' your stakkr services\n')
ctx.obj['STAKKR'].stop(container, proxy)


Expand All @@ -202,12 +202,12 @@ def _get_cmd_user(user: str, container: str):
def _show_status(ctx):
services_ports = ctx.obj['STAKKR'].get_services_urls()
if services_ports == '':
print('\nServices Status:')
stdout.write('\nServices Status:\n')
ctx.invoke(status)
return

print('\nServices URLs :')
print(services_ports)
stdout.write('\nServices URLs :\n')
stdout.write(services_ports)


def _get_project_dir(config: str):
Expand All @@ -223,7 +223,7 @@ def _get_project_dir(config: str):

def debug_mode():
"""Guess if we are in debug mode, useful to display runtime errors."""
if '--debug' in sys.argv or '-d' in sys.argv:
if '--debug' in argv or '-d' in argv:
return True

return False
Expand Down Expand Up @@ -268,13 +268,13 @@ def _f(ctx: Context, extra_args: tuple, tty: bool):
|______|_| \_\_| \_\\____/|_| \_\
""", fg='yellow')
msg += click.style('{}'.format(error), fg='red')
print(msg + '\n', file=sys.stderr)
msg += click.style('{}\n'.format(error), fg='red')
stderr.write(msg)

if debug_mode() is True:
raise error

sys.exit(1)
exit(1)


if __name__ == '__main__':
Expand Down
7 changes: 4 additions & 3 deletions stakkr/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Manage public proxy to expose containers."""

import click
from sys import stdout
from docker.errors import DockerException
from stakkr import docker_actions as docker
from stakkr.file_utils import get_dir
Expand All @@ -24,8 +25,8 @@ def __init__(self,
def start(self, stakkr_network: str = None):
"""Start stakkr proxy if stopped."""
if docker.container_running(self.ct_name) is False:
msg = ' traefik (Dashboard: http://traefik.docker.localhost)'
print(click.style('[STARTING]', fg='green') + msg)
msg = ' traefik (Dashboard: http://traefik.docker.localhost)\n'
stdout.write(click.style('[STARTING]', fg='green') + msg)
self._start_container()

# Connect it to network if asked
Expand All @@ -37,7 +38,7 @@ def stop(self):
if docker.container_running(self.ct_name) is False:
return

print(click.style('[STOPPING]', fg='green') + ' traefik')
stdout.write(click.style('[STOPPING]', fg='green') + ' traefik\n')
proxy_ct = self.docker_client.containers.get(self.ct_name)
proxy_ct.stop()

Expand Down
4 changes: 2 additions & 2 deletions stakkr/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def _copy_file(project_dir: str, source_file: str, force: bool):
try:
shutil.copy(full_path, dest_file)
except Exception:
msg = "Error trying to copy {} .. check that the file is there ..."
print(msg.format(full_path), file=sys.stderr)
msg = "Error trying to copy {} .. check that the file is there ...\n"
sys.stderr.write(msg.format(full_path))


def _recipe_get_config(recipe: str):
Expand Down

0 comments on commit 59a04bc

Please sign in to comment.