Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25217 > unrolled thread

Re: adding a simulation mode

Started byandrea crotti <andrea.crotti.0@gmail.com>
First post2012-07-12 14:55 +0100
Last post2012-07-12 14:55 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: adding a simulation mode andrea crotti <andrea.crotti.0@gmail.com> - 2012-07-12 14:55 +0100

#25217 — Re: adding a simulation mode

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2012-07-12 14:55 +0100
SubjectRe: adding a simulation mode
Message-ID<mailman.2041.1342101337.4697.python-list@python.org>
One way instead that might actually work is this

def default_mock_action(func_name):
    def _default_mock_action(*args, **kwargs):
        print("running {} with args {} and {}".format(func_name, args, kwargs))

    return _default_mock_action


def mock_fs_actions(to_run):
    """Take a function to run, and run it in an environment which
    mocks all the possibly dangerous operations
    """
    side_effect = [
        'copytree',
        'copy',
    ]

    acts = dict((s, default_mock_action(s)) for s in side_effect)

    with patch('pytest.runner.commands.ShellCommand.run',
default_mock_action('run')):
        with patch.multiple('shutil', **acts):
            to_run()


So I can just pass the main function inside the mock like
        mock_fs_actions(main)

and it seems to do the job, but I have to list manually all the things
to mock and I'm not sure is the best idea anyway..

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web