|
| 1 | +import subprocess |
| 2 | +from pathlib import Path |
| 3 | +import pytest |
| 4 | +from sqlmesh.utils.git import GitClient |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def git_repo(tmp_path: Path) -> Path: |
| 9 | + repo_path = tmp_path / "test_repo" |
| 10 | + repo_path.mkdir() |
| 11 | + subprocess.run(["git", "init", "-b", "main"], cwd=repo_path, check=True, capture_output=True) |
| 12 | + return repo_path |
| 13 | + |
| 14 | + |
| 15 | +def test_git_uncommitted_changes(git_repo: Path): |
| 16 | + git_client = GitClient(git_repo) |
| 17 | + |
| 18 | + test_file = git_repo / "model.sql" |
| 19 | + test_file.write_text("SELECT 1 AS a") |
| 20 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 21 | + subprocess.run( |
| 22 | + [ |
| 23 | + "git", |
| 24 | + "-c", |
| 25 | + "user.name=Max", |
| 26 | + "-c", |
| 27 | + "user.email=max@rb.com", |
| 28 | + "commit", |
| 29 | + "-m", |
| 30 | + "Initial commit", |
| 31 | + ], |
| 32 | + cwd=git_repo, |
| 33 | + check=True, |
| 34 | + capture_output=True, |
| 35 | + ) |
| 36 | + assert git_client.list_uncommitted_changed_files() == [] |
| 37 | + |
| 38 | + # make an unstaged change and see that it is listed |
| 39 | + test_file.write_text("SELECT 2 AS a") |
| 40 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 41 | + assert len(uncommitted) == 1 |
| 42 | + assert uncommitted[0].name == "model.sql" |
| 43 | + |
| 44 | + # stage the change and test that it is still detected |
| 45 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 46 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 47 | + assert len(uncommitted) == 1 |
| 48 | + assert uncommitted[0].name == "model.sql" |
| 49 | + |
| 50 | + |
| 51 | +def test_git_both_staged_and_unstaged_changes(git_repo: Path): |
| 52 | + git_client = GitClient(git_repo) |
| 53 | + |
| 54 | + file1 = git_repo / "model1.sql" |
| 55 | + file2 = git_repo / "model2.sql" |
| 56 | + file1.write_text("SELECT 1") |
| 57 | + file2.write_text("SELECT 2") |
| 58 | + subprocess.run(["git", "add", "."], cwd=git_repo, check=True, capture_output=True) |
| 59 | + subprocess.run( |
| 60 | + [ |
| 61 | + "git", |
| 62 | + "-c", |
| 63 | + "user.name=Max", |
| 64 | + "-c", |
| 65 | + "user.email=max@rb.com", |
| 66 | + "commit", |
| 67 | + "-m", |
| 68 | + "Initial commit", |
| 69 | + ], |
| 70 | + cwd=git_repo, |
| 71 | + check=True, |
| 72 | + capture_output=True, |
| 73 | + ) |
| 74 | + |
| 75 | + # stage file1 |
| 76 | + file1.write_text("SELECT 10") |
| 77 | + subprocess.run(["git", "add", "model1.sql"], cwd=git_repo, check=True, capture_output=True) |
| 78 | + |
| 79 | + # modify file2 but don't stage it! |
| 80 | + file2.write_text("SELECT 20") |
| 81 | + |
| 82 | + # both should be detected |
| 83 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 84 | + assert len(uncommitted) == 2 |
| 85 | + file_names = {f.name for f in uncommitted} |
| 86 | + assert file_names == {"model1.sql", "model2.sql"} |
| 87 | + |
| 88 | + |
| 89 | +def test_git_untracked_files(git_repo: Path): |
| 90 | + git_client = GitClient(git_repo) |
| 91 | + initial_file = git_repo / "initial.sql" |
| 92 | + initial_file.write_text("SELECT 0") |
| 93 | + subprocess.run(["git", "add", "initial.sql"], cwd=git_repo, check=True, capture_output=True) |
| 94 | + subprocess.run( |
| 95 | + [ |
| 96 | + "git", |
| 97 | + "-c", |
| 98 | + "user.name=Max", |
| 99 | + "-c", |
| 100 | + "user.email=max@rb.com", |
| 101 | + "commit", |
| 102 | + "-m", |
| 103 | + "Initial commit", |
| 104 | + ], |
| 105 | + cwd=git_repo, |
| 106 | + check=True, |
| 107 | + capture_output=True, |
| 108 | + ) |
| 109 | + |
| 110 | + new_file = git_repo / "new_model.sql" |
| 111 | + new_file.write_text("SELECT 1") |
| 112 | + |
| 113 | + # untracked file should not appear in uncommitted changes |
| 114 | + assert git_client.list_uncommitted_changed_files() == [] |
| 115 | + |
| 116 | + # but in untracked |
| 117 | + untracked = git_client.list_untracked_files() |
| 118 | + assert len(untracked) == 1 |
| 119 | + assert untracked[0].name == "new_model.sql" |
| 120 | + |
| 121 | + |
| 122 | +def test_git_committed_changes(git_repo: Path): |
| 123 | + git_client = GitClient(git_repo) |
| 124 | + |
| 125 | + test_file = git_repo / "model.sql" |
| 126 | + test_file.write_text("SELECT 1") |
| 127 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 128 | + subprocess.run( |
| 129 | + [ |
| 130 | + "git", |
| 131 | + "-c", |
| 132 | + "user.name=Max", |
| 133 | + "-c", |
| 134 | + "user.email=max@rb.com", |
| 135 | + "commit", |
| 136 | + "-m", |
| 137 | + "Initial commit", |
| 138 | + ], |
| 139 | + cwd=git_repo, |
| 140 | + check=True, |
| 141 | + capture_output=True, |
| 142 | + ) |
| 143 | + |
| 144 | + subprocess.run( |
| 145 | + ["git", "checkout", "-b", "feature"], |
| 146 | + cwd=git_repo, |
| 147 | + check=True, |
| 148 | + capture_output=True, |
| 149 | + ) |
| 150 | + |
| 151 | + test_file.write_text("SELECT 2") |
| 152 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 153 | + subprocess.run( |
| 154 | + [ |
| 155 | + "git", |
| 156 | + "-c", |
| 157 | + "user.name=Max", |
| 158 | + "-c", |
| 159 | + "user.email=max@rb.com", |
| 160 | + "commit", |
| 161 | + "-m", |
| 162 | + "Update on feature branch", |
| 163 | + ], |
| 164 | + cwd=git_repo, |
| 165 | + check=True, |
| 166 | + capture_output=True, |
| 167 | + ) |
| 168 | + |
| 169 | + committed = git_client.list_committed_changed_files(target_branch="main") |
| 170 | + assert len(committed) == 1 |
| 171 | + assert committed[0].name == "model.sql" |
| 172 | + |
| 173 | + assert git_client.list_uncommitted_changed_files() == [] |
0 commit comments