def setUp(self):
     if not os.path.exists(TEST_WORK_DIR):
         os.makedirs(TEST_WORK_DIR)
     helper = Helpers()
     helper.set_work_dir(TEST_WORK_DIR)
     helper._get_file(R_TEST_SRPM)
     helper._get_file(R_TEST_SPEC)
     helper._get_file(R_TEST_SRC)
     del helper
class MiscTests(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        unittest.TestCase.__init__(self, methodName)
        self.srpm_file = TEST_WORK_DIR + os.path.basename(TEST_SRPM)
        self.spec_file = TEST_WORK_DIR + os.path.basename(TEST_SPEC)
        self.source_file = TEST_WORK_DIR + os.path.basename(TEST_SRC)
        self.helper = Helpers()

    def setUp(self):
        if not os.path.exists(TEST_WORK_DIR):
            os.makedirs(TEST_WORK_DIR)
        self.helper.set_work_dir(TEST_WORK_DIR)
        self.helper._get_file(TEST_SRPM)
        #self.helper._get_file(TEST_SRC)
        self.helper._get_file(TEST_SPEC)


    def test_spec_file(self):
        ''' Test the SpecFile class'''
        spec = SpecFile(self.spec_file) 
        # Test misc rpm values (Macro resolved)
        self.assertEqual(spec.name,'python-test')
        self.assertEqual(spec.version,'1.0')
        # resolve the dist-tag
        dist = self.helper._run_cmd('rpm --eval %dist')[:-1]
        self.assertEqual(spec.release,'1'+dist)
        # test misc rpm values (without macro resolve)
        self.assertEqual(spec.find_tag('Release'), ['1%{?dist}'])
        self.assertEqual(spec.find_tag('License'), ['GPLv2+'])
        self.assertEqual(spec.find_tag('Group'), ['Development/Languages'])
        # Test rpm value not there
        self.assertEqual(spec.find_tag('PreReq'), [])
        # Test get sections
        expected = {'%clean': ['rm -rf $RPM_BUILD_ROOT']}
        self.assertEqual(spec.get_section('%clean'), expected)
        expected = {'%build': ['%{__python} setup.py build']}
        self.assertEqual(spec.get_section('%build'), expected)
        expected = {'%install': ['rm -rf $RPM_BUILD_ROOT', '%{__python} setup.py install -O1 --skip-build --root $RPM_BUILD_ROOT']}
        self.assertEqual(spec.get_section('%install'),expected)
        expected = {'%files': ['%defattr(-,root,root,-)', '%doc COPYING', '%{python_sitelib}/*']}
        self.assertEqual(spec.get_section('%files'),expected)
        # Test get_sources (return the Source/Patch lines with macros resolved)
        expected = {'Source0': 'http://timlau.fedorapeople.org/files/test/review-test/python-test-1.0.tar.gz'}
        self.assertEqual(spec.get_sources(), expected)
        # Test find
        regex = re.compile(r'^Release\s*:\s*(.*)')
        res = spec.find(regex)
        if res:
            self.assertEqual(res.groups(), ('1%{?dist}',))
        else:
            self.assertTrue(False)
            
        
        
    def test_source_file(self):
        """ Test the SourceFile class """
        source = Source()
        # set the work dir
        source.set_work_dir(TEST_WORK_DIR)
        # download the upstream source file
        source.get_source(TEST_SRC)
        # check that source exists and source.filename point to the right location
        self.assertEqual(source.filename, self.source_file)
        self.assertTrue(os.path.exists(self.source_file))
        self.assertEqual(source.check_source_md5(), "289cb714af3a85fe36a51fa3612b57ad")
        
    def test_srpm_file(self):
        """ Test the SRPMFile class """
        srpm = SRPMFile(self.srpm_file)
        # install the srpm
        srpm.install()
        self.assertTrue(srpm.is_installed)
        src_files = glob.glob(os.path.expanduser('~/rpmbuild/SOURCES/*'))
        expected = os.path.expanduser('~/rpmbuild/SOURCES/python-test-1.0.tar.gz')
        self.assertTrue(expected in src_files)
        # Generate the config file for the current OS run
        release = self.helper._run_cmd('rpm --eval %{fedora}')[:-1]
        arch = self.helper._run_cmd('arch')[:-1]
        srpm.mock_config = 'fedora-%s-%s' % (release, arch)
        # Do the mock build
        srpm.build(silence=True)
        self.assertTrue(srpm.is_build)
        # Retrieve the list of files in the mock folder and below
        rpm_files = []
        for root, dirs, files in os.walk(srpm.get_mock_dir()):
            rpm_files.extend(files)
        dist = self.helper._run_cmd('rpm --eval %dist')[:-1]
        expected = os.path.expanduser('python-test-1.0-1%(dist)s.noarch.rpm') % {'dist': dist}
        self.assertTrue(expected in rpm_files)