Completed
Pull Request — master (#2132)
by Udayan
02:44 queued 44s
created

ApplyPatchActionTest.test_apply_delete()   A

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 3
Ratio 15 %

Importance

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