[apparmor] [patch 0/8] misc parser test cleanups (was [patch 05/13] parser - rewrite caching tests in python unittest)

Steve Beattie steve at nxnw.org
Thu Oct 24 01:41:44 UTC 2013


On Wed, Oct 16, 2013 at 12:28:48AM +0200, Christian Boltz wrote:
> What about this?
> 
> def write_file(directory, filename, contents):
>     '''write contents to path'''
>     path = os.path.join(directory, path)
>     with open(path, 'w+') as f:
>         f.write(contents)
>     return path
> 
> This makes the tests a bit more readable, and if you need the filename 
> later, you can use
>     filename = write_file(...)

I'm pretty sure we're diminishing returns here, but I went ahead and did
this as patch 5 in the following patch set.

> > That said, I was able to make the decorator function approach work.
> 
> I'd prefer if you can do this on a global level instead of having it on 
> every test.
> 
> google says it's possible, see 
> http://stackoverflow.com/questions/6695854/writing-a-class-decorator-that-applies-a-decorator-to-all-methods
> and http://stackoverflow.com/questions/3467526/attaching-a-decorator-to-all-functions-within-a-class
> and some more, see 
> https://www.google.com/search?q=python+decorator+all+functions&ie=UTF-8

Thanks for the pointers. From my reading and especially given
the quirky nature of how unittest.TestCase does it's instance
initialization, it seemed that a metaclass was the right way
to accomplish this. The downside is that the way metaclasses are
specified and used differs between python 2 and python 3. The last
patch in the following patch series does this with python 3, but the
downside is that the test scripts that use testlib.py can only be run
under python 3 then. I *think* that's a fair tradeoff in this instance,
but am willing to hear arguments against.

Assorted fixes to the parser tests follow.

-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/



More information about the AppArmor mailing list