class TestExcludes(unittest.TestCase): def setUp(self): self.exclude = Excludes(directory=tempfile.gettempdir()) self.file_path = self.exclude._exclude_file_path def tearDown(self): if hasattr(self, 'file_path') and os.path.exists(self.file_path): os.remove(self.file_path) def test_update_excludes(self): self.exclude.update_excludes(['foo']) self._verify_exclude_file(['foo\n']) def test_updating_excludes_does_not_repeat_path(self): self.exclude.update_excludes(['foo']) self.exclude.update_excludes(['foo']) self._verify_exclude_file(['foo\n']) def test_updating_excludes_does_not_repeat_almost_similar_paths(self): self.exclude.update_excludes(['/foo/bar']) self.exclude.update_excludes(['/foo/bar/']) self._verify_exclude_file(['/foo/bar\n']) def test_contains(self): self.exclude.update_excludes(['/foo/bar/baz']) self.assertTrue(self.exclude.contains('/foo/bar/baz')) self._verify_exclude_file(['/foo/bar/baz\n']) def test_contains_when_file_is_in_excluded_directory(self): self.exclude.update_excludes(['/foo']) self.assertTrue(self.exclude.contains('/foo/bar/baz')) self.assertTrue(self.exclude.contains('/foo/bar/')) self.assertTrue(self.exclude.contains('/foo/')) self._verify_exclude_file(['/foo\n']) def test_contains_when_there_is_no_path(self): self.assertFalse(self.exclude.contains(None)) def test_remove_path(self): excludes = ['/foo', '/bar/baz', '/qux', '/quux/corge'] removed = ['/bar/baz', '/quux/corge'] self.exclude.update_excludes(excludes) self._verify_exclude_file([ex + '\n' for ex in excludes]) self.exclude.remove_path(removed[0]) self.assertEqual(len(self.exclude.get_excludes()), len(excludes)-1) self.exclude.remove_path(removed[1]) self.assertEqual(len(self.exclude.get_excludes()), len(excludes)-2) def test_when_exclude_file_points_to_directory(self): dir = tempfile.mkdtemp() os.mkdir(os.path.join(dir, 'excludes')) self.exclude = Excludes(dir) del self.file_path # not created nor used in this test self.assertRaises(NameError, self.exclude._get_exclude_file, 'w') def _verify_exclude_file(self, expected): file_contents = open(self.file_path, 'r').readlines() self.assertEqual(file_contents, expected)