Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""Test Code Format Command.
 
The formatters are captured rather than run, since what this module does is decide which files to format with which formatter. The
repository is written here so a file can be left out of the working tree while git still reports it."""
 
####################################################################################################################################
import io
import os
import tempfile
from contextlib import redirect_stdout
from unittest.mock import patch
 
from harness.test import *
 
from command.code.format import *
from common.error import *
from common.log import *
from common.storage import file_write, path_create
 
# What git reports for the repository, i.e. everything that is version controlled. src/gone.c is in the index but has been removed
# from the working tree and local is a path rather than a file, so neither is written as a file below.
FILE_LIST = (
    "README.md",
    "doc/release.py",
    "local",
    "src/build/help.auto.c.inc",
    "src/gone.c",
    "src/main.c",
    "src/main.h",
    "src/module.vendor.c.inc",
    "src/vendor.vendor.h",
    "test/lib/common/log.py",
    "test/src/test.c",
    "test/test.py",
)
 
 
####################################################################################################################################
class Config:
    """What the command reads from the command line."""
 
    def __init__(self, repo_path, check=False):
        self.repo_path = repo_path
        self.check = check
 
 
####################################################################################################################################
def _repo_create(path):
    """Write the repository, i.e. everything git reports that is a file that is still there."""
 
    for file in FILE_LIST:
        if file != "src/gone.c":
            file_write(os.path.join(path, file), "")
 
    # A path where git reported one, which is what it does for a symlink to a directory
    os.remove(os.path.join(path, "local"))
    path_create(os.path.join(path, "local"))
 
    return path
 
 
####################################################################################################################################
def _cmd_code_format(config, fail=None):
    """Run the command with the formatters captured rather than run.
 
    Returns the commands and the error raised, if any. Naming a formatter in fail makes it report that it found something."""
 
    command_list = []
    error = None
 
    def exec_fake(command, result_expect=0, show_output=False):
        command_list.append(command)
 
        if command.startswith("git "):
            return "\n".join(FILE_LIST) + "\n"
 
        if fail is not None and command.startswith(fail):
            raise ToolError("%s terminated unexpectedly [1]" % fail)
 
        return ""
 
    output = io.StringIO()
    log_init(INFO, False)
 
    try:
        with patch("command.code.format.exec_one", exec_fake):
            with redirect_stdout(output):
                try:
                    cmd_code_format(config)
                except ToolError as exception:
                    error = str(exception)
    finally:
        log_init(INFO, True)
 
    return command_list, error, output.getvalue()
 
 
####################################################################################################################################
def test_code_format():
    """Every version controlled file goes to the formatter for its language and nothing else does."""
 
    with tempfile.TemporaryDirectory() as path:
        path_repo = _repo_create(path)
 
        command_list, error, output = _cmd_code_format(Config(path_repo))
 
        assert_is_none(error)
        assert_equal(output, "P00   INFO: code format\n")
 
        # Only the version controlled files are formatted, which is what git reports
        assert_equal(command_list[0], "git -C %s ls-files -c --others --exclude-standard" % path_repo)
 
        # The C, which is formatted in place
        assert_equal(
            command_list[1],
            "uncrustify -c %s/test/uncrustify.cfg --replace --no-backup %s/src/main.c %s/src/main.h" % ((path_repo,) * 3),
        )
 
        # The python, which is formatted to the same line length as the rest of the project
        assert_equal(
            command_list[2],
            "black --line-length=132 %s/doc/release.py %s/test/lib/common/log.py %s/test/test.py" % ((path_repo,) * 3),
        )
 
 
####################################################################################################################################
def test_code_format_skip():
    """A file that is not formatted is left out of both formatters."""
 
    with tempfile.TemporaryDirectory() as path:
        path_repo = _repo_create(path)
 
        command_list, error, output = _cmd_code_format(Config(path_repo))
        command = " ".join(command_list)
 
        # The test template does not format correctly since it is not valid C until the harness fills it in
        assert_not_in("test/src/test.c", command)
 
        # Nothing vendorized or generated, since neither is ours to format
        assert_not_in("help.auto.c.inc", command)
        assert_not_in("vendor.vendor.h", command)
        assert_not_in("module.vendor.c.inc", command)
 
        # Nothing in a language neither formatter handles
        assert_not_in("README.md", command)
        assert_not_in("README.md", command)
 
        # A file that git reports but is no longer in the working tree, since there is nothing there to format
        assert_not_in("gone.c", command)
 
        # An entry that is a path rather than a file, which is also not something to check the permissions of
        assert_not_in("local", command)
 
 
####################################################################################################################################
def test_code_format_check():
    """The check reports what is not formatted rather than formatting it, and says how to fix what it found."""
 
    with tempfile.TemporaryDirectory() as path:
        path_repo = _repo_create(path)
 
        command_list, error, output = _cmd_code_format(Config(path_repo, check=True))
 
        assert_is_none(error)
        assert_equal(output, "P00   INFO: code format check\n")
        assert_in("uncrustify -c %s/test/uncrustify.cfg --check " % path_repo, command_list[1])
 
        # Black shows what it would change so the failure says what is wrong rather than only that something is
        assert_in("black --line-length=132 --check --diff ", command_list[2])
 
        # A check that finds something says how to fix it, since CI is where it usually runs
        for formatter in ("uncrustify", "black"):
            command_list, error, output = _cmd_code_format(Config(path_repo, check=True), fail=formatter)
 
            assert_equal(error, "%s terminated unexpectedly [1]\nHINT: run 'test.py code-format' to format the code" % formatter)
 
        # Formatting rather than checking reports what the formatter said and nothing else, since there is nothing to fix by hand
        command_list, error, output = _cmd_code_format(Config(path_repo), fail="uncrustify")
 
        assert_equal(error, "uncrustify terminated unexpectedly [1]")
 
 
####################################################################################################################################
def test_code_format_permission():
    """Nothing is executable that is not meant to be, which formatting can munge."""
 
    with tempfile.TemporaryDirectory() as path:
        path_repo = _repo_create(path)
 
        # The files that are meant to be executable are
        for file in ("doc/release.py", "test/test.py"):
            os.chmod(os.path.join(path_repo, file), 0o755)
 
        command_list, error, output = _cmd_code_format(Config(path_repo))
 
        assert_is_none(error)
 
        # Nothing else is
        os.chmod(os.path.join(path_repo, "src/main.c"), 0o750)
 
        command_list, error, output = _cmd_code_format(Config(path_repo))
 
        assert_equal(error, "expected mode '0640' for 'src/main.c' but found '0750'")