import py
from py.__.test import session
mypath = py.magic.autopath().dirpath()

from pydirs.src import pydirs

try:
    import cProfile as profile
except ImportError:
    print 'cProfile could not be imported - falling back on profile'
    import profile

try:
    import coverage
except ImportError:
    class CoverageSession(session.Session):
        def main(self, *args, **kwargs):
            print ('you have not installed \'coverage\', so code coverage is '
                   'not supported')
else:
    class CoverageSession(session.Session):
        def main(self, *args, **kwargs):
            print 'running the tests while building \'coverage\' report,'
            print 'this will take a while, without producing any output'
            print 'on the terminal... please wait'
            coverage.start()
            ret = super(CoverageSession, self).main(*args, **kwargs)
            coverage.stop()
            coverage.report([pydirs]) #, pysvndirs])
            return ret

class ProfileSession(session.Session):
    def main(self, *args, **kwargs):
        profile.runctx(
            "super(ProfileSession, self).main(*args, **kwargs)",
            globals(), locals())

class ApigenLinkWriter(object):
    def get_link(self, base, path):
        rel = path.relto(base)
        return '../apigen/source/%s.html' % (rel,)

linkwriter = ApigenLinkWriter()

class Directory(py.test.collect.Directory):
    def run(self):
        if self.fspath == mypath:
            return ['test', 'doc']
        return super(Directory, self).run()

