[py-dev] Advanced monkeypatching?

Floris Bruynooghe flub at devork.be
Tue Dec 21 11:45:46 CET 2010


On 21 December 2010 09:54, Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de> wrote:
> On Mon, 2010-12-20 at 13:47 +0000, Floris Bruynooghe wrote:
>> On 20 December 2010 09:56, Virgil Dupras <hsoft at hardcoded.net> wrote:
>> >
>> > On 2010-12-20, at 10:41 AM, Ronny Pfannschmidt wrote:
>> >> On Mon, 2010-12-20 at 09:22 +0100, Virgil Dupras wrote:
>> >>> On 2010-12-19, at 3:38 PM, Floris Bruynooghe wrote:
>> >>>>
>> >>>> Looking at the patch_osstat() it seems to me that it just fakes
>> >>>> os.stat() for one specific path but uses the original in place
>> >>>> otherwise.  This sounds like it could be just a specialised object
>> >>>> used to patch with.  I can imagine the monkeypatch plugin to provide a
>> >>>> funcarg which provides this functionality, e.g.:
>> >>>>
>> >>>> def test_foo(monkeypatch, monkey_osstat):
>> >>>>   monkeypatch.setattr(monkey_ossstat('/tmp/some_file'))
>> >>>>   ...
>> >>>>
>> >>>
>> >>> This would kind of work, but it would become complicated to patch os.stat() for two or more files, wouldn't it?
>> >> there could be a monkey_stat object that controlls the set of
>> >> files/subtrees under control
>> >>
>> >
>> > Yes, of course, but keeping the proposed API would make it unintuitive for multiple files. Example:
>> >
>> > monkeypatch.setattr(os, 'stat', monkey_osstat('some_file'))
>> > monkeypatch.setattr(os, 'stat', monkey_osstat('some_other_file'))
>> >
>> > We're not sure what happens there, as os.stat is patched twice. Are we overwriting our old monkeypatch, or is there some magic caching inside monkey_osstat() making it work for both files? That's the kind of question the developer will wonder about.
>>
>> I was thinking of something slightly different when I said this:
>>
>> def test_foo(monkeypatch, monkey_osstat):
>>     stat = monkey_ossstat('file1', 'file2')
>>     stat.add_file('file3')
>>     monkeypatch.setattr(os, 'stat', stat)
>>     stat.add_file('file4', st_size=123)
>
> i was thinking more along the lines of::
>
>    def test_foo(osstat_patch):
>        stat.add_file(...) #setup single files
>        stat.add_callback(root, somefunction) # hand off a whole tree
>
> i suppose better function names are possible
> also im wondering if this should integrate with py.path more deeply
> (having a virtual view on the whole fs seems neat)

That is a nice idea, I have at least one test where I create a simple
directory structure using (the old) py.test.ensuretemp() to test if
some sorting on different os.stat() fields works in a function.  This
is an annoying case since I need to do time.sleep() in between
creating the files as some platforms don't have a high enough
resolution on file timestamps.  So being able to "set" stat attributes
on py.path items would certainly be an interesting area to explore.


Regards
Floris

-- 
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org



More information about the Pytest-dev mailing list