Python flexmock.flexmock() Examples

The following are 30 code examples of flexmock.flexmock(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flexmock , or try the search function .
Example #1
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_calls_full_command_with_input_file():
    full_command = ['foo', 'bar']
    processes = (flexmock(),)
    input_file = flexmock(name='test')
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=input_file,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command_with_processes(full_command, processes, input_file=input_file)

    assert output is None 
Example #2
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_calls_full_command_with_input_file():
    full_command = ['foo', 'bar']
    input_file = flexmock(name='test')
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=input_file,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command(full_command, input_file=input_file)

    assert output is None 
Example #3
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_calls_full_command_with_extra_environment():
    full_command = ['foo', 'bar']
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env={'a': 'b', 'c': 'd'},
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command(full_command, extra_environment={'c': 'd'})

    assert output is None 
Example #4
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_calls_full_command_with_working_directory():
    full_command = ['foo', 'bar']
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd='/working',
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command(full_command, working_directory='/working')

    assert output is None 
Example #5
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_without_run_to_completion_returns_process():
    full_command = ['foo', 'bar']
    process = flexmock()
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(process).once()
    flexmock(module).should_receive('log_outputs')

    assert module.execute_command(full_command, run_to_completion=False) == process 
Example #6
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_global_arguments_with_help_does_not_apply_default_subparsers():
    global_namespace = flexmock(verbosity='lots')
    action_namespace = flexmock()
    top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
    subparsers = flexmock(
        choices={
            'action': flexmock(
                parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
            ),
            'other': flexmock(),
        }
    )

    arguments = module.parse_global_arguments(
        ('--verbosity', 'lots', '--help'), top_level_parser, subparsers
    )

    assert arguments == global_namespace 
Example #7
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_subparser_arguments_consumes_multiple_subparser_arguments():
    action_namespace = flexmock(foo=True)
    other_namespace = flexmock(bar=3)
    subparsers = flexmock(
        choices={
            'action': flexmock(
                parse_known_args=lambda arguments: (action_namespace, ['--bar', '3'])
            ),
            'other': flexmock(parse_known_args=lambda arguments: (other_namespace, [])),
        }
    )

    arguments = module.parse_subparser_arguments(
        ('action', '--foo', 'true', 'other', '--bar', '3'), subparsers
    )

    assert arguments == {'action': action_namespace, 'other': other_namespace} 
Example #8
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_subparser_arguments_consumes_subparser_arguments_with_alias():
    action_namespace = flexmock(foo=True)
    action_subparser = flexmock(parse_known_args=lambda arguments: (action_namespace, []))
    subparsers = flexmock(
        choices={
            'action': action_subparser,
            '-a': action_subparser,
            'other': flexmock(),
            '-o': flexmock(),
        }
    )
    flexmock(module).SUBPARSER_ALIASES = {'action': ['-a'], 'other': ['-o']}

    arguments = module.parse_subparser_arguments(('-a', '--foo', 'true'), subparsers)

    assert arguments == {'action': action_namespace} 
Example #9
Source File: test_load.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_load_configuration_merges_include():
    builtins = flexmock(sys.modules['builtins'])
    builtins.should_receive('open').with_args('include.yaml').and_return(
        '''
        foo: bar
        baz: quux
        '''
    )
    builtins.should_receive('open').with_args('config.yaml').and_return(
        '''
        foo: override
        <<: !include include.yaml
        '''
    )

    assert module.load_configuration('config.yaml') == {'foo': 'override', 'baz': 'quux'} 
Example #10
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_calls_full_command():
    full_command = ['foo', 'bar']
    processes = (flexmock(),)
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command_with_processes(full_command, processes)

    assert output is None 
Example #11
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_calls_full_command_with_output_file():
    full_command = ['foo', 'bar']
    processes = (flexmock(),)
    output_file = flexmock(name='test')
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=output_file,
        stderr=module.subprocess.PIPE,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stderr=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command_with_processes(full_command, processes, output_file=output_file)

    assert output is None 
Example #12
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_global_arguments_consumes_global_arguments_before_subparser_name():
    global_namespace = flexmock(verbosity='lots')
    action_namespace = flexmock()
    top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
    subparsers = flexmock(
        choices={
            'action': flexmock(
                parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
            ),
            'other': flexmock(),
        }
    )

    arguments = module.parse_global_arguments(
        ('--verbosity', 'lots', 'action'), top_level_parser, subparsers
    )

    assert arguments == global_namespace 
Example #13
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_calls_full_command_with_extra_environment():
    full_command = ['foo', 'bar']
    processes = (flexmock(),)
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env={'a': 'b', 'c': 'd'},
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command_with_processes(
        full_command, processes, extra_environment={'c': 'd'}
    )

    assert output is None 
Example #14
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_calls_full_command_with_working_directory():
    full_command = ['foo', 'bar']
    processes = (flexmock(),)
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd='/working',
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command_with_processes(
        full_command, processes, working_directory='/working'
    )

    assert output is None 
Example #15
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_with_processes_kills_processes_on_error():
    full_command = ['foo', 'bar']
    process = flexmock(stdout=flexmock(read=lambda count: None))
    process.should_receive('poll')
    process.should_receive('kill').once()
    processes = (process,)
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
    flexmock(module).should_receive('log_outputs').never()

    with pytest.raises(subprocess.CalledProcessError):
        module.execute_command_with_processes(full_command, processes) 
Example #16
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_calls_full_command_with_output_file():
    full_command = ['foo', 'bar']
    output_file = flexmock(name='test')
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=output_file,
        stderr=module.subprocess.PIPE,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stderr=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command(full_command, output_file=output_file)

    assert output is None 
Example #17
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_execute_command_calls_full_command():
    full_command = ['foo', 'bar']
    flexmock(module.os, environ={'a': 'b'})
    flexmock(module.subprocess).should_receive('Popen').with_args(
        full_command,
        stdin=None,
        stdout=module.subprocess.PIPE,
        stderr=module.subprocess.STDOUT,
        shell=False,
        env=None,
        cwd=None,
    ).and_return(flexmock(stdout=None)).once()
    flexmock(module).should_receive('log_outputs')

    output = module.execute_command(full_command)

    assert output is None 
Example #18
Source File: test_pypi.py    From release-bot with GNU General Public License v3.0 6 votes vote down vote up
def pypi(self, tmpdir):
        conf = Configuration()
        path = str(tmpdir)
        src = Path(__file__).parent / "src/rlsbot-test"
        shutil.copy2(str(src / "setup.py"), path)
        shutil.copy2(str(src / "rlsbot_test.py"), path)
        self.run_cmd("git init .", work_directory=str(tmpdir))
        set_git_credentials(str(tmpdir), "Release Bot", "bot@example.com")
        self.run_cmd("git add .", work_directory=str(tmpdir))
        self.run_cmd("git commit -m 'initial commit'", work_directory=str(tmpdir))
        git_repo = Git(str(tmpdir), conf)
        pypi = PyPi(configuration, git_repo)
        (flexmock(pypi)
         .should_receive("upload")
         .replace_with(lambda x: None))
        return pypi 
Example #19
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_global_arguments_consumes_global_arguments_after_subparser_name():
    global_namespace = flexmock(verbosity='lots')
    action_namespace = flexmock()
    top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
    subparsers = flexmock(
        choices={
            'action': flexmock(
                parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
            ),
            'other': flexmock(),
        }
    )

    arguments = module.parse_global_arguments(
        ('action', '--verbosity', 'lots'), top_level_parser, subparsers
    )

    assert arguments == global_namespace 
Example #20
Source File: test_dispatch.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_call_hooks_calls_each_hook_and_collects_return_values():
    hooks = {'super_hook': flexmock(), 'other_hook': flexmock()}
    expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
    flexmock(module).should_receive('call_hook').and_return(
        expected_return_values['super_hook']
    ).and_return(expected_return_values['other_hook'])

    return_values = module.call_hooks('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)

    assert return_values == expected_return_values 
Example #21
Source File: test_execute.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_command_for_process_converts_sequence_command_to_string():
    process = flexmock(args=['foo', 'bar', 'baz'])

    assert module.command_for_process(process) == 'foo bar baz' 
Example #22
Source File: test_dispatch.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_call_hook_without_hook_config_skips_call():
    hooks = {'other_hook': flexmock()}
    test_module = sys.modules[__name__]
    flexmock(module).HOOK_NAME_TO_MODULE = {'super_hook': test_module}
    flexmock(test_module).should_receive('hook_function').never()

    module.call_hook('hook_function', hooks, 'prefix', 'super_hook', 55, value=66) 
Example #23
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_post_hook_error():
    flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
    flexmock(module).should_receive('run_configuration').and_return([])
    expected_logs = (flexmock(),)
    flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
    arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}

    logs = tuple(
        module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
    )

    assert expected_logs[0] in logs 
Example #24
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_parse_subparser_arguments_consumes_subparser_arguments_after_subparser_name():
    action_namespace = flexmock(foo=True)
    subparsers = flexmock(
        choices={
            'action': flexmock(parse_known_args=lambda arguments: (action_namespace, [])),
            'other': flexmock(),
        }
    )

    arguments = module.parse_subparser_arguments(('action', '--foo', 'true'), subparsers)

    assert arguments == {'action': action_namespace} 
Example #25
Source File: test_arguments.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_parse_subparser_arguments_consumes_subparser_arguments_before_subparser_name():
    action_namespace = flexmock(foo=True)
    subparsers = flexmock(
        choices={
            'action': flexmock(parse_known_args=lambda arguments: (action_namespace, [])),
            'other': flexmock(),
        }
    )

    arguments = module.parse_subparser_arguments(('--foo', 'true', 'action'), subparsers)

    assert arguments == {'action': action_namespace} 
Example #26
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
    flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
        ['baz']
    )
    flexmock(module.sys.stdout).should_receive('write').with_args('["foo", "bar", "baz"]').once()
    arguments = {}

    tuple(
        module.collect_configuration_run_summary_logs(
            {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
        )
    ) 
Example #27
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_run_configuration_error():
    flexmock(module.validate).should_receive('guard_configuration_contains_repository')
    flexmock(module).should_receive('run_configuration').and_return(
        [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
    )
    flexmock(module).should_receive('make_error_log_records').and_return([])
    arguments = {}

    logs = tuple(
        module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
    )

    assert {log.levelno for log in logs} == {logging.CRITICAL} 
Example #28
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_info_for_success_with_list():
    flexmock(module).should_receive('run_configuration').and_return([])
    arguments = {'list': flexmock(repository='repo', archive=None)}

    logs = tuple(
        module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
    )

    assert {log.levelno for log in logs} == {logging.INFO} 
Example #29
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
    flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
        ValueError
    )
    expected_logs = (flexmock(),)
    flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
    arguments = {'list': flexmock(repository='repo', archive='test')}

    logs = tuple(
        module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
    )

    assert logs == expected_logs 
Example #30
Source File: test_borgmatic.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def test_collect_configuration_run_summary_logs_mount_with_repository_error():
    flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
        ValueError
    )
    expected_logs = (flexmock(),)
    flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
    arguments = {'mount': flexmock(repository='repo')}

    logs = tuple(
        module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
    )

    assert logs == expected_logs