Alex Willmer | 6 Dec 2010 21:00
Picon
Gravatar

[Trac-dev] Unit testing an extension point

Hello again,


In the last week I've been working on adding versioning to attachments in Trac, on behalf of Logica.

The code so far, based on Trac 0.12 and modelled after the WikiPage class is here:


I've tried to maintain unit test coverage as I've gone along. Today I tried to add tests for IAttachmentChangeListener, but my TestAttachmentChangeListener is not being registered with the existing AttachmentModule.change_listeners. Is there another step I need to add, or can you spot anything I've done wrong?

from datetime import datetime
import os.path
import shutil
from StringIO import StringIO
import tempfile
import unittest

from trac.attachment import IAttachmentChangeListener, \
                            Attachment, AttachmentModule
from trac.core import Component, implements
from trac.perm import IPermissionPolicy, PermissionCache
from trac.resource import Resource, resource_exists
from trac.test import EnvironmentStub
from trac.util.datefmt import utc, to_utimestamp

class TicketOnlyViewsTicket(Component):
    implements(IPermissionPolicy)

    def check_permission(self, action, username, resource, perm):
        if action.startswith('TICKET_'):
            return resource.realm == 'ticket'
        else:
            return None
...
class AttachmentTestCase(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub()
        self.env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv')
        os.mkdir(self.env.path)
        self.attachments_dir = os.path.join(self.env.path, 'attachments')
        self.archive_dir = os.path.join(self.env.path,
                                        AttachmentModule.ARCHIVE_DIR)
        self.env.config.set('trac', 'permission_policies',
                            'TicketOnlyViewsTicket, LegacyAttachmentPolicy')
        self.env.config.set('attachment', 'max_size', 512)

        self.perm = PermissionCache(self.env)
...
    def test_insert(self):
        attachment = Attachment(self.env, 'ticket', 42)
        attachment.insert('foo.txt', StringIO(''), 0, 1)
        attachment = Attachment(self.env, 'ticket', 42)
        attachment.insert('bar.jpg', StringIO(''), 0, 2)

        attachments = Attachment.select(self.env, 'ticket', 42)
        self.assertEqual('foo.txt', attachments.next().filename)
        self.assertEqual('bar.jpg', attachments.next().filename)
        self.assertRaises(StopIteration, attachments.next)

        listener = TestAttachmentChangeListener(self.env)
        module = AttachmentModule(self.env)
        self.assertEquals(1, len(module.change_listeners))
        self.assertEquals('foo.txt', listener.added[0].filename)
        self.assertEquals('bar.jpg', listener.added[1].filename)

The output of this is:
alex <at> martha:~/src/trac-gitsvn$ python trac/tests/attachment.py
............F.......F.......
======================================================================
FAIL: test_insert (__main__.AttachmentTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "trac/tests/attachment.py", line 174, in test_insert
    self.assertEquals(1, len(module.change_listeners))
AssertionError: 1 != 0

======================================================================
FAIL: Ensure that legacy action tests are done on parent.  As
----------------------------------------------------------------------
Traceback (most recent call last):
  File "trac/tests/attachment.py", line 425, in test_legacy_permission_on_parent
    self.assert_('ATTACHMENT_VIEW' in self.perm(attachment.resource))
AssertionError

----------------------------------------------------------------------
Ran 28 tests in 1.669s

FAILED (failures=2)

I'm not too concerned about the second (ATTACHMENT_VIEW) failure, as it doesn't occur during a full unit-test run. The first however I cannot correct - if I remove the check on change_listeners then it fails on the next line - added has no members. Any ideas?

Thanks, Alex
--
Alex Willmer <alex <at> moreati.org.uk>
http://moreati.org.uk/blog http://twitter.com/moreati

--
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.
osimons | 6 Dec 2010 23:23
Picon

[Trac-dev] Re: Unit testing an extension point

On Dec 6, 9:00 pm, Alex Willmer <a... <at> moreati.org.uk> wrote:
> Hello again,
>
> In the last week I've been working on adding versioning to attachments in
> Trac, on behalf of Logica.
>
> The code so far, based on Trac 0.12 and modelled after the WikiPage class is
> here:
>
> https://github.com/moreati/trac-gitsvn/tree/0.12-versionedattachments
>
> <https://github.com/moreati/trac-gitsvn/tree/0.12-versionedattachments>I've
> tried to maintain unit test coverage as I've gone along. Today I tried to
> add tests for IAttachmentChangeListener, but my TestAttachmentChangeListener
> is not being registered with the existing AttachmentModule.change_listeners.
> Is there another step I need to add, or can you spot anything I've done
> wrong?
>
> from datetime import datetime
> import os.path
> import shutil
> from StringIO import StringIO
> import tempfile
> import unittest
>
> from trac.attachment import IAttachmentChangeListener, \
>                             Attachment, AttachmentModule
> from trac.core import Component, implements
> from trac.perm import IPermissionPolicy, PermissionCache
> from trac.resource import Resource, resource_exists
> from trac.test import EnvironmentStub
> from trac.util.datefmt import utc, to_utimestamp
>
> class TicketOnlyViewsTicket(Component):
>     implements(IPermissionPolicy)
>
>     def check_permission(self, action, username, resource, perm):
>         if action.startswith('TICKET_'):
>             return resource.realm == 'ticket'
>         else:
>             return None
> ...
> class AttachmentTestCase(unittest.TestCase):
>
>     def setUp(self):
>         self.env = EnvironmentStub()
>         self.env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv')
>         os.mkdir(self.env.path)
>         self.attachments_dir = os.path.join(self.env.path, 'attachments')
>         self.archive_dir = os.path.join(self.env.path,
>                                         AttachmentModule.ARCHIVE_DIR)
>         self.env.config.set('trac', 'permission_policies',
>                             'TicketOnlyViewsTicket, LegacyAttachmentPolicy')
>         self.env.config.set('attachment', 'max_size', 512)
>
>         self.perm = PermissionCache(self.env)
> ...
>     def test_insert(self):
>         attachment = Attachment(self.env, 'ticket', 42)
>         attachment.insert('foo.txt', StringIO(''), 0, 1)
>         attachment = Attachment(self.env, 'ticket', 42)
>         attachment.insert('bar.jpg', StringIO(''), 0, 2)
>
>         attachments = Attachment.select(self.env, 'ticket', 42)
>         self.assertEqual('foo.txt', attachments.next().filename)
>         self.assertEqual('bar.jpg', attachments.next().filename)
>         self.assertRaises(StopIteration, attachments.next)
>
>         listener = TestAttachmentChangeListener(self.env)
>         module = AttachmentModule(self.env)
>         self.assertEquals(1, len(module.change_listeners))
>         self.assertEquals('foo.txt', listener.added[0].filename)
>         self.assertEquals('bar.jpg', listener.added[1].filename)
>
> The output of this is:
> alex <at> martha:~/src/trac-gitsvn$ python trac/tests/attachment.py
> ............F.......F.......
> ======================================================================
> FAIL: test_insert (__main__.AttachmentTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "trac/tests/attachment.py", line 174, in test_insert
>     self.assertEquals(1, len(module.change_listeners))
> AssertionError: 1 != 0
>
> ======================================================================
> FAIL: Ensure that legacy action tests are done on parent.  As
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "trac/tests/attachment.py", line 425, in
> test_legacy_permission_on_parent
>     self.assert_('ATTACHMENT_VIEW' in self.perm(attachment.resource))
> AssertionError
>
> ----------------------------------------------------------------------
> Ran 28 tests in 1.669s
>
> FAILED (failures=2)
>
> I'm not too concerned about the second (ATTACHMENT_VIEW) failure, as it
> doesn't occur during a full unit-test run. The first however I cannot
> correct - if I remove the check on change_listeners then it fails on the
> next line - added has no members. Any ideas?
>
> Thanks, Alex
> --
> Alex Willmer <a... <at> moreati.org.uk>http://moreati.org.uk/bloghttp://twitter.com/moreati

You need to instantiate the test plugin to load it - as you do for the
listener but not for the permission policy plugin. Plugins that are
part of trac.* namespace should get enabled automatically when loaded,
but anything in external plugins or test plugins saved to project
plugins directory or otherwise outside regular namespace needs to be
specifically enabled. Here is an example of how that is done:

https://www.coderesort.com/p/open/browser/trac-talkplugin/trunk/tractalk/tests/plugins.py

The magic lines being:

self.env = EnvironmentStub(
   enable=['trac.*', 'tractalk.core.*', 'tractalk.plugins.*'])

...and...

TalkActionPlugin(self.env)

Hope it helps.

:::simon

https://www.coderesort.com
http://trac-hacks.org/wiki/osimons

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

Alex Willmer | 7 Dec 2010 00:14
Picon
Gravatar

Re: [Trac-dev] Re: Unit testing an extension point

On Mon, Dec 6, 2010 at 10:23 PM, osimons <oddsimons <at> gmail.com> wrote:

You need to instantiate the test plugin to load it - as you do for the
listener but not for the permission policy plugin. Plugins that are
part of trac.* namespace should get enabled automatically when loaded,
but anything in external plugins or test plugins saved to project
plugins directory or otherwise outside regular namespace needs to be
specifically enabled. Here is an example of how that is done:

https://www.coderesort.com/p/open/browser/trac-talkplugin/trunk/tractalk/tests/plugins.py

The magic lines being:

self.env = EnvironmentStub(
  enable=['trac.*', 'tractalk.core.*', 'tractalk.plugins.*'])

Thank you, adding the enable clause fixed both test cases

         self.env = EnvironmentStub(enable=['trac.attachment.*',
                                           TicketOnlyViewsTicket,
                                           TestAttachmentChangeListener])
 
--
Alex Willmer <alex <at> moreati.org.uk>
http://moreati.org.uk/blog http://twitter.com/moreati

--
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.
Henrik | 9 Dec 2010 14:39
Picon

[Trac-dev] Wiki syntax provider question

Hi,

I have tried to write a new wiki syntax provider to create links to
our NCR system (IBM Telelogic Synergy). These NCR's looks like this,
cbxpt_se#1234. I have managed to create my own class (see below).
However, it works at the moment for this syntax, cbxpt_se:1234. So my
question, is it possible to adapt my wikisyntaxprovider to match #
instead of :? Or is this reserved for trac ticket links, #1234?

class cbxptWikiSyntax(Component):

    implements(IWikiSyntaxProvider)

    def get_link_resolvers(self):
        return [ ('cbxpt_se', self._format_link) ]

    def get_wiki_syntax(self):
        return []

    def _format_link(self, formatter, ns, target, label):
	        url = "http://stoweb01.scan.bombardier.com/~ebiconfig/
infopoint/index.php?
option=com_rcs_ncr_reports&action=detailedReport&type=cr&name=cbxpt_se
%C2%A7"
	        if target.isdigit():
	            return tag.a(
	                "cbxpt_se#%d" % int(target),
	                href = url + target,
	                title = "View cbx_pt #%d on infopoint" %
(int(target))
	            )
	        else:
	            return label

/Henrik

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

osimons | 9 Dec 2010 16:50
Picon

[Trac-dev] Re: Wiki syntax provider question

On Dec 9, 2:39 pm, Henrik <hjoens... <at> gmail.com> wrote:
> Hi,
>
> I have tried to write a new wiki syntax provider to create links to
> our NCR system (IBM Telelogic Synergy). These NCR's looks like this,
> cbxpt_se#1234. I have managed to create my own class (see below).
> However, it works at the moment for this syntax, cbxpt_se:1234. So my
> question, is it possible to adapt my wikisyntaxprovider to match #
> instead of :? Or is this reserved for trac ticket links, #1234?
>
> class cbxptWikiSyntax(Component):
>
>     implements(IWikiSyntaxProvider)
>
>     def get_link_resolvers(self):
>         return [ ('cbxpt_se', self._format_link) ]
>
>     def get_wiki_syntax(self):
>         return []
>
>     def _format_link(self, formatter, ns, target, label):
>                 url = "http://stoweb01.scan.bombardier.com/~ebiconfig/
> infopoint/index.php?
> option=com_rcs_ncr_reports&action=detailedReport&type=cr&name=cbxpt_se
> %C2%A7"
>                 if target.isdigit():
>                     return tag.a(
>                         "cbxpt_se#%d" % int(target),
>                         href = url + target,
>                         title = "View cbx_pt #%d on infopoint" %
> (int(target))
>                     )
>                 else:
>                     return label

Not reserved as such, but a module of higher preference are currently
responding. If you don't use the ticket system, you can just disable
it and there won't be anything in Trac that knows about '#' link
prefix. In trac.ini:

[components]
trac.ticket.* = disabled

Haven't tried running with such as setting myself, but see no reason
why it shouldn't work.

:::simon

https://www.coderesort.com
http://trac-hacks.org/wiki/osimons

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

Henrik | 9 Dec 2010 17:09
Picon

[Trac-dev] Re: Wiki syntax provider question

Thanks for the answer. Howerver, I use the ticket system so I can't
disable it. Is it possible to disable the internal trac link somehow?
I'm a python newbie, I don't understand how my custom syntax provider
gets hooked up with : and not #.

/Henrik

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

Noah Kantrowitz | 9 Dec 2010 19:09
Gravatar

Re: [Trac-dev] Re: Wiki syntax provider question

You defined a link resolver, not a new bit of syntax. Standard links [foo:bar baz] are abstracted to make
them a bit easier to add. Look at the wiki syntax providers for the tickets or wiki system to see how to write a
syntax regex.

--Noah

"Henrik" <hjoensson <at> gmail.com> wrote:

>Thanks for the answer. Howerver, I use the ticket system so I can't
>disable it. Is it possible to disable the internal trac link somehow?
>I'm a python newbie, I don't understand how my custom syntax provider
>gets hooked up with : and not #.
>
>/Henrik
>
>-- 
>You received this message because you are subscribed to the Google
>Groups "Trac Development" group.
>To post to this group, send email to trac-dev <at> googlegroups.com.
>To unsubscribe from this group, send email to
>trac-dev+unsubscribe <at> googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/trac-dev?hl=en.

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

Steffen Hoffmann | 12 Dec 2010 14:41
Picon

Re: [Trac-dev] Wiki syntax provider question


Henrik wrote:
> Hi,
> 
> I have tried to write a new wiki syntax provider to create links to
> our NCR system (IBM Telelogic Synergy). These NCR's looks like this,
> cbxpt_se#1234. I have managed to create my own class (see below).
> However, it works at the moment for this syntax, cbxpt_se:1234. So my
> question, is it possible to adapt my wikisyntaxprovider to match #
> instead of :? Or is this reserved for trac ticket links, #1234?
> 
> class cbxptWikiSyntax(Component):
> 
>     implements(IWikiSyntaxProvider)
> 

I'm a bit late, but here are my two cents on the topic anyway:

I guess that you'll need to take a deeper look into the
IWikiSyntaxProvider source. The matching of colon syntax like
cbxpt_se:1234 is certainly the generic resource identifier translation,
same as done with wiki:WikiStart, ticket:34, tags:ProjectX,
screenshot:21, ...

But I see your preferred markup is a shortcut syntax, again this should
still be possible, since you don't request matching '#' alone, but
'cbxpt_se#', right. You may have a chance to do it, but before the end
of the day you should ask yourself, if the '#' vs ':' really matters to
you, and more important, if your users wouldn't do much better with a
real shortcut instead of that monster-prefix ('cbxpt_se#'), like  <at> 1234 ?

Sincerely

Steffen Hoffmann
(hasienda)
Henrik | 13 Dec 2010 15:16
Picon

[Trac-dev] Re: Wiki syntax provider question

Thanks for all your answers. I found another solution when googling.
There is a plugin on trac hacks that worked perfectly for me:
http://trac-hacks.org/wiki/RegexLinkPlugin

/Henrik

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.

Mat Booth | 15 Dec 2010 00:59
Picon

[Trac-dev] Nautilus plug-in for attaching files to Trac tickets.

Hi all,

I am forever wanting to send email attachments from Evolution (the
Gnome mail client) to Trac as ticket attachments, but I am lazy and I
don't want to save the attachment to my desktop first before I am able
to upload them to Trac.

So I wrote this little plug-in for Nautilus (the Gnome file manager)
that allows you to right click on files or directories in Nautilus and
email attachments in Evolution and send them directly to Trac as
optionally compressed ticket attachments using the Trac XML-RPC API.

I realise it's probably rather a niche tool, but I thought I'd post it
here in case anyone other than me finds it useful. Here's the link:

http://www.matbooth.co.uk/trac/wiki/NautilusSendtoTracStart

I have only tested it on Gnome 2.28.0 but it may work on older
versions if you change the nautilus and soup version requirements in
configure.ac.

Regards,
Mat

-- 
Mat Booth

--

-- 
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To post to this group, send email to trac-dev <at> googlegroups.com.
To unsubscribe from this group, send email to trac-dev+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/trac-dev?hl=en.


Gmane