File: test_reformatting.py

package info (click to toggle)
debmutate 0.80
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 588 kB
  • sloc: python: 8,346; makefile: 12; sh: 1
file content (162 lines) | stat: -rw-r--r-- 4,441 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python
# Copyright (C) 2019 Jelmer Vernooij
# This file is a part of debmutate.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for lintian brush reformatting tools."""

from debmutate.reformatting import (
    FormattingUnpreservable,
    GeneratedFile,
    check_generated_file,
    check_preserve_formatting,
    edit_formatted_file,
)

from . import TestCase, TestCaseInTempDir


class CheckPreserveFormattingTests(TestCase):
    def test_formatting_same(self):
        check_preserve_formatting("FOO  ", "FOO  ", "debian/blah")

    def test_formatting_different(self):
        self.assertRaises(
            FormattingUnpreservable,
            check_preserve_formatting,
            "FOO ",
            "FOO  ",
            "debian/blah",
        )

    def test_diff(self):
        e = FormattingUnpreservable("debian/blah", "FOO X\n", "FOO  X\n")
        self.assertEqual(
            "".join(e.diff()),
            """\
--- original
+++ rewritten
@@ -1 +1 @@
-FOO X
+FOO  X
""",
        )

    def test_reformatting_allowed(self):
        check_preserve_formatting(
            "FOO  ", "FOO ", "debian/blah", allow_reformatting=True
        )


class GeneratedFileTests(TestCaseInTempDir):
    def test_generated_control_file(self):
        self.build_tree_contents(
            [
                ("debian/",),
                (
                    "debian/control.in",
                    """\
Source: blah
""",
                ),
            ]
        )
        self.assertRaises(GeneratedFile, check_generated_file, "debian/control")

    def test_missing(self):
        check_generated_file("debian/control")

    def test_do_not_edit(self):
        self.build_tree_contents(
            [
                ("debian/",),
                (
                    "debian/control",
                    """\
# DO NOT EDIT
# This file was generated by blah

Source: blah
Testsuite: autopkgtest

""",
                ),
            ]
        )
        self.assertRaises(GeneratedFile, check_generated_file, "debian/control")

    def test_do_not_edit_after_header(self):
        # check_generated_file() only checks the first 20 lines.
        self.build_tree_contents(
            [
                ("debian/",),
                (
                    "debian/control",
                    ("\n" * 50)
                    + """\
# DO NOT EDIT
# This file was generated by blah

Source: blah
Testsuite: autopkgtest

""",
                ),
            ]
        )
        check_generated_file("debian/control")


class EditFormattedFileTests(TestCaseInTempDir):
    def test_unchanged(self):
        self.build_tree_contents([("a", "some content\n")])
        self.assertFalse(
            edit_formatted_file(
                "a", "some content\n", "some content reformatted\n", "some content\n"
            )
        )
        self.assertFalse(
            edit_formatted_file(
                "a", "some content\n", "some content\n", "some content\n"
            )
        )
        self.assertFalse(
            edit_formatted_file(
                "a",
                "some content\n",
                "some content reformatted\n",
                "some content reformatted\n",
            )
        )

    def test_changed(self):
        self.build_tree_contents([("a", "some content\n")])
        self.assertTrue(
            edit_formatted_file(
                "a", "some content\n", "some content\n", "new content\n"
            )
        )
        self.assertFileEqual("new content\n", "a")

    def test_unformattable(self):
        self.assertRaises(
            FormattingUnpreservable,
            edit_formatted_file,
            "a",
            "some content\n",
            "reformatted content\n",
            "new content\n",
        )