1
|
|
|
import os |
2
|
|
|
import tempfile |
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
from coalib import coala_delete_orig |
6
|
|
|
from coalib.misc.ContextManagers import retrieve_stdout |
7
|
|
|
from coalib.parsing import Globbing |
8
|
|
|
from coalib.settings.Section import Section |
9
|
|
|
from coalib.settings.Setting import Setting |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class coalaDeleteOrigTest(unittest.TestCase): |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
self.section = Section("default") |
16
|
|
|
self.section.append(Setting("config", '/path/to/file')) |
17
|
|
|
|
18
|
|
|
def test_nonexistent_coafile(self): |
19
|
|
|
old_getcwd = os.getcwd |
20
|
|
|
os.getcwd = lambda *args: None |
21
|
|
|
with retrieve_stdout() as stdout: |
22
|
|
|
retval = coala_delete_orig.main() |
23
|
|
|
self.assertIn("Can only delete .orig files if ", stdout.getvalue()) |
24
|
|
|
self.assertEqual(retval, 255) |
25
|
|
|
os.getcwd = old_getcwd |
26
|
|
|
|
27
|
|
|
def test_remove_exception(self): |
28
|
|
|
old_glob = Globbing.glob |
29
|
|
|
|
30
|
|
|
# Non existent file |
31
|
|
|
with retrieve_stdout() as stdout: |
32
|
|
|
Globbing.glob = lambda *args: ["non_existent_file"] |
33
|
|
|
retval = coala_delete_orig.main(section=self.section) |
34
|
|
|
output = stdout.getvalue() |
35
|
|
|
self.assertEqual(retval, 0) |
36
|
|
|
self.assertIn("Couldn't delete", output) |
37
|
|
|
|
38
|
|
|
# Directory instead of file |
39
|
|
|
with tempfile.TemporaryDirectory() as filename, \ |
40
|
|
|
retrieve_stdout() as stdout: |
41
|
|
|
Globbing.glob = lambda *args: [filename] |
42
|
|
|
retval = coala_delete_orig.main(section=self.section) |
43
|
|
|
output = stdout.getvalue() |
44
|
|
|
self.assertEqual(retval, 0) |
45
|
|
|
self.assertIn("Couldn't delete", output) |
46
|
|
|
|
47
|
|
|
Globbing.glob = old_glob |
48
|
|
|
|