import atexit
import logging
import os
import site

import coverage
import mock
import testtools

from smiley import tracer


LOG = logging.getLogger(__name__)


class TracerTest(testtools.TestCase):

    def test_interesting_locals(self):
        def _func():
            pass
        t = tracer.Tracer(None)
        f = mock.Mock()
        f.f_locals = {
            'simple_name': 1,
            'module': tracer,
            'function': _func,
            'method': self.setUp,
            '__magic__': 2,
        }
        interesting = t._get_interesting_locals(f)
        self.assertEqual(interesting, {'simple_name': 1})

    def test_ignore_stdlib_maybe_c(self):
        # The atexit module is implemented in C in python 3.3, so it
        # has a different filename and path than under 2.7. However,
        # on some systems (Linux?) the module is not even a dll, it's
        # a builtin.
        if not hasattr(atexit, '__file__'):
            self.skipTest('atexit is a builtin')
        t = tracer.Tracer(None, include_stdlib=False)
        self.assertTrue(t._should_ignore_file(atexit.__file__))

    def test_ignore_stdlib(self):
        t = tracer.Tracer(None, include_stdlib=False)
        os_path = os.path.abspath(os.__file__)
        LOG.debug('checking %s', os_path)
        self.assertIn(
            os.path.dirname(os_path) + os.sep,
            t._stdlibdirs,
        )
        LOG.debug('checking %s', os.__file__)
        self.assertTrue(
            t._should_ignore_file(os.__file__),
            'should ignore %s' % os.__file__,
        )

    def test_capture_stdlib(self):
        t = tracer.Tracer(None, include_stdlib=True)
        self.assertFalse(t._should_ignore_file(site.__file__))

    def test_ignore_smiley_by_default(self):
        t = tracer.Tracer(None)
        self.assertTrue(t._should_ignore_file(tracer.__file__))

    def test_ignore_coverage_by_default(self):
        t = tracer.Tracer(None)
        self.assertTrue(t._should_ignore_file(coverage.__file__))

    def test_include_smiley(self):
        t = tracer.Tracer(None, include_packages=['smiley'])
        self.assertFalse(t._should_ignore_file(tracer.__file__))

    def test_ignore_site_packages(self):
        t = tracer.Tracer(None, include_site_packages=False)
        self.assertTrue(
            t._should_ignore_file(mock.__file__),
            'should ignore %s' % mock.__file__,
        )

    def test_include_site_packages(self):
        t = tracer.Tracer(None, include_site_packages=True)
        self.assertFalse(t._should_ignore_file(mock.__file__))
