import unittest
import sys
import re
import os
import saliweb.build

class DummyEnv(object):
    def __init__(self, exit_val):
        self.exit_val = exit_val
        self.env = {'ENV': {}, 'service_module': 'testser'}
    def __getitem__(self, key):
        return self.env[key]
    def get(self, key, default):
        return self.env.get(key, default)
    def Clone(self):
        return self
    def Execute(self, exe):
        self.exec_str = exe
        return self.exit_val


class BuilderTest(unittest.TestCase):
    """Check builder functions"""

    def test_builder_python_tests(self):
        """Test builder_python_tests function"""
        e = DummyEnv(0)
        t = saliweb.build.builder_python_tests('dummytgt',
                                               ['foo.py', 'bar.py'], e)
        self.assertEqual(t, None)

        e = DummyEnv(1)
        t = saliweb.build.builder_python_tests('dummytgt',
                                               ['foo.py', 'bar.py'], e)
        self.assertEqual(e.env['ENV'], {'PYTHONPATH': 'python'})
        regex = 'python .*/run\-tests\.py foo\.py bar\.py$'
        m = re.match(regex, e.exec_str)
        self.assertNotEqual(m, None, 'String %s does not match regex %s' \
                            % (e.exec_str, regex))
        self.assertEqual(t, 1)

        e = DummyEnv(0)
        e.env['html_coverage'] = 'testcov'
        t = saliweb.build.builder_python_tests('dummytgt',
                                               ['foo.py', 'bar.py'], e)
        regex = 'python .*/run\-tests\.py --html_coverage=testcov foo\.py ' \
                + 'bar\.py$'
        m = re.match(regex, e.exec_str)
        self.assertNotEqual(m, None, 'String %s does not match regex %s' \
                            % (e.exec_str, regex))

    def test_builder_perl_tests(self):
        """Test builder_perl_tests function"""
        os.mkdir('lib')
        open('lib/testser.pm', 'w').write('line1\n@CONFIG@\nline2\n')
        open('lib/other.pm', 'w')
        e = DummyEnv(0)
        t = saliweb.build.builder_perl_tests('dummytgt',
                                             ['foo.pl', 'bar.pl'], e)
        self.assertEqual(t, None)

        e = DummyEnv(1)
        t = saliweb.build.builder_perl_tests('dummytgt',
                                             ['foo.pl', 'bar.pl'], e)
        self.assert_('PERL5LIB' in e.env['ENV'])
        self.assertEqual(e.exec_str, "prove foo.pl bar.pl")
        self.assertEqual(t, 1)
        os.unlink('lib/other.pm')
        os.unlink('lib/testser.pm')
        os.rmdir('lib')

if __name__ == '__main__':
    unittest.main()

@@ -60,16 +60,46 @@
 eb.build
+%0Aimport tempfile%0Aimport shutil
 %0A%0Aclass 
@@ -1810,16 +1810,736 @@
 egex))%0A%0A
+    def test_fixup_perl(self):%0A        %22%22%22Test _fixup_perl_html_coverage function%22%22%22%0A        tmpdir = tempfile.mkdtemp()%0A        prefix = '/foo/bar/'%0A        mangled = '-foo-bar-'%0A        f = open(os.path.join(tmpdir, 'coverage.html'), 'w')%0A        f.write('%25sbaz/foo.html%5Cn%25sbaz-foo.html%5Cn' %25 (prefix, mangled))%0A        f.close()%0A        open(os.path.join(tmpdir, '%25sbaz-foo.html' %25 mangled), 'w')%0A        saliweb.build._fixup_perl_html_coverage(prefix, tmpdir)%0A%0A        f = open(os.path.join(tmpdir, 'index.html'))%0A        self.assertEqual(f.read(), 'baz/foo.html%5Cnbaz-foo.html%5Cn')%0A        os.unlink(os.path.join(tmpdir, 'index.html'))%0A        os.unlink(os.path.join(tmpdir, 'baz-foo.html'))%0A        os.rmdir(tmpdir)%0A%0A
     def 
@@ -2608,32 +2608,81 @@
 sts function%22%22%22%0A
+        shutil.rmtree('lib', ignore_errors=True)%0A
         os.mkdir
@@ -3239,16 +3239,16 @@
 ar.pl%22)%0A
-
         
@@ -3270,16 +3270,587 @@
 l(t, 1)%0A
+%0A        e = DummyEnv(0)%0A        e.env%5B'html_coverage'%5D = 'testcov'%0A        old = saliweb.build._fixup_perl_html_coverage%0A        try:%0A            def dummy_func(prefix, outdir): pass%0A            saliweb.build._fixup_perl_html_coverage = dummy_func%0A            t = saliweb.build.builder_perl_tests('dummytgt',%0A                                                 %5B'foo.pl', 'bar.pl'%5D, e)%0A        finally:%0A            saliweb.build._fixup_perl_html_coverage = old%0A        self.assert_('PERL5LIB' in e.env%5B'ENV'%5D)%0A        self.assert_('HARNESS_PERL_SWITCHES' in e.env%5B'ENV'%5D)%0A%0A
         

Test new Perl coverage builder.