"""Tests for the 'setuptools' package"""
from unittest import TestSuite, TestCase, makeSuite, defaultTestLoader
import distutils.core, distutils.cmd
from distutils.errors import DistutilsOptionError, DistutilsPlatformError
from distutils.errors import DistutilsSetupError
import setuptools, setuptools.dist
from setuptools import Feature
from distutils.core import Extension
extract_constant, get_module_constant = None, None
from setuptools.depends import *
from distutils.version import StrictVersion, LooseVersion
from distutils.util import convert_path
import sys, os.path

def additional_tests():
    import doctest, unittest
    suite = unittest.TestSuite((
        doctest.DocFileSuite(
            'api_tests.txt',
            optionflags=doctest.ELLIPSIS, package='pkg_resources',
            ),
        ))
    if sys.platform == 'win32':
        suite.addTest(doctest.DocFileSuite('win_script_wrapper.txt'))
    return suite

def makeSetup(**args):
    """Return distribution from 'setup(**args)', without executing commands"""

    distutils.core._setup_stop_after = "commandline"

    # Don't let system command line leak into tests!
    args.setdefault('script_args',['install'])

    try:
        return setuptools.setup(**args)
    finally:
        distutils.core_setup_stop_after = None




class DependsTests(TestCase):

    def testExtractConst(self):
        if not extract_constant: return  # skip on non-bytecode platforms

        def f1():
            global x,y,z
            x = "test"
            y = z

        # unrecognized name
        self.assertEqual(extract_constant(f1.func_code,'q', -1), None)

        # constant assigned
        self.assertEqual(extract_constant(f1.func_code,'x', -1), "test")

        # expression assigned
        self.assertEqual(extract_constant(f1.func_code,'y', -1), -1)

        # recognized name, not assigned
        self.assertEqual(extract_constant(f1.func_code,'z', -1), None)


    def testFindModule(self):
        self.assertRaises(ImportError, find_module, 'no-such.-thing')
        self.assertRaises(ImportError, find_module, 'setuptools.non-existent')
        f,p,i = find_module('setuptools.tests'); f.close()

    def testModuleExtract(self):
        if not get_module_constant: return  # skip on non-bytecode platforms
        from distutils import __version__
        self.assertEqual(
            get_module_constant('distutils','__version__'), __version__
        )
        self.assertEqual(
            get_module_constant('sys','version'), sys.version
        )
        self.assertEqual(
            get_module_constant('setuptools.tests','__doc__'),__doc__
        )

    def testRequire(self):
        if not extract_constant: return  # skip on non-bytecode platforms

        req = Require('Distutils','1.0.3','distutils')

        self.assertEqual(req.name, 'Distutils')
        self.assertEqual(req.module, 'distutils')
        self.assertEqual(req.requested_version, '1.0.3')
        self.assertEqual(req.attribute, '__version__')
        self.assertEqual(req.full_name(), 'Distutils-1.0.3')

        from distutils import __version__
        self.assertEqual(req.get_version(), __version__)
        self.failUnless(req.version_ok('1.0.9'))
        self.failIf(req.version_ok('0.9.1'))
        self.failIf(req.version_ok('unknown'))

        self.failUnless(req.is_present())
        self.failUnless(req.is_current())

        req = Require('Distutils 3000','03000','distutils',format=LooseVersion)
        self.failUnless(req.is_present())
        self.failIf(req.is_current())
        self.failIf(req.version_ok('unknown'))

        req = Require('Do-what-I-mean','1.0','d-w-i-m')
        self.failIf(req.is_present())
        self.failIf(req.is_current())

        req = Require('Tests', None, 'tests', homepage="http://example.com")
        self.assertEqual(req.format, None)
        self.assertEqual(req.attribute, None)
        self.assertEqual(req.requested_version, None)
        self.assertEqual(req.full_name(), 'Tests')
        self.assertEqual(req.homepage, 'http://example.com')

        paths = [os.path.dirname(p) for p in __path__]