Completed
Pull Request — master (#1569)
by Abdeali
02:51 queued 01:03
created

coalib.tests.raise_oserror()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
import os
2
import unittest
3
4
from coalib import coala_delete_orig
5
from coalib.misc.ContextManagers import retrieve_stdout
6
from coalib.parsing import Globbing
7
from coalib.settings.Section import Section
8
from coalib.settings.Setting import Setting
9
10
11
def raise_oserror(*args, **kwargs):
12
    raise OSError
13
14
15
class coalaDeleteOrigTest(unittest.TestCase):
16
17
    def setUp(self):
18
        self.section = Section("default")
19
        self.section.append(Setting("config", '/path/to/file'))
20
21
    def test_nonexistent_coafile(self):
22
        old_getcwd = os.getcwd
23
        os.getcwd = lambda *args: None
24
        with retrieve_stdout() as stdout:
25
            retval = coala_delete_orig.main()
26
            self.assertIn("Can only delete .orig files if ", stdout.getvalue())
27
            self.assertEqual(retval, 255)
28
        os.getcwd = old_getcwd
29
30
    def test_remove_exception(self):
31
        old_remove = os.remove
32
        old_glob = Globbing.glob
33
        Globbing.glob = lambda *args: ["file1", "file2"]
34
        os.remove = raise_oserror
35
        with retrieve_stdout() as stdout:
36
            retval = coala_delete_orig.main(section=self.section)
37
            output = stdout.getvalue()
38
            self.assertEqual(retval, 0)
39
            self.assertIn("Couldn't delete... file1", output)
40
            self.assertIn("Couldn't delete... file2", output)
41
        os.remove = old_remove
42
        Globbing.glob = old_glob
43