Completed
Pull Request — master (#2132)
by Udayan
01:56
created

ApplyPatchActionTest.test_apply_delete()   A

Complexity

Conditions 2

Size

Total Lines 19

Duplication

Lines 3
Ratio 15.79 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 3
loc 19
rs 9.4285
c 1
b 0
f 0
1
import unittest
2
from os.path import isfile
3
4
from coalib.misc.ContextManagers import make_temp
5
from coalib.results.Diff import Diff
6
from coalib.results.Result import Result
7
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
8
from coalib.settings.Section import Section
9
10
11
class ApplyPatchActionTest(unittest.TestCase):
12
13
    def test_apply(self):
14
        uut = ApplyPatchAction()
15
        with make_temp() as f_a, make_temp() as f_b, make_temp() as f_c:
16
17
            file_dict = {
18
                f_a: ["1\n", "2\n", "3\n"],
19
                f_b: ["1\n", "2\n", "3\n"],
20
                f_c: ["1\n", "2\n", "3\n"]
21
            }
22
            expected_file_dict = {
23
                f_a: ["1\n", "3_changed\n"],
24
                f_b: ["1\n", "2\n", "3_changed\n"],
25
                f_c: ["1\n", "2\n", "3\n"]
26
            }
27
28
            file_diff_dict = {}
29
30
            diff = Diff(file_dict[f_a])
31
            diff.delete_line(2)
32
            uut.apply_from_section(Result("origin", "msg", diffs={f_a: diff}),
33
                                   file_dict,
34
                                   file_diff_dict,
35
                                   Section("t"))
36
37
            diff = Diff(file_dict[f_a])
38
            diff.change_line(3, "3\n", "3_changed\n")
39
            uut.apply_from_section(Result("origin", "msg", diffs={f_a: diff}),
40
                                   file_dict,
41
                                   file_diff_dict,
42
                                   Section("t"))
43
44
            diff = Diff(file_dict[f_b])
45
            diff.change_line(3, "3\n", "3_changed\n")
46
            uut.apply(Result("origin", "msg", diffs={f_b: diff}),
47
                      file_dict,
48
                      file_diff_dict)
49
50
            for filename in file_diff_dict:
51
                file_dict[filename] = file_diff_dict[filename].modified
52
53
            self.assertEqual(file_dict, expected_file_dict)
54
            with open(f_a) as fa:
55
                self.assertEqual(file_dict[f_a], fa.readlines())
56
            with open(f_b) as fb:
57
                self.assertEqual(file_dict[f_b], fb.readlines())
58
            with open(f_c) as fc:
59
                # File c is unchanged and should be untouched
60
                self.assertEqual([], fc.readlines())
61
62 View Code Duplication
    def test_apply_orig_option(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
        uut = ApplyPatchAction()
64
        with make_temp() as f_a, make_temp() as f_b:
65
            file_dict = {
66
                f_a: ["1\n", "2\n", "3\n"],
67
                f_b: ["1\n", "2\n", "3\n"]
68
                }
69
            expected_file_dict = {
70
                f_a: ["1\n", "2\n", "3_changed\n"],
71
                f_b: ["1\n", "2\n", "3_changed\n"]
72
                }
73
            file_diff_dict = {}
74
            diff = Diff(file_dict[f_a])
75
            diff.change_line(3, "3\n", "3_changed\n")
76
            uut.apply(Result("origin", "msg", diffs={f_a: diff}),
77
                      file_dict,
78
                      file_diff_dict,
79
                      no_orig=True)
80
            diff = Diff(file_dict[f_b])
81
            diff.change_line(3, "3\n", "3_changed\n")
82
            uut.apply(Result("origin", "msg", diffs={f_b: diff}),
83
                      file_dict,
84
                      file_diff_dict,
85
                      no_orig=False)
86
            self.assertFalse(isfile(f_a+".orig"))
87
            self.assertTrue(isfile(f_b+".orig"))
88
89
            for filename in file_diff_dict:
90
                file_dict[filename] = file_diff_dict[filename].modified
91
92
            self.assertEqual(file_dict, expected_file_dict)
93
94 View Code Duplication
    def test_apply_rename(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
95
        uut = ApplyPatchAction()
96
        with make_temp() as f_a:
97
            file_dict = {f_a: ["1\n", "2\n", "3\n"]}
98
            expected_file_dict = {f_a: ["1\n", "2_changed\n", "3_changed\n"]}
99
            file_diff_dict = {}
100
            diff = Diff(file_dict[f_a], rename=f_a+".renamed")
101
            diff.change_line(3, "3\n", "3_changed\n")
102
            uut.apply(Result("origin", "msg", diffs={f_a: diff}),
103
                      file_dict,
104
                      file_diff_dict)
105
            self.assertTrue(isfile(f_a+".orig"))
106
            self.assertTrue(isfile(f_a+".renamed"))
107
108
            diff = Diff(file_dict[f_a])
109
            diff.change_line(2, "2\n", "2_changed\n")
110
            uut.apply(Result("origin", "msg", diffs={f_a: diff}),
111
                      file_dict,
112
                      file_diff_dict)
113
            self.assertTrue(isfile(f_a+".renamed.orig"))
114
115
            for filename in file_diff_dict:
116
                file_dict[filename] = file_diff_dict[filename].modified
117
118
            self.assertEqual(file_dict, expected_file_dict)
119
120
    def test_apply_delete(self):
121
        uut = ApplyPatchAction()
122
        with make_temp() as f_a:
123
            file_dict = {f_a: ["1\n", "2\n", "3\n"]}
124
            file_diff_dict = {}
125
            diff = Diff(file_dict[f_a], delete=True)
126
            uut.apply(Result("origin", "msg", diffs={f_a: diff}),
127
                      file_dict,
128
                      file_diff_dict)
129
            self.assertFalse(isfile(f_a))
130
            self.assertTrue(isfile(f_a+".orig"))
131
132
            diff = Diff(file_dict[f_a])
133
            diff.change_line(3, "3\n", "3_changed\n")
134
            uut.apply(Result("origin", "msg", diffs={f_a: diff}),
135
                      file_dict,
136
                      file_diff_dict)
137
            self.assertFalse(isfile(f_a+".orig"))
138
            open(f_a, 'w').close()
139
140
    def test_is_applicable(self):
141
        diff = Diff(["1\n", "2\n", "3\n"])
142
        diff.delete_line(2)
143
        patch_result = Result("", "", diffs={'f': diff})
144
        self.assertTrue(
145
            ApplyPatchAction.is_applicable(patch_result, {}, {}))
146
147
    def test_is_applicable_conflict(self):
148
        diff = Diff(["1\n", "2\n", "3\n"])
149
        diff.add_lines(2, ['a line'])
150
151
        conflict_result = Result("", "", diffs={'f': diff})
152
        # Applying the same diff twice will result in a conflict
153
        self.assertFalse(
154
            ApplyPatchAction.is_applicable(conflict_result, {}, {'f': diff}))
155
156
    def test_is_applicable_empty_patch(self):
157
        empty_patch_result = Result("", "", diffs={})
158
        self.assertFalse(
159
            ApplyPatchAction.is_applicable(empty_patch_result, {}, {}))
160
161
    def test_is_applicable_without_patch(self):
162
        result = Result("", "")
163
        self.assertFalse(ApplyPatchAction.is_applicable(result, {}, {}))
164