Failed Conditions
Pull Request — master (#1182)
by Lasse
02:02 queued 26s
created

e_edit()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 10
rs 9.4286
1
import subprocess
2
import sys
3
import os
4
import unittest
5
import tempfile
6
7
sys.path.insert(0, ".")
8
from coalib.results.Diff import Diff
9
from coalib.results.Result import Result
10
from coalib.results.result_actions.OpenEditorAction import OpenEditorAction
11
from coalib.settings.Section import Section, Setting
12
13
14
class OpenEditorActionTest(unittest.TestCase):
15
    @staticmethod
16
    def fake_edit(commands):
17
        filename = commands[1]
18
        with open(filename) as f:
19
            lines = f.readlines()
20
21
        del lines[1]
22
23
        with open(filename, "w") as f:
24
            f.writelines(lines)
25
26
    @staticmethod
27
    def fake_edit_subl(commands, stdout):
28
        """
29
        Solely the declaration raises an exception if stdout not provided.
30
        """
31
        assert ("--wait" in commands), "Did not wait for the editor to close"
32
33
    def setUp(self):
34
        fahandle, self.fa = tempfile.mkstemp()
35
        os.close(fahandle)
36
        fbhandle, self.fb = tempfile.mkstemp()
37
        os.close(fbhandle)
38
39
    def tearDown(self):
40
        os.remove(self.fa)
41
        os.remove(self.fb)
42
43
    def test_apply(self):
44
        # Initial file contents, *before* a patch was applied
45
        file_dict = {
46
            self.fa: ["1\n", "2\n", "3\n"],
47
            self.fb: ["1\n", "2\n", "3\n"],
48
            "f_c": ["1\n", "2\n", "3\n"]}
49
50
        # A patch that was applied for some reason to make things complicated
51
        diff_dict = {self.fb: Diff(file_dict[self.fb])}
52
        diff_dict[self.fb].change_line(3, "3\n", "3_changed\n")
53
54
        # File contents after the patch was applied, that's what's in the files
55
        current_file_dict = {
56
            filename: diff_dict[filename].modified
57
            if filename in diff_dict else file_dict[filename]
58
            for filename in (self.fa, self.fb)}
59
        for filename in current_file_dict:
60
            with open(filename, 'w') as handle:
61
                handle.writelines(current_file_dict[filename])
62
63
        # End file contents after the patch and the OpenEditorAction was
64
        # applied
65
        expected_file_dict = {
66
            self.fa: ["1\n", "3\n"],
67
            self.fb: ["1\n", "3_changed\n"],
68
            "f_c": ["1\n", "2\n", "3\n"]}
69
70
        section = Section("")
71
        section.append(Setting("editor", ""))
72
        uut = OpenEditorAction()
73
        subprocess.call = self.fake_edit
74
        diff_dict = uut.apply_from_section(
75
            Result.from_values("origin", "msg", self.fa),
76
            file_dict,
77
            diff_dict,
78
            section)
79
        diff_dict = uut.apply_from_section(
80
            Result.from_values("origin", "msg", self.fb),
81
            file_dict,
82
            diff_dict,
83
            section)
84
85
        for filename in diff_dict:
86
            file_dict[filename] = (
87
                diff_dict[filename].modified)
88
89
        self.assertEqual(file_dict, expected_file_dict)
90
91
    def test_subl(self):
92
        file_dict = {self.fa: []}
93
        section = Section("")
94
        section.append(Setting("editor", "subl"))
95
        uut = OpenEditorAction()
96
        subprocess.call = self.fake_edit_subl
97
        diff_dict = uut.apply_from_section(
98
            Result.from_values("origin", "msg", self.fa),
99
            file_dict,
100
            {},
101
            section)
102
        file_dict[self.fa] = diff_dict[self.fa].modified
103
104
        self.assertEqual(file_dict, file_dict)
105
106
    def test_is_applicable(self):
107
        result1 = Result("", "")
108
        result2 = Result.from_values("", "", "")
109
        invalid_result = ""
110
        self.assertFalse(OpenEditorAction.is_applicable(result1, None, None))
111
        self.assertTrue(OpenEditorAction.is_applicable(result2, None, None))
112
113
        self.assertFalse(
114
            OpenEditorAction.is_applicable(invalid_result, None, None))
115
116
117
if __name__ == '__main__':
118
    unittest.main(verbosity=2)
119