예제 #1
0
def test_CommandLineApp_sigterm_handler():
    csboilerplate.CommandLineApp(noop, sigterm_handler=False)
    assert signal.getsignal(signal.SIGTERM) == signal.SIG_DFL
    csboilerplate.CommandLineApp(noop)
    assert callable(signal.getsignal(signal.SIGTERM))
    with pytest.raises(SystemExit):
        signal.getsignal(signal.SIGTERM)('signal', 'frame')
    csboilerplate.CommandLineApp(noop, sigterm_handler=noop)
    assert signal.getsignal(signal.SIGTERM) == noop
예제 #2
0
def test_CommandLineApp_interrupt():
    interrupted = Mock(side_effect=KeyboardInterrupt)
    exit = Mock(side_effect=SystemExit)
    app = csboilerplate.CommandLineApp(interrupted, exit_handler=exit)
    with pytest.raises(SystemExit):
        app()
    exit.assert_called_once_with(app, 'KeyboardInterrupt')
예제 #3
0
def test_CommandLineApp_argparser():
    app = csboilerplate.CommandLineApp(noop)
    assert isinstance(app.argparser, argparse.ArgumentParser)
    assert app.args is None
    with pytest.raises(SystemExit):
        app()
    assert isinstance(app.args, argparse.Namespace)
예제 #4
0
def test_CommandLineApp():
    dummy_main = Mock()
    app = csboilerplate.CommandLineApp(dummy_main)
    assert callable(app)
    dummy_main.assert_not_called()
    with pytest.raises(SystemExit):
        app()
    dummy_main.assert_called_once_with(app)
예제 #5
0
def test_CommandLineApp_uncaught_exception():
    broken = Mock(side_effect=ValueError)
    app = csboilerplate.CommandLineApp(broken)
    logger_exception = Mock()
    app.logger.exception = logger_exception
    with pytest.raises(SystemExit) as excinfo:
        app()
    assert excinfo.value.code == 'uncaught exception'
    logger_exception.assert_called_once()
    assert isinstance(logger_exception.call_args[0][0], ValueError)
예제 #6
0
def test_CommandLineApp_exit_handler():
    exit = Mock()
    app = csboilerplate.CommandLineApp(noop, exit_handler=exit)
    exit.assert_not_called()
    app()
    exit.assert_called_once_with(app)
    app.exit()
    exit.assert_called_with(app)
    assert exit.call_count == 2
    app.exit(error_message='error')
    exit.assert_called_with(app, error_message='error')
예제 #7
0
def test_CommandLineApp_logging_config(basicConfig):
    app = csboilerplate.CommandLineApp(noop)
    app.logging_config(log_level=0)
    assert basicConfig.call_args[1]['level'] == logging.WARNING
    app.logging_config(log_level=1)
    assert basicConfig.call_args[1]['level'] == logging.INFO
    app.logging_config(log_level=2)
    assert basicConfig.call_args[1]['level'] == logging.DEBUG
    with pytest.raises(IndexError):
        app.logging_config(log_level=3)
    app.logging_config(handlers=[42])
    assert basicConfig.call_args[1]['handlers'] == [42]
    app.logging_config(log_level=3, log_levels=[0, 1, 2, 3])
    assert basicConfig.call_args[1]['level'] == 3
예제 #8
0
def test_CommandLineApp_name():
    app = csboilerplate.CommandLineApp(noop, name='test_name')
    assert app.name == 'test_name'
예제 #9
0
def test_CommandLineApp_attribute_defaults():
    app = csboilerplate.CommandLineApp(noop)
    assert app.name == sys.argv[0]
    assert app.exit is sys.exit