File tree Expand file tree Collapse file tree 2 files changed +84
-0
lines changed
Expand file tree Collapse file tree 2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ import subprocess
2+ from functools import wraps
3+ from pathlib import Path
4+
5+
6+ def run_command (func ):
7+ @wraps (func )
8+ def wrapper (* args , ** kwargs ) -> str :
9+ command_list = func (* args , ** kwargs ).split ()
10+ output = subprocess .run (
11+ command_list , capture_output = True , text = True , check = True
12+ )
13+ return output .stdout .strip ()
14+
15+ return wrapper
16+
17+
18+ class Git :
19+ @staticmethod
20+ @run_command
21+ def get_latest_tag () -> str :
22+ """
23+ Get the latest tag from the git repository.
24+ """
25+ return "git describe --tags --abbrev=0"
26+
27+ @staticmethod
28+ @run_command
29+ def read_file_from_tag (tag : str , remote_file : str ) -> str :
30+ """
31+ Read the contents of the specified file `remote_file` at the point in time
32+ specified by git tag `tag`.
33+ """
34+ return f"git cat-file blob { tag } :{ remote_file } "
35+
36+ @staticmethod
37+ def copy_remote_file_locally (
38+ tag : str , remote_file : str , destination_directory : Path
39+ ) -> None :
40+ """
41+ Copy the contents of the specified file `remote_file` at the point in time
42+ specified by git tag `tag` and copy it into the local `destination_directory/remote_file`.
43+ """
44+ contents = Git .read_file_from_tag (tag = tag , remote_file = remote_file )
45+ destination_filepath = destination_directory / remote_file
46+ destination_filepath .write_text (contents )
Original file line number Diff line number Diff line change 1+ import pytest
2+ from packaging .version import Version
3+ from toolbox .util .git import Git
4+
5+ POETRY_LOCK = "poetry.lock"
6+
7+
8+ @pytest .fixture (scope = "module" )
9+ def latest_tag () -> str :
10+ return Git .get_latest_tag ()
11+
12+
13+ @pytest .fixture (scope = "module" )
14+ def read_file_from_tag (latest_tag ) -> str :
15+ return Git .read_file_from_tag (tag = latest_tag , remote_file = POETRY_LOCK )
16+
17+
18+ class TestGit :
19+ @staticmethod
20+ def test_latest_tag (latest_tag ):
21+ assert isinstance (latest_tag , str )
22+ assert Version (latest_tag )
23+
24+ @staticmethod
25+ def test_read_file_from_tag (read_file_from_tag ):
26+ assert isinstance (read_file_from_tag , str )
27+ assert read_file_from_tag != ""
28+
29+ @staticmethod
30+ def test_copy_remote_file_locally (tmp_path , read_file_from_tag ):
31+ latest_tag = Git .get_latest_tag ()
32+
33+ Git .copy_remote_file_locally (
34+ tag = latest_tag , remote_file = POETRY_LOCK , destination_directory = tmp_path
35+ )
36+
37+ result = (tmp_path / POETRY_LOCK ).read_text ()
38+ assert result == read_file_from_tag
You can’t perform that action at this time.
0 commit comments