File: test_diff_stream.py

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (160 lines) | stat: -rw-r--r-- 4,538 bytes parent folder | download
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
# 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

import mozunit

from mozversioncontrol import get_repository_object

STEPS = {
    "hg": [
        """
        echo constant > constant.txt
        hg add constant.txt
        hg commit -m "BASE PATCH"
        """,
        """
        echo foo > file1.txt
        echo autre > anotherfile.txt
        hg add file1.txt anotherfile.txt
        hg commit -m "FIRST PATCH"
        echo bar > file1.txt
        """,
        # Create some files for path testing.
        """
       mkdir subdir
       echo content > subdir/file.c
       echo content > subdir/file.json
       echo content > top.cpp
       echo content > top.md
       hg add top.* subdir/file.*
       hg commit -m "Set up some files for path testing"
       """,
    ],
    "git": [
        """
        git branch -m main
        echo constant > constant.txt
        git add constant.txt
        git commit -m "BASE PATCH"
        """,
        """
        git switch -c dev
        echo foo > file1.txt
        echo autre > anotherfile.txt
        git add file1.txt anotherfile.txt
        git commit -m "FIRST PATCH"
        echo bar > file1.txt
        git add file1.txt
        """,
        # Create some files for path testing.
        """
       mkdir subdir
       echo content > subdir/file.c
       echo content > subdir/file.json
       echo content > top.cpp
       echo content > top.md
       git add top.* subdir/file.*
       git commit -m "Set up some files for path testing"
       """,
    ],
    "jj": [
        # Make a base commit.
        """
        echo constant > constant.txt
        jj commit -m "BASE PATCH"
        """,
        # Create a conflict and then resolve the conflict.
        """
        echo foo > file1.txt
        jj desc -m "FIRST PATCH"
        jj new "description('BASE PATCH')"
        echo notfoo > file1.txt
        echo bar > anotherfile.txt
        jj desc -m "OTHER PATCH"
        jj new "description('FIRST PATCH')" @ -m "SECOND PATCH"
        jj new -m "resolve conflict"
        echo merged > file1.txt
       """,
        # Create some files for path testing.
        """
       mkdir subdir
       echo content > subdir/file.c
       echo content > subdir/file.json
       echo content > top.cpp
       echo content > top.md
       jj commit -m "Set up some files for path testing"
       """,
    ],
}


def test_diff_stream(repo):
    vcs = get_repository_object(repo.dir)

    # Create a base commit.
    repo.execute_next_step()
    base_rev = vcs.head_ref

    # Add some commits on top.
    repo.execute_next_step()

    def changed_files(stream):
        files = set()
        for line in stream:
            print(line, end="")
            if m := re.match(r"diff --git \w/(\S+)", line):
                files.add(m[1])
        return files

    # Default: "uncommitted" changes (meaning @ in jj), except in hg
    # (see bug 1993225)
    files = changed_files(vcs.diff_stream())
    if vcs.name != "hg":
        assert "file1.txt" in files
        assert "anotherfile.txt" not in files
        assert "constant.txt" not in files
    else:
        assert "file1.txt" in files
        assert "anotherfile.txt" in files
        assert "constant.txt" not in files

    # Changes in selected revision ("BASE PATCH")
    files = changed_files(vcs.diff_stream(base_rev))
    assert "file1.txt" not in files
    assert "anotherfile.txt" not in files
    assert "constant.txt" in files

    # All changes since the main branch.
    #
    # For jj, verify that diff_stream only includes files modified in mutable
    # changes (git diff HEAD would include all files in the repository because
    # of the dummy conflict commit).
    range = {
        "hg": f"{base_rev}::",
        "git": "main..",
        "jj": f"{base_rev}..@",
    }[vcs.name]
    files = changed_files(vcs.diff_stream(range))
    assert "file1.txt" in files
    assert "anotherfile.txt" in files
    assert "constant.txt" not in files

    # Create some files in a subdir
    repo.execute_next_step()

    files = changed_files(
        vcs.diff_stream(
            vcs.head_ref, extensions=(".cpp", ".c", ".cc", ".h", ".m", ".mm")
        )
    )
    assert "top.cpp" in files
    assert "top.md" not in files
    assert "subdir/file.c" in files
    assert "subdir/file.json" not in files


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