def test_command_injection_exit(fixture_tmpdir): """test that subprocess.run isn't raising an exception due to exit 1""" mocked_self = mock.MagicMock() mocked_self._gitcmd = Gitget._gitcmd mocked_self._repodir = fixture_tmpdir # must not raise an exception Gitget._run_command(mocked_self, ("--version", ";", "exit", "1"))
def test_non_existing_commit(): with pytest.raises(GitgetException) as exception_info: with Gitget(repository="https://github.com/simonkowallik/ihac", commit=40 * "1") as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException
def test_Gitget_tag(): with Gitget( repository="https://github.com/simonkowallik/as3ninjaDemo", branch="tag_v1.0", ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "tag_v1.0"
def test_invalid_commit_id(): with pytest.raises(ValueError) as exception_info: with Gitget(repository="https://github.com/simonkowallik/ihac", commit="1234567") as gitrepo: print(gitrepo.info) assert exception_info.type is ValueError
def test_full_clone(): with Gitget( repository="https://github.com/simonkowallik/as3ninjaDemo", commit="924f79f8569317d01d1be7d6a77ac8e2b88332ff", depth=0, ) as gitrepo: assert isinstance(gitrepo.info, dict)
def test_command_injection_output(fixture_tmpdir): """test that subprocess.run output doesn't contain output from injected command""" mocked_self = mock.MagicMock() mocked_self._gitcmd = Gitget._gitcmd mocked_self._repodir = fixture_tmpdir output = Gitget._run_command(mocked_self, ("--version", ";", "pwd")) assert mocked_self._repodir not in output
def test_Gitget_depth0(): with Gitget( repository="https://github.com/simonkowallik/as3ninjaDemo", branch="master", depth=0, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master"
def test_non_existing_repository(): """test a non-existing repository""" with pytest.raises(GitgetException) as exception_info: with Gitget( repository="https://github.com/repository-does-not-exist" ) as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException
def test_TimeoutExpired(): """test TimeoutExpired is raised when an operation takes too long.""" with pytest.raises(GitgetException) as exception_info: with Gitget( repository="https://github.com/python/cpython") as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException assert "TimeoutExpired" in str(exception_info.value)
def test_commit_missing_in_clone(): with pytest.raises(GitgetException) as exception_info: with Gitget( repository="https://github.com/simonkowallik/as3ninjaDemo", commit="924f79f8569317d01d1be7d6a77ac8e2b88332ff", depth=3, ) as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException
def test_Gitget_depth_negative(): with pytest.raises(ValueError) as exception_info: with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", depth=-1, ) as gitrepo: print(gitrepo.info) assert exception_info.type is ValueError
def test_custom_repodir(fixture_tmpdir): repodir = fixture_tmpdir with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", repodir=repodir, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master" assert Path(repodir).exists()
def test_custom_repodir_2ndclone_force(fixture_tmpdir): repodir = fixture_tmpdir # first clone with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", repodir=repodir, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master" # second clone to existing repo with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", repodir=repodir, force=True, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master"
def test_non_existing_repository(): # TODO: this prompts for user+password. requires credential handling with pytest.raises(GitgetException) as exception_info: with Gitget( repository= "https://github.com/simonkowallik/does-not-exist", branch="doesnt-exist", ) as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException
def test_custom_repodir_2ndclone_noforce(fixture_tmpdir): repodir = fixture_tmpdir # first clone with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", repodir=repodir, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master" # second clone to existing repo without force raises exception with pytest.raises(GitgetException) as exception_info: with Gitget( repository="https://github.com/simonkowallik/ihac", branch="master", repodir=repodir, ) as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException
def test_rmrepodir(fixture_tmpdir): repodir = fixture_tmpdir with Gitget( repository="https://github.com/simonkowallik/as3ninjaDemo", branch="master", repodir=repodir, ) as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master" gitrepo.rmrepodir() assert Path(repodir).exists() is False
def test_private_repository(): """test a private repository requiring authentication. This test will prompt for credentials if not authenticated and will run into a timeout. """ with pytest.raises(GitgetException) as exception_info: with Gitget( repository= "https://github.com/simonkowallik/some-private-repository" ) as gitrepo: print(gitrepo.info) assert exception_info.type is GitgetException assert "CalledProcessError" in str(exception_info.value)
def test_Gitget_tag(): with Gitget(repository="https://github.com/simonkowallik/ihac", branch="2.0") as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "2.0"
def test_Gitget_repo_only(): with Gitget( repository="https://github.com/simonkowallik/ihac") as gitrepo: assert isinstance(gitrepo.info, dict) assert gitrepo.info["branch"] == "master"
def test_sh_quote__command_injection(): teststring = "'; ls /; echo" result = Gitget._sh_quote(teststring) assert result == "''\"'\"'; ls /; echo'"
def test_sh_quote__simple_quote(): teststring = "a b c" result = Gitget._sh_quote(teststring) assert result == f"'{teststring}'"
def test_sh_quote__integer(): teststring = 1234 result = Gitget._sh_quote(teststring) assert result == f"{teststring}"
def test_sh_quote__no_quote(): teststring = "abc" result = Gitget._sh_quote(teststring) assert result == f"{teststring}"
def test_HEAD_invalid(): teststring = "HEAD~ invalid" result = Gitget._sh_quote(teststring) assert result == f"'{teststring}'"
def test_dir_exists_on_with(): with Gitget( repository="https://github.com/simonkowallik/ihac") as gitrepo: assert Path(gitrepo.repodir + "/.git").exists()
def test_datetime_format_string(): result = Gitget._datetime_format("1234567890") assert result == "2009-02-13T23:31:30Z"
def test_HEAD_no_int(): teststring = "HEAD~no_int" result = Gitget._sh_quote(teststring) assert result == f"'{teststring}'"
def test_datetime_format_int(): result = Gitget._datetime_format(1234567890) assert result == "2009-02-13T23:31:30Z"
def test_datetime_format_returns_string(): result = Gitget._datetime_format("1234567890") assert isinstance(result, str)
def test_shell_false(mocker): """test that subprocess.run is using shell=False""" mocked_run = mocker.patch("as3ninja.gitget.run") Gitget._run_command(mock.MagicMock(), ("ls")) assert "shell=False" in str(mocked_run.call_args)