1 | import tempfile |
||
2 | import unittest |
||
3 | import os |
||
4 | import re |
||
5 | |||
6 | from coalib import coala_delete_orig |
||
7 | from coalib.misc.ContextManagers import retrieve_stdout |
||
8 | from coalib.settings.Section import Section |
||
9 | from coalib.settings.Setting import Setting |
||
10 | from coalib.misc.ContextManagers import make_temp |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
11 | |||
12 | |||
13 | class coalaDeleteOrigTest(unittest.TestCase): |
||
14 | |||
15 | def setUp(self): |
||
16 | self.section = Section("default") |
||
17 | self.section.append(Setting("config", '/path/to/file')) |
||
18 | |||
19 | @unittest.mock.patch('os.getcwd') |
||
20 | def test_nonexistent_coafile(self, mocked_getcwd): |
||
21 | mocked_getcwd.return_value = None |
||
22 | retval = coala_delete_orig.main() |
||
23 | self.assertEqual(retval, 255) |
||
24 | |||
25 | @unittest.mock.patch('coalib.parsing.Globbing.glob') |
||
26 | def test_remove_exception(self, mock_glob): |
||
27 | # Non existent file |
||
28 | mock_glob.return_value = ["non_existent_file"] |
||
29 | with retrieve_stdout() as stdout: |
||
30 | retval = coala_delete_orig.main(section=self.section) |
||
31 | output = stdout.getvalue() |
||
32 | self.assertEqual(retval, 0) |
||
33 | self.assertIn("Couldn't delete", output) |
||
34 | |||
35 | # Directory instead of file |
||
36 | with tempfile.TemporaryDirectory() as filename, \ |
||
37 | retrieve_stdout() as stdout: |
||
38 | mock_glob.return_value = [filename] |
||
39 | retval = coala_delete_orig.main(section=self.section) |
||
40 | output = stdout.getvalue() |
||
41 | self.assertEqual(retval, 0) |
||
42 | self.assertIn("Couldn't delete", output) |
||
43 | |||
44 | def test_normal_running(self): |
||
45 | with tempfile.TemporaryDirectory() as directory: |
||
46 | temporary = tempfile.mkstemp(suffix=".orig", dir=directory) |
||
47 | os.close(temporary[0]) |
||
48 | section = Section("") |
||
49 | section.append(Setting("project_dir", re.escape(directory))) |
||
50 | retval = coala_delete_orig.main(section=section) |
||
51 | self.assertEqual(retval, 0) |
||
52 | self.assertFalse(os.path.isfile(temporary[1])) |
||
53 |