|
| 1 | +import logging |
1 | 2 | import os |
2 | 3 | import platform |
3 | 4 | import subprocess |
|
24 | 25 | get_staged_filepaths, |
25 | 26 | git, |
26 | 27 | git_ls_unstaged, |
| 28 | + gitignore, |
27 | 29 | is_git_available, |
28 | 30 | is_git_dir, |
| 31 | + is_gitignored, |
29 | 32 | is_valid_git_commit_ref, |
30 | 33 | simplify_git_url, |
31 | 34 | ) |
@@ -680,3 +683,87 @@ def test_git_command_includes_longpaths_on_windows(mock_run): |
680 | 683 | assert ( |
681 | 684 | not longpaths_included |
682 | 685 | ), f"core.longpaths=true found in command: {command}" |
| 686 | + |
| 687 | + |
| 688 | +def test_check_if_path_is_gitignored(tmp_path): |
| 689 | + # GIVEN a repository |
| 690 | + repo = Repository.create(tmp_path) |
| 691 | + repo.create_commit() |
| 692 | + |
| 693 | + # WHEN checking if the path is ignored |
| 694 | + with cd(str(repo.path)): |
| 695 | + not_ignored = is_gitignored(Path("*.pyc")) |
| 696 | + with open(repo.path / ".gitignore", "w") as f: |
| 697 | + f.write("*.pyc") |
| 698 | + ignored = is_gitignored(Path("*.pyc")) |
| 699 | + |
| 700 | + # THEN the correct value is returned |
| 701 | + assert ignored |
| 702 | + assert not_ignored is False |
| 703 | + |
| 704 | + |
| 705 | +def test_check_if_path_is_gitignored_no_repo(tmp_path): |
| 706 | + # GIVEN a directory that is not a git repository |
| 707 | + # WHEN checking if the path is ignored |
| 708 | + with cd(str(tmp_path)): |
| 709 | + no_git = is_gitignored(Path("*.pyc")) |
| 710 | + |
| 711 | + # THEN None is returned |
| 712 | + assert no_git is None |
| 713 | + |
| 714 | + |
| 715 | +def test_gitignore(tmp_path): |
| 716 | + # GIVEN a repository |
| 717 | + repo = Repository.create(tmp_path) |
| 718 | + repo.create_commit() |
| 719 | + |
| 720 | + # WHEN adding a path to the gitignore |
| 721 | + with cd(str(repo.path)): |
| 722 | + gitignore(Path("*.pyc")) |
| 723 | + |
| 724 | + # THEN the path is added to the gitignore file |
| 725 | + gitignore_content = (repo.path / ".gitignore").read_text() |
| 726 | + assert "\n*.pyc\n" in gitignore_content |
| 727 | + |
| 728 | + |
| 729 | +def test_gitignore_with_existing_gitignore(tmp_path): |
| 730 | + # GIVEN a repository with an existing gitignore file |
| 731 | + repo = Repository.create(tmp_path) |
| 732 | + repo.create_commit() |
| 733 | + with open(tmp_path / ".gitignore", "w") as f: |
| 734 | + f.write("node_modules/") |
| 735 | + |
| 736 | + # WHEN adding a path to the gitignore |
| 737 | + with cd(str(repo.path)): |
| 738 | + gitignore(Path("*.pyc")) |
| 739 | + |
| 740 | + # THEN the path is added to the gitignore file |
| 741 | + gitignore_content = (repo.path / ".gitignore").read_text() |
| 742 | + assert "\n*.pyc\n" in gitignore_content |
| 743 | + |
| 744 | + |
| 745 | +def test_gitignore_in_non_git_directory(tmp_path): |
| 746 | + # GIVEN a non-git directory |
| 747 | + # WHEN adding a path to the gitignore |
| 748 | + with cd(str(tmp_path)): |
| 749 | + gitignore(Path("*.pyc")) |
| 750 | + |
| 751 | + # THEN the path is not added to the gitignore file |
| 752 | + assert not (tmp_path / ".gitignore").exists() |
| 753 | + |
| 754 | + |
| 755 | +def test_gitignore_write_error(caplog, tmp_path): |
| 756 | + # GIVEN a repository with a gitignore file that cannot be written |
| 757 | + repo = Repository.create(tmp_path) |
| 758 | + repo.create_commit() |
| 759 | + with open(tmp_path / ".gitignore", "w") as f: |
| 760 | + f.write("node_modules/") |
| 761 | + (tmp_path / ".gitignore").chmod(0o000) |
| 762 | + |
| 763 | + with caplog.at_level(logging.DEBUG): |
| 764 | + # WHEN adding a path to the gitignore |
| 765 | + with cd(str(repo.path)): |
| 766 | + gitignore(Path("*.pyc")) |
| 767 | + |
| 768 | + # THEN the error is logged |
| 769 | + assert "Failed to add *.pyc to .gitignore in" in caplog.text |
0 commit comments