tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_diff_stream.py (4538B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import re
      6 
      7 import mozunit
      8 
      9 from mozversioncontrol import get_repository_object
     10 
     11 STEPS = {
     12    "hg": [
     13        """
     14        echo constant > constant.txt
     15        hg add constant.txt
     16        hg commit -m "BASE PATCH"
     17        """,
     18        """
     19        echo foo > file1.txt
     20        echo autre > anotherfile.txt
     21        hg add file1.txt anotherfile.txt
     22        hg commit -m "FIRST PATCH"
     23        echo bar > file1.txt
     24        """,
     25        # Create some files for path testing.
     26        """
     27       mkdir subdir
     28       echo content > subdir/file.c
     29       echo content > subdir/file.json
     30       echo content > top.cpp
     31       echo content > top.md
     32       hg add top.* subdir/file.*
     33       hg commit -m "Set up some files for path testing"
     34       """,
     35    ],
     36    "git": [
     37        """
     38        git branch -m main
     39        echo constant > constant.txt
     40        git add constant.txt
     41        git commit -m "BASE PATCH"
     42        """,
     43        """
     44        git switch -c dev
     45        echo foo > file1.txt
     46        echo autre > anotherfile.txt
     47        git add file1.txt anotherfile.txt
     48        git commit -m "FIRST PATCH"
     49        echo bar > file1.txt
     50        git add file1.txt
     51        """,
     52        # Create some files for path testing.
     53        """
     54       mkdir subdir
     55       echo content > subdir/file.c
     56       echo content > subdir/file.json
     57       echo content > top.cpp
     58       echo content > top.md
     59       git add top.* subdir/file.*
     60       git commit -m "Set up some files for path testing"
     61       """,
     62    ],
     63    "jj": [
     64        # Make a base commit.
     65        """
     66        echo constant > constant.txt
     67        jj commit -m "BASE PATCH"
     68        """,
     69        # Create a conflict and then resolve the conflict.
     70        """
     71        echo foo > file1.txt
     72        jj desc -m "FIRST PATCH"
     73        jj new "description('BASE PATCH')"
     74        echo notfoo > file1.txt
     75        echo bar > anotherfile.txt
     76        jj desc -m "OTHER PATCH"
     77        jj new "description('FIRST PATCH')" @ -m "SECOND PATCH"
     78        jj new -m "resolve conflict"
     79        echo merged > file1.txt
     80       """,
     81        # Create some files for path testing.
     82        """
     83       mkdir subdir
     84       echo content > subdir/file.c
     85       echo content > subdir/file.json
     86       echo content > top.cpp
     87       echo content > top.md
     88       jj commit -m "Set up some files for path testing"
     89       """,
     90    ],
     91 }
     92 
     93 
     94 def test_diff_stream(repo):
     95    vcs = get_repository_object(repo.dir)
     96 
     97    # Create a base commit.
     98    repo.execute_next_step()
     99    base_rev = vcs.head_ref
    100 
    101    # Add some commits on top.
    102    repo.execute_next_step()
    103 
    104    def changed_files(stream):
    105        files = set()
    106        for line in stream:
    107            print(line, end="")
    108            if m := re.match(r"diff --git \w/(\S+)", line):
    109                files.add(m[1])
    110        return files
    111 
    112    # Default: "uncommitted" changes (meaning @ in jj), except in hg
    113    # (see bug 1993225)
    114    files = changed_files(vcs.diff_stream())
    115    if vcs.name != "hg":
    116        assert "file1.txt" in files
    117        assert "anotherfile.txt" not in files
    118        assert "constant.txt" not in files
    119    else:
    120        assert "file1.txt" in files
    121        assert "anotherfile.txt" in files
    122        assert "constant.txt" not in files
    123 
    124    # Changes in selected revision ("BASE PATCH")
    125    files = changed_files(vcs.diff_stream(base_rev))
    126    assert "file1.txt" not in files
    127    assert "anotherfile.txt" not in files
    128    assert "constant.txt" in files
    129 
    130    # All changes since the main branch.
    131    #
    132    # For jj, verify that diff_stream only includes files modified in mutable
    133    # changes (git diff HEAD would include all files in the repository because
    134    # of the dummy conflict commit).
    135    range = {
    136        "hg": f"{base_rev}::",
    137        "git": "main..",
    138        "jj": f"{base_rev}..@",
    139    }[vcs.name]
    140    files = changed_files(vcs.diff_stream(range))
    141    assert "file1.txt" in files
    142    assert "anotherfile.txt" in files
    143    assert "constant.txt" not in files
    144 
    145    # Create some files in a subdir
    146    repo.execute_next_step()
    147 
    148    files = changed_files(
    149        vcs.diff_stream(
    150            vcs.head_ref, extensions=(".cpp", ".c", ".cc", ".h", ".m", ".mm")
    151        )
    152    )
    153    assert "top.cpp" in files
    154    assert "top.md" not in files
    155    assert "subdir/file.c" in files
    156    assert "subdir/file.json" not in files
    157 
    158 
    159 if __name__ == "__main__":
    160    mozunit.main()