def test_file_package_excludes_excluded_directories(self): fp = FilePackage() fp.add_directory("dir1") fp.add_directories("dir2", "dir3", "dir4", "dir5") fp.exclude_directory("dir3") fp.exclude_directories("dir4", "dir5") eq_(set(("dir1", "dir2")), set(fp.get_directories()))
def test_file_iterator_gets_stats_for_dir_relative_to_base_dir(self): dir1full = self._create_dir(self.directory, 'dir1') self._create_file(dir1full, 'file1') self._create_file(dir1full, 'file2') fp = FilePackage() fp.add_directory("dir1") fp.set_basedir(self.directory) self.file_iterator.set_filepackages([fp]) s = set(self.file_iterator.files()) eq_(set(self._prepend_dir(['file1', 'file2'], dir1full)), s)
def test_proper_ccn_is_found_in_single_file(self): directory = mkdtemp("-caboose-java-ccn-single-test") self.ccn_file_creator.create_file_with_ccn(3, directory) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatJavaMeanCcn() stat.set_files(file_iterator.files()) eq_(3, stat.get_stat()) rmtree(directory)
def test_proper_number_of_lines_are_counted_in_single_file(self): directory = mkdtemp("-caboose-java-ncss-single-test") self._create_file_with_lines(directory, 2, 4, 2) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatJavaNcss() stat.set_files(file_iterator.files()) eq_(2, stat.get_stat()) rmtree(directory)
def test_stat_lines_counts_zero_if_directory_does_not_exist(self): directory = mkdtemp("-caboose-non-exist-dir-test") inner = path.join(directory, 'nonexistant') fp = FilePackage() fp.add_directory(inner) file_iterator = FileIterator([fp]) stat = StatJavaNcss() stat.set_files(file_iterator.files()) eq_(0, stat.get_stat()) rmtree(directory)
def test_file_package_includes_globbed_directories(self): basedir = mkdtemp('-caboose-file-package-tests2') dirs = ("1dir", "2dir", "different") self.produce_fake_directories(basedir, dirs) #create a file package which uses a globbed directory name fp = FilePackage() fp.set_basedir(basedir) fp.add_directory("*dir") expected = [os.path.join(basedir, name) for name in ("1dir", "2dir")] eq_(set(expected), set(fp.get_directories()))
def test_match_files_by_filematcher(self): self._create_file(self.directory, 'file.txt') self._create_file(self.directory, 'file.java') fm = MockFileMatcher() fm.add_match("file.java") fp = FilePackage() fp.add_directory(self.directory) fp.add_file_matcher(fm) self.file_iterator.set_filepackages([fp]) s = set(self.file_iterator.files()) eq_(set([os.path.join(self.directory, 'file.java')]), s)
def test_ccn_func_count_when_over_limit_multiple(self): directory = mkdtemp("-caboose-java-ccn-count-high-single-test") self.ccn_file_creator.create_file_with_funcs_with_ccns(directory, [5, 1, 3, 2]) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatJavaCcnFuncCount() stat.set_ccn_limit(3) stat.set_files(file_iterator.files()) eq_(2, stat.get_stat()) rmtree(directory)
def test_zero_ccn_func_count_when_limit_not_met(self): directory = mkdtemp("-caboose-java-ccn-count-low-single-test") self.ccn_file_creator.create_file_with_ccn(1, directory) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatJavaCcnFuncCount() stat.set_ccn_limit(2) stat.set_files(file_iterator.files()) eq_(0, stat.get_stat()) rmtree(directory)
def test_proper_number_of_lines_are_counted_in_multiple_files(self): directory = mkdtemp("-caboose-numlines-multiple-test") self._create_file_with_n_lines(directory, 2) self._create_file_with_n_lines(directory, 3) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatLines() stat.set_files(file_iterator.files()) eq_(5, stat.get_stat()) rmtree(directory)
def test_proper_number_of_lines_are_counted_in_inner_dir(self): directory = mkdtemp("-caboose-numlines-inner-dir-test") inner = mkdtemp("-inner-dir", dir=directory) self._create_file_with_lines(directory, 2, 3, 4) self._create_file_with_lines(inner, 5, 6, 7) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) stat = StatJavaNcss() stat.set_files(file_iterator.files()) eq_(7, stat.get_stat()) rmtree(directory)
def test_stat_lines_works_with_absurd_number_of_files(self): directory = mkdtemp("-caboose-absurd-file-count-test") self._create_file_with_lines(directory, 5, 6, 7) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) files = file_iterator.files() #count the same file 5000 times files = files * 5000 stat = StatJavaNcss() stat._set_ncss_command("bash -c 'for x in `seq $#`; do echo 5; done' filler") stat.set_files(files) eq_(25000, stat.get_stat()) rmtree(directory)
def test_use_preprocessor_when_requested(self): directory = mkdtemp("-caboose-numlines-single-test") self._create_file_with_n_lines(directory, 2) fp = FilePackage() fp.add_directory(directory) file_iterator = FileIterator([fp]) ppf = MockPreProcessorFactory() stat = StatLines() stat.set_preprocessor_factory(ppf) stat.set_config({'preprocessor': 'name_here'}) stat.set_files(file_iterator.files()) stat.get_stat() ok_(ppf.get_preprocessor_called) eq_(ppf.preprocessor_name_requested, 'name_here') ok_(ppf.preprocessor.set_config_called) ok_(ppf.preprocessor.get_output_called) rmtree(directory)
def test_file_package_add_basedir_subdirs(self): basedir = mkdtemp('-caboose-file-package-tests') expected_dirs = set(("dir1", "dir2", "dir3")) self.produce_fake_directories(basedir, expected_dirs) expected_dirs = [os.path.join(basedir, name) for name in expected_dirs] #add a file which should be ignored as it is not a directory filepath = os.path.join(basedir, "file1") with open(filepath, 'w') as f: f.write("file contents here") fp = FilePackage() fp.set_basedir(basedir) fp.add_directory("*") eq_(set(expected_dirs), set(fp.get_directories())) rmtree(basedir)
def test_match_files_in_directory_tree_by_filematcher(self): self._create_file(self.directory, 'file0.java') dir1 = self._create_dir(self.directory, 'dir1') self._create_file(dir1, 'file1.c') dir2 = self._create_dir(dir1, 'dir2') self._create_file(dir2, 'file2.java') dir3 = self._create_dir(self.directory, 'dir3') self._create_file(dir3, 'file3.py') fm = MockFileMatcher() fm.add_match("file0.java") fm.add_match("file2.java") fp = FilePackage() fp.add_directory(self.directory) fp.add_file_matcher(fm) self.file_iterator.set_filepackages([fp]) expected = set() expected.add(os.path.join(self.directory, 'file0.java')) expected.add(os.path.join(dir2, 'file2.java')) s = set(self.file_iterator.files()) eq_(expected, s)
class FileIteratorTests(TestCase): def setUp(self): self.directory = mkdtemp('-caboose-file-iterator-tests') self.file_package = FilePackage() self.file_package.add_directory(self.directory) self.file_iterator = FileIterator([self.file_package,]) def tearDown(self): rmtree(self.directory) def test_iterate_files_in_single_directory(self): self._create_file(self.directory, 'file1') self._create_file(self.directory, 'file2') s = set(self.file_iterator.files()) eq_(set(self._prepend_dir(['file1', 'file2'])), s) def test_iterate_files_in_directory_tree(self): self._create_file(self.directory, 'file0') dir1 = self._create_dir(self.directory, 'dir1') self._create_file(dir1, 'file1') dir2 = self._create_dir(dir1, 'dir2') self._create_file(dir2, 'file2') dir3 = self._create_dir(self.directory, 'dir3') self._create_file(dir3, 'file3') expected = set() expected.add(os.path.join(self.directory, 'file0')) expected.add(os.path.join(dir1, 'file1')) expected.add(os.path.join(dir2, 'file2')) expected.add(os.path.join(dir3, 'file3')) s = set(self.file_iterator.files()) eq_(expected, s) def test_match_files_by_filematcher(self): self._create_file(self.directory, 'file.txt') self._create_file(self.directory, 'file.java') fm = MockFileMatcher() fm.add_match("file.java") fp = FilePackage() fp.add_directory(self.directory) fp.add_file_matcher(fm) self.file_iterator.set_filepackages([fp]) s = set(self.file_iterator.files()) eq_(set([os.path.join(self.directory, 'file.java')]), s) def test_match_files_in_directory_tree_by_filematcher(self): self._create_file(self.directory, 'file0.java') dir1 = self._create_dir(self.directory, 'dir1') self._create_file(dir1, 'file1.c') dir2 = self._create_dir(dir1, 'dir2') self._create_file(dir2, 'file2.java') dir3 = self._create_dir(self.directory, 'dir3') self._create_file(dir3, 'file3.py') fm = MockFileMatcher() fm.add_match("file0.java") fm.add_match("file2.java") fp = FilePackage() fp.add_directory(self.directory) fp.add_file_matcher(fm) self.file_iterator.set_filepackages([fp]) expected = set() expected.add(os.path.join(self.directory, 'file0.java')) expected.add(os.path.join(dir2, 'file2.java')) s = set(self.file_iterator.files()) eq_(expected, s) def test_iterate_files_in_multiple_directories(self): dir1 = self._create_dir(self.directory, 'dir1') dir2 = self._create_dir(self.directory, 'dir2') self._create_file(dir1, 'file1') self._create_file(dir2, 'file2') fp = FilePackage() fp.add_directories(dir1, dir2) self.file_iterator.set_filepackages([fp]) expected = set() expected.add(os.path.join(dir1, 'file1')) expected.add(os.path.join(dir2, 'file2')) s = set(self.file_iterator.files()) eq_(expected, s) def test_file_iterator_gets_stats_for_dir_relative_to_base_dir(self): dir1full = self._create_dir(self.directory, 'dir1') self._create_file(dir1full, 'file1') self._create_file(dir1full, 'file2') fp = FilePackage() fp.add_directory("dir1") fp.set_basedir(self.directory) self.file_iterator.set_filepackages([fp]) s = set(self.file_iterator.files()) eq_(set(self._prepend_dir(['file1', 'file2'], dir1full)), s) def test_file_iterator_acts_like_iterator(self): files = ['file.txt', 'file.java'] need_to_find = [os.path.join(self.directory, f) for f in files] for f in need_to_find: self._create_file(self.directory, f) for f in self.file_iterator: need_to_find.remove(f) eq_(0, len(need_to_find)) def test_file_iterator_excludes_broken_symlinks(self): files = ['exists.txt', 'movealong.txt'] self._create_file(self.directory, 'exists.txt') os.symlink(os.path.join(self.directory, 'movealong.txt'), os.path.join(self.directory, 'broken.txt')) eq_(1, len(self.file_iterator.files())) eq_(os.path.join(self.directory, 'exists.txt'), self.file_iterator.files()[0]) def test_file_iterator_excludes_excluded_path_globs(self): srcdir = self._create_dir(self.directory, 'src') self._create_file(srcdir, 'srcfile.java') testdir = self._create_dir(self.directory, 'test') self._create_file(testdir, 'testfile.java') self.file_iterator.exclude_path_globs(*["*/test/*"]) eq_(1, len(self.file_iterator.files())) eq_(os.path.join(srcdir, 'srcfile.java'), self.file_iterator.files()[0]) @nottest def _create_file(self, directory, filename): filepath = os.path.join(directory, filename) with open(filepath, 'w') as f: f.write(filename) f.close() @nottest def _create_dir(self, directory, dirname): dirpath = os.path.join(directory, dirname) os.mkdir(dirpath) return dirpath @nottest def _prepend_dir(self, files, directory=None): if directory is None: directory = self.directory return [os.path.join(directory, f) for f in files]
def test_file_package_holds_many_directories(self): fp = FilePackage() fp.add_directory("dir1") fp.add_directories("dir2", "dir3") eq_(set(("dir1", "dir2", "dir3")), set(fp.get_directories()))