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


Groups > comp.lang.python > #25217

Re: adding a simulation mode

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'args': 0.04; '"""': 0.05; 'run,': 0.07; 'subject:adding': 0.07; '**kwargs):': 0.09; 'args,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'skip:p 40': 0.15; 'skip:{ 20': 0.17; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'possibly': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'environment': 0.29; "i'm": 0.29; 'function': 0.30; 'operations': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'sure': 0.38; 'instead': 0.39; 'header:Received:5': 0.40; 'dangerous': 0.66; 'acts': 0.71; 'mock': 0.84; 'subject:mode': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=QlD1IKZ6hI/lXaMeFoMZAPyI3zpKc/jxZTH3tJ/ZwXE=; b=sxr4crDCPh1v5WvxbFOstugZgtuWq3VUZtvPIg2ewESMj5WVjv+phCrluTcr74JZ6A SCHHTFkGXg7cm/JA5xBVVxPnzwuPxFNUABmONM9jNa0k+eMOTgiNu8IKtcUPt5v+t9VB BrpuuhkzNHZ9zjoxmxKVC50SVpQUTCuyudT2+kvXs2qPjS+fq/QRLpKey/g+6KcBJ+2Z WuVxUFow7yv1BdXzFyvo47z8F5SExxS3M13aTTFym1YqaS+OLc6TtJxOwEMx8gf7C526 gAgz1XaLWKwpHqwoTzbQG1yRpWR54elCfyyTdZy++x8R/NIcp6AXKUhG3exvnvtxfEas MJog==
MIME-Version 1.0
In-Reply-To <CAF_E5JYDXe_tni4NohVE5Mj8bEjeJ7xxZ6XLDhaM6sQi_oX0aA@mail.gmail.com>
References <CAF_E5JbFK4ZYsvwdVHxw-S7vLkpwskF=OCohWW2okmPi56ObOA@mail.gmail.com> <87liiyr48f.fsf@handshake.de> <CAF_E5JaUD-AyudMut00Bjjan0Zv767do=XM_zKS3DvBmC4reyQ@mail.gmail.com> <CAF_E5JYDXe_tni4NohVE5Mj8bEjeJ7xxZ6XLDhaM6sQi_oX0aA@mail.gmail.com>
Date Thu, 12 Jul 2012 14:55:35 +0100
Subject Re: adding a simulation mode
From andrea crotti <andrea.crotti.0@gmail.com>
To Dieter Maurer <dieter@handshake.de>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2041.1342101337.4697.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342101337 news.xs4all.nl 6887 [2001:888:2000:d::a6]:58382
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25217

Show key headers only | View raw


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..

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web