1
|
|
|
import os |
2
|
|
|
import subprocess |
3
|
|
|
import tempfile |
4
|
|
|
import unittest |
5
|
|
|
|
6
|
|
|
from coalib.results.Diff import Diff |
7
|
|
|
from coalib.results.Result import Result |
8
|
|
|
from coalib.results.result_actions.OpenEditorAction import OpenEditorAction |
9
|
|
|
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction |
10
|
|
|
from coalib.settings.Section import Section, Setting |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class OpenEditorActionTest(unittest.TestCase): |
14
|
|
|
|
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
|
|
|
self.old_subprocess_call = subprocess.call |
39
|
|
|
|
40
|
|
|
def tearDown(self): |
41
|
|
|
os.remove(self.fa) |
42
|
|
|
os.remove(self.fb) |
43
|
|
|
subprocess.call = self.old_subprocess_call |
44
|
|
|
|
45
|
|
|
def test_apply(self): |
46
|
|
|
# Initial file contents, *before* a patch was applied |
47
|
|
|
file_dict = { |
48
|
|
|
self.fa: ["1\n", "2\n", "3\n"], |
49
|
|
|
self.fb: ["1\n", "2\n", "3\n"], |
50
|
|
|
"f_c": ["1\n", "2\n", "3\n"]} |
51
|
|
|
|
52
|
|
|
# A patch that was applied for some reason to make things complicated |
53
|
|
|
diff_dict = {self.fb: Diff(file_dict[self.fb])} |
54
|
|
|
diff_dict[self.fb].change_line(3, "3\n", "3_changed\n") |
55
|
|
|
|
56
|
|
|
# File contents after the patch was applied, that's what's in the files |
57
|
|
|
current_file_dict = { |
58
|
|
|
filename: diff_dict[filename].modified |
59
|
|
|
if filename in diff_dict else file_dict[filename] |
60
|
|
|
for filename in (self.fa, self.fb)} |
61
|
|
|
for filename in current_file_dict: |
62
|
|
|
with open(filename, 'w') as handle: |
63
|
|
|
handle.writelines(current_file_dict[filename]) |
64
|
|
|
|
65
|
|
|
# End file contents after the patch and the OpenEditorAction was |
66
|
|
|
# applied |
67
|
|
|
expected_file_dict = { |
68
|
|
|
self.fa: ["1\n", "3\n"], |
69
|
|
|
self.fb: ["1\n", "3_changed\n"], |
70
|
|
|
"f_c": ["1\n", "2\n", "3\n"]} |
71
|
|
|
|
72
|
|
|
section = Section("") |
73
|
|
|
section.append(Setting("editor", "")) |
74
|
|
|
uut = OpenEditorAction() |
75
|
|
|
subprocess.call = self.fake_edit |
76
|
|
|
diff_dict = uut.apply_from_section( |
77
|
|
|
Result.from_values("origin", "msg", self.fa), |
78
|
|
|
file_dict, |
79
|
|
|
diff_dict, |
80
|
|
|
section) |
81
|
|
|
diff_dict = uut.apply_from_section( |
82
|
|
|
Result.from_values("origin", "msg", self.fb), |
83
|
|
|
file_dict, |
84
|
|
|
diff_dict, |
85
|
|
|
section) |
86
|
|
|
|
87
|
|
|
for filename in diff_dict: |
88
|
|
|
file_dict[filename] = ( |
89
|
|
|
diff_dict[filename].modified) |
90
|
|
|
|
91
|
|
|
self.assertEqual(file_dict, expected_file_dict) |
92
|
|
|
|
93
|
|
|
def test_apply_rename(self): |
94
|
|
|
# Initial file contents, *before* a patch was applied |
95
|
|
|
file_dict = { |
96
|
|
|
self.fa: ["1\n", "2\n", "3\n"]} |
97
|
|
|
|
98
|
|
|
# A patch that was applied for some reason to make things complicated |
99
|
|
|
file_diff_dict = {} |
100
|
|
|
diff = Diff(file_dict[self.fa], rename=self.fa+".renamed") |
101
|
|
|
diff.change_line(3, "3\n", "3_changed\n") |
102
|
|
|
ApplyPatchAction().apply( |
103
|
|
|
Result("origin", "msg", diffs={self.fa: diff}), |
104
|
|
|
file_dict, |
105
|
|
|
file_diff_dict) |
106
|
|
|
# End file contents after the patch and the OpenEditorAction was |
107
|
|
|
# applied |
108
|
|
|
expected_file_dict = { |
109
|
|
|
self.fa: ["1\n", "3_changed\n"]} |
110
|
|
|
|
111
|
|
|
section = Section("") |
112
|
|
|
section.append(Setting("editor", "")) |
113
|
|
|
uut = OpenEditorAction() |
114
|
|
|
subprocess.call = self.fake_edit |
115
|
|
|
diff_dict = uut.apply_from_section( |
116
|
|
|
Result.from_values("origin", "msg", self.fa), |
117
|
|
|
file_dict, |
118
|
|
|
file_diff_dict, |
119
|
|
|
section) |
120
|
|
|
|
121
|
|
|
for filename in diff_dict: |
122
|
|
|
file_dict[filename] = ( |
123
|
|
|
file_diff_dict[filename].modified) |
124
|
|
|
|
125
|
|
|
self.assertEqual(file_dict, expected_file_dict) |
126
|
|
|
open(self.fa, 'w').close() |
127
|
|
|
|
128
|
|
|
def test_subl(self): |
129
|
|
|
file_dict = {self.fa: []} |
130
|
|
|
section = Section("") |
131
|
|
|
section.append(Setting("editor", "subl")) |
132
|
|
|
uut = OpenEditorAction() |
133
|
|
|
subprocess.call = self.fake_edit_subl |
134
|
|
|
diff_dict = uut.apply_from_section( |
135
|
|
|
Result.from_values("origin", "msg", self.fa), |
136
|
|
|
file_dict, |
137
|
|
|
{}, |
138
|
|
|
section) |
139
|
|
|
file_dict[self.fa] = diff_dict[self.fa].modified |
140
|
|
|
|
141
|
|
|
self.assertEqual(file_dict, file_dict) |
142
|
|
|
|
143
|
|
|
def test_is_applicable(self): |
144
|
|
|
result1 = Result("", "") |
145
|
|
|
result2 = Result.from_values("", "", "") |
146
|
|
|
result3 = Result.from_values("", "", "file") |
147
|
|
|
invalid_result = "" |
148
|
|
|
self.assertFalse(OpenEditorAction.is_applicable(result1, None, {})) |
149
|
|
|
self.assertTrue(OpenEditorAction.is_applicable(result2, None, {})) |
150
|
|
|
# Check non-existent file |
151
|
|
|
self.assertFalse(OpenEditorAction.is_applicable(result3, None, {})) |
152
|
|
|
|
153
|
|
|
self.assertFalse( |
154
|
|
|
OpenEditorAction.is_applicable(invalid_result, None, {})) |
155
|
|
|
|