File: test_commit.py

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (139 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download | duplicates (2)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import re
from datetime import datetime
from pathlib import Path

import mozunit

from mozversioncontrol import get_repository_object

STEPS = {
    "hg": [
        "",
        """
        echo "bar" >> bar
        echo "baz" > foo
        """,
    ],
    "git": [
        "",
        """
        echo "bar" >> bar
        echo "baz" > foo
        """,
    ],
    "jj": [
        """
        jj describe -m "Ignore file for testing"
        echo foo > .gitignore
        jj new
        """,
        """
        echo "bar" >> bar
        echo "baz" > foo
        """,
    ],
}


def test_commit(repo):
    vcs = get_repository_object(repo.dir)
    assert vcs.working_directory_clean()

    # Setup step for jj to allow untracked changes.
    repo.execute_next_step()

    # Modify both foo and bar
    repo.execute_next_step()
    if repo.vcs != "jj":
        # jj never has a dirty working directory.
        assert not vcs.working_directory_clean()

    date_string = "2017-07-14 02:40:00 +0000"

    # Commit just bar
    vcs.commit(
        message="Modify bar\n\nbut not baz",
        author="Testing McTesterson <test@example.org>",
        date=date_string,
        paths=["bar"],
    )

    original_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S %z")
    date_from_vcs = vcs.get_last_modified_time_for_file(Path("bar"))

    assert original_date == date_from_vcs

    # We only committed bar, so foo is still keeping the working dir dirty. jj
    # always treats the working directory as clean, because the top commit holds
    # any changes in it.
    if repo.vcs == "jj":
        assert vcs.working_directory_clean()
    else:
        assert not vcs.working_directory_clean()

    if repo.vcs == "git":
        log_cmd = ["log", "-1", "--format=%an,%ae,%aD,%B"]
        patch_cmd = ["log", "-1", "-p"]
    elif repo.vcs == "hg":
        log_cmd = [
            "log",
            "-l",
            "1",
            "-T",
            "{person(author)},{email(author)},{date|rfc822date},{desc}",
        ]
        patch_cmd = ["log", "-l", "1", "-p"]
    elif repo.vcs == "jj":
        log_cmd = [
            "log",
            "-n1",
            "--no-graph",
            "-r@-",
            "-T",
            'separate(",", author.name(), author.email(), commit_timestamp(self).format("%a, %d %b %Y %H:%M:%S %z"), description)',
        ]
        patch_cmd = ["show", "@-", "--git"]

    # Verify commit metadata (we rstrip to normalize trivial differences)
    log = vcs._run(*log_cmd).rstrip()
    assert log == (
        "Testing McTesterson,test@example.org,Fri, 14 "
        "Jul 2017 02:40:00 +0000,Modify bar\n\nbut not baz"
    )

    # Verify only the intended file was added to the commit
    patch = vcs._run(*patch_cmd)

    def find_diff_marker(patch: str, filename: str):
        patterns = [
            rf"^diff --git a/{re.escape(filename)} b/{re.escape(filename)}$",
            rf"^Modified regular file {re.escape(filename)}:$",
        ]

        matches = [
            line
            for line in patch.splitlines()
            if any(re.fullmatch(p, line) for p in patterns)
        ]

        assert matches, f"No diff marker found for '{filename}'"
        assert (
            len(matches) == 1
        ), f"More than one diff marker for '{filename}': {matches}"

        return matches[0]

    marker = find_diff_marker(patch, "bar")

    assert marker in [
        "diff --git a/bar b/bar",
        "Modified regular file bar:",
    ]


if __name__ == "__main__":
    mozunit.main()