|
| 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"], 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 | + ["git", "commit", "-m", "onitial commit"], |
| 23 | + cwd=git_repo, |
| 24 | + check=True, |
| 25 | + capture_output=True, |
| 26 | + ) |
| 27 | + assert git_client.list_uncommitted_changed_files() == [] |
| 28 | + |
| 29 | + # make an unstaged change and see that it is listed |
| 30 | + test_file.write_text("SELECT 2 AS a") |
| 31 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 32 | + assert len(uncommitted) == 1 |
| 33 | + assert uncommitted[0].name == "model.sql" |
| 34 | + |
| 35 | + # stage the change and test that it is still detected |
| 36 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 37 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 38 | + assert len(uncommitted) == 1 |
| 39 | + assert uncommitted[0].name == "model.sql" |
| 40 | + |
| 41 | + |
| 42 | +def test_git_both_staged_and_unstaged_changes(git_repo: Path): |
| 43 | + git_client = GitClient(git_repo) |
| 44 | + |
| 45 | + file1 = git_repo / "model1.sql" |
| 46 | + file2 = git_repo / "model2.sql" |
| 47 | + file1.write_text("SELECT 1") |
| 48 | + file2.write_text("SELECT 2") |
| 49 | + subprocess.run(["git", "add", "."], cwd=git_repo, check=True, capture_output=True) |
| 50 | + subprocess.run( |
| 51 | + ["git", "commit", "-m", "onitial commit"], |
| 52 | + cwd=git_repo, |
| 53 | + check=True, |
| 54 | + capture_output=True, |
| 55 | + ) |
| 56 | + |
| 57 | + # stage file1 |
| 58 | + file1.write_text("SELECT 10") |
| 59 | + subprocess.run(["git", "add", "model1.sql"], cwd=git_repo, check=True, capture_output=True) |
| 60 | + |
| 61 | + # mdify file2 but don't stage it! |
| 62 | + file2.write_text("SELECT 20") |
| 63 | + |
| 64 | + # both should be detected |
| 65 | + uncommitted = git_client.list_uncommitted_changed_files() |
| 66 | + assert len(uncommitted) == 2 |
| 67 | + file_names = {f.name for f in uncommitted} |
| 68 | + assert file_names == {"model1.sql", "model2.sql"} |
| 69 | + |
| 70 | + |
| 71 | +def test_git_untracked_files(git_repo: Path): |
| 72 | + git_client = GitClient(git_repo) |
| 73 | + initial_file = git_repo / "initial.sql" |
| 74 | + initial_file.write_text("SELECT 0") |
| 75 | + subprocess.run(["git", "add", "initial.sql"], cwd=git_repo, check=True, capture_output=True) |
| 76 | + subprocess.run( |
| 77 | + ["git", "commit", "-m", "onitial commit"], |
| 78 | + cwd=git_repo, |
| 79 | + check=True, |
| 80 | + capture_output=True, |
| 81 | + ) |
| 82 | + |
| 83 | + new_file = git_repo / "new_model.sql" |
| 84 | + new_file.write_text("SELECT 1") |
| 85 | + |
| 86 | + # untracked file should not appear in uncommitted changes |
| 87 | + assert git_client.list_uncommitted_changed_files() == [] |
| 88 | + |
| 89 | + # but in untracked |
| 90 | + untracked = git_client.list_untracked_files() |
| 91 | + assert len(untracked) == 1 |
| 92 | + assert untracked[0].name == "new_model.sql" |
| 93 | + |
| 94 | + |
| 95 | +def test_git_committed_changes(git_repo: Path): |
| 96 | + git_client = GitClient(git_repo) |
| 97 | + |
| 98 | + test_file = git_repo / "model.sql" |
| 99 | + test_file.write_text("SELECT 1") |
| 100 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 101 | + subprocess.run( |
| 102 | + ["git", "commit", "-m", "onitial commit"], |
| 103 | + cwd=git_repo, |
| 104 | + check=True, |
| 105 | + capture_output=True, |
| 106 | + ) |
| 107 | + |
| 108 | + result = subprocess.run( |
| 109 | + ["git", "branch", "--show-current"], |
| 110 | + cwd=git_repo, |
| 111 | + check=True, |
| 112 | + capture_output=True, |
| 113 | + text=True, |
| 114 | + ) |
| 115 | + default_branch = result.stdout.strip() |
| 116 | + |
| 117 | + subprocess.run( |
| 118 | + ["git", "checkout", "-b", "feature"], |
| 119 | + cwd=git_repo, |
| 120 | + check=True, |
| 121 | + capture_output=True, |
| 122 | + ) |
| 123 | + |
| 124 | + test_file.write_text("SELECT 2") |
| 125 | + subprocess.run(["git", "add", "model.sql"], cwd=git_repo, check=True, capture_output=True) |
| 126 | + subprocess.run( |
| 127 | + ["git", "commit", "-m", "an update on feature bramch"], |
| 128 | + cwd=git_repo, |
| 129 | + check=True, |
| 130 | + capture_output=True, |
| 131 | + ) |
| 132 | + |
| 133 | + committed = git_client.list_committed_changed_files(target_branch=default_branch) |
| 134 | + assert len(committed) == 1 |
| 135 | + assert committed[0].name == "model.sql" |
| 136 | + |
| 137 | + assert git_client.list_uncommitted_changed_files() == [] |
0 commit comments