#!/usr/bin/env python # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # import os import re import sys import optparse import unittest import xml.etree.cElementTree as ElementTree # so that we can import mouse sys.path.append(os.path.dirname(sys.path[0])) import mouse import sources import guesser data_path = 'data' resources_path = os.path.join(os.path.dirname(sys.path[0]), 'resources') def attributes_are_same(elem1, elem2): '''Return True if both elements share the same attribute dicts and values.''' attrib1 = elem1.attrib attrib2 = elem2.attrib if len(attrib1) != len(attrib2): return False for key in attrib1.keys(): if not attrib2.has_key(key): return False if attrib1[key] != attrib2[key]: return False return True def elements_are_same(elem1, elem2): '''Return True if these elements are the same, disreguarding unimportant ordering concerns. (What those are is left as an exercise for the reader.)''' # Check the attributes are the same if not attributes_are_same(elem1, elem2): return False # Check the content of these elements if elem1.text != elem2.text: return False # Finally recursively check the children children1 = elem1.getchildren() children2 = elem2.getchildren() if len(children1) != len(children2): return False for child1, child2 in zip(children1, children2): if not elements_are_same(child1,child2): return False return True def _count_items(items): 'Return the number of items generated by ITEMS' i = 0 for item in items(): i = i + 1 return i class TestReport(unittest.TestCase): 'Test generating reports' plain_xsl_path = os.path.join(resources_path, 'plain-rat.xsl') expected_output_path = os.path.join(data_path, 'expected_output') def _check_plain_output(self, input_path, expected_output_filename): items = sources.get_items(os.path.join(data_path, input_path)) report = mouse.generate_report(items) output = mouse.process_report(report) output = mouse.transform_xslt(output, self.plain_xsl_path) actual = output.split() expected = open(os.path.join(self.expected_output_path, expected_output_filename)).read().split() self.assertEqual(actual, expected) def _check_xml_output(self, input_path, expected_output_filename): items = sources.get_items(os.path.join(data_path, input_path)) report = mouse.generate_report(items) output = mouse.process_report(report) output_element = ElementTree.XML(output) target_xml = open(os.path.join(self.expected_output_path, expected_output_filename)).read() target_element = ElementTree.XML(target_xml) self.assertTrue(elements_are_same(output_element, target_element)) def test_greek_vanilla_xml(self): self._check_xml_output('greek', 'greek_vanilla.xml') def test_greek_vanilla(self): self._check_plain_output('greek', 'greek_vanilla') def test_binary_xml(self): self._check_xml_output(os.path.join('rat-tests', 'binaries'), 'binaries.xml') def test_binary(self): self._check_plain_output(os.path.join('rat-tests', 'binaries'), 'binaries') def test_archive_xml(self): self._check_xml_output(os.path.join('rat-tests', 'artifacts'), 'archive.xml') def test_archive(self): self._check_plain_output(os.path.join('rat-tests', 'artifacts'), 'archive') # def test_rat_xml(self): # self._check_xml_output('rat-tests', 'rat-tests.xml') # # def test_rat(self): # self._check_plain_output('rat-tests', 'rat-tests') class TestBinaryGuessing(unittest.TestCase): '''Test the various ways we tell if something is a binary file. This set of tests is largely stolen from RAT's testsuite.''' _names = [ 'image.png', 'image.pdf', 'image.gif', 'image.giff', 'image.tif', 'image.tiff', 'image.jpg', 'image.jpeg', 'image.exe', 'Whatever.class', 'data.dat', 'libicudata.so.34.' ] def test_is_binary(self): for name in self._names: # Don't bother giving the item contents, since we shouldn't ever # have to check the content, anyway self.assertTrue(guesser.is_binary(sources.Item(name, None))) def test_is_binary_content(self): self.assertTrue(guesser.is_binary(sources.Item('txt', open(os.path.join(data_path, 'rat-tests', 'binaries', 'Image-png.not'))))) self.assertFalse(guesser.is_binary(sources.Item('txt', open(os.path.join(data_path, 'rat-tests', 'elements', 'Source.java'))))) self.assertFalse(guesser.is_binary(sources.Item('txt', open(os.path.join(data_path, 'rat-tests', 'elements', 'NOTICE'))))) class TestNoticeGuessing(unittest.TestCase): '''Test to see if we recognize various notice files.''' _names = [ 'LICENSE', 'LICENSE.TXT', 'NOTICE', 'NOTICE.TXT', 'README', 'README.TXT' ] def test_is_notice(self): for name in self._names: # Don't bother giving the item contents, since we shouldn't ever # have to check the content, anyway self.assertTrue(guesser.is_notice(sources.Item(name, None))) class TestArchiveGuessing(unittest.TestCase): _names = [ '42.jar', '42.tar.gz', '42.zip', '42.tar', '42.bz', '42.bz2' ] def test_is_archive(self): for name in self._names: # Don't bother giving the item contents, since we shouldn't ever # have to check the content, anyway self.assertTrue(guesser.is_archive(sources.Item(name, None))) class TestFilters(unittest.TestCase): 'Test filtering various files and patterns' def test_single_exclude(self): items = sources.get_items(os.path.join(data_path, 'greek')) self.assertEqual(12, _count_items(items)) items = mouse.filter_excludes(items, (re.compile('.*greek\/iota$'), )) self.assertEqual(11, _count_items(items)) class TestSources(unittest.TestCase): 'Test the parsing of various data sources.' zip_path = os.path.join(data_path, 'greek.zip') tar_gz_path = os.path.join(data_path, 'greek.tar.gz') tar_bz2_path = os.path.join(data_path, 'greek.tar.bz2') greek_path = os.path.join(data_path, 'greek') iota_path = os.path.join(greek_path, 'iota') latin_path = os.path.join(data_path, 'latin') def _assert_items(self, path): items = sources.get_items(path) self.assertEqual(12, _count_items(items)) def test_dir(self): self._assert_items(self.greek_path) def test_tar_gz(self): self._assert_items(self.tar_gz_path) def test_tar_bz2(self): self._assert_items(self.tar_bz2_path) def test_zip(self): self._assert_items(self.zip_path) def test_archive_error(self): self.assertRaises(IOError, sources.get_items, self.latin_path) def test_bogus_filetype(self): self.assertRaises(sources._UnknownArchiveError, sources.get_items, self.iota_path) if __name__ == '__main__': # change the cwd to the directory cwd = os.getcwd() os.chdir(os.path.dirname(sys.argv[0])) unittest.main() os.chdir(cwd)