"""Tests for distutils.command.build_py."""

import os
import unittest

from distutils.core import Distribution
from distutils.tests import support


class OptionsParsingTestCase(support.LoggingSilencer,
                      unittest.TestCase):

    def test_basic_options(self):
        ae = self.assertEquals
        d = Distribution()
        ae(d.verbose, 1)
        # Why doesn't Distribution have a default commands = [] ??
        ae(hasattr(d, 'commands'), False)
        d.script_name = 'test'
        d.script_args = ['--quiet',
                         'build', '--force', '--build-temp=/tem/por/rary',
                         'install','--prefix=/foo/bar/baz']
        d.parse_command_line()
        ae(d.verbose, 0)
        ae(d.commands, ['build','install',])
        io = d.get_option_dict('install')
        ae(io.keys(), ['prefix'])
        ae(io['prefix'], ('command line', '/foo/bar/baz'))
        bo = d.get_option_dict('build')
        ae(sorted(bo.keys()), ['build_temp', 'force',])
        ae(bo['force'], ('command line', 1))
        ae(bo['build_temp'], ('command line', '/tem/por/rary'))

    def test_aliases_and_negatives(self):
        ae = self.assertEquals
        d = Distribution()
        ae(d.verbose, 1)
        d.script_name = 'test'
        d.script_args = ['--quiet', '--licence', 'install']
        d.parse_command_line()
        ae(d.verbose, 0)

        d = Distribution()
        ae(d.verbose, 1)
        d.script_name = 'test'
        d.script_args = ['--verbose', '--license', 'install']
        d.parse_command_line()
        ae(d.verbose, 2)

        d = Distribution()
        ae(d.verbose, 1)
        d.script_name = 'test'
        d.script_args = ['--verbose', '--verbose', '--verbose', 'install']
        d.parse_command_line()
        ae(d.verbose, 4)

    def test_command_packages(self):
        ae = self.assertEquals
        d = Distribution()
        d.script_name = 'test'
        d.script_args = ['--command-packages=bar,baz', 'install']
        d.parse_command_line()
        ae(d.get_command_packages(), ['distutils.command', 'bar', 'baz'])


def test_suite():
    return unittest.makeSuite(OptionsParsingTestCase)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")
