diff --git a/tests/commands/test_version.py b/tests/commands/test_version.py index 2ba195e..25bae6f 100644 --- a/tests/commands/test_version.py +++ b/tests/commands/test_version.py @@ -25,14 +25,8 @@ def mock_get_distribution(package: str): class TestVersionCommand: def test_exec(self, capsys): - VersionCommand().exec() + vc = VersionCommand() + vc._version = "1.2.3" + vc.exec() text = capsys.readouterr() - assert text.out.startswith("terraform-worker version") - - with mock.patch( - "tfworker.commands.version.get_distribution", - side_effect=mock_get_distribution, - ): - VersionCommand().exec() - text = capsys.readouterr() - assert text.out == "terraform-worker version unknown\n" + assert text.out == "terraform-worker version 1.2.3\n" diff --git a/tests/util/test_system.py b/tests/util/test_system.py index 07cea2e..f3d061f 100644 --- a/tests/util/test_system.py +++ b/tests/util/test_system.py @@ -17,7 +17,7 @@ import pytest -from tfworker.util.system import pipe_exec, which +from tfworker.util.system import pipe_exec, which, get_version, strip_ansi # context manager to allow testing exceptions in parameterized tests @@ -33,6 +33,10 @@ def mock_pipe_exec(args, stdin=None, cwd=None, env=None): def mock_tf_version(args: str): return (0, args.encode(), "".encode()) +def mock_distribution(*args, **kwargs): + Class = mock.MagicMock() + Class.version = "1.2.3" + return Class class TestUtilSystem: @pytest.mark.parametrize( @@ -109,3 +113,40 @@ def test_which(self): ): with mock.patch("os.access", side_effect=lambda x, y: True): assert which("terraform") is not None + + def test_which_full_path(self): + with mock.patch( + "os.path.isfile", + side_effect=lambda x: True, + ): + with mock.patch("os.access", side_effect=lambda x, y: True): + assert which("/full/path/to/file") is not None + + def test_which_not_found(self): + with mock.patch( + "os.path.isfile", + side_effect=lambda x: False, + ): + with mock.patch("os.access", side_effect=lambda x, y: False): + assert which("terraform") is None + + def test_get_version(self): + with mock.patch( + "tfworker.util.system.get_distribution", + side_effect=mock_distribution, + ): + assert get_version() == "1.2.3" + + def test_get_version_unknown(self): + from pkg_resources import DistributionNotFound + with mock.patch( + "tfworker.util.system.get_distribution", + side_effect=DistributionNotFound, + ): + assert get_version() == "unknown" + + def test_strip_ansi(self): + assert strip_ansi("\x1B[31mHello\x1B[0m") == "Hello" + assert strip_ansi("\x1B[32mWorld\x1B[0m") == "World" + assert strip_ansi("\x1B[33mFoo\x1B[0m") == "Foo" + assert strip_ansi("\x1B[34mBar\x1B[0m") == "Bar" diff --git a/tfworker/commands/base.py b/tfworker/commands/base.py index a06765e..1bd024c 100644 --- a/tfworker/commands/base.py +++ b/tfworker/commands/base.py @@ -23,7 +23,7 @@ from tfworker.handlers.exceptions import HandlerError, UnknownHandler from tfworker.plugins import PluginsCollection from tfworker.providers import ProvidersCollection -from tfworker.util.system import pipe_exec, which +from tfworker.util.system import pipe_exec, which, get_version class MissingDependencyException(Exception): @@ -36,7 +36,7 @@ def __init__(self, rootc, deployment="undefined", limit=tuple(), **kwargs): self._args_dict = dict(kwargs) self._args_dict.update(self._rootc.args.__dict__) - self._version = None + self._version = get_version() self._providers = None self._definitions = None self._backend = None diff --git a/tfworker/commands/version.py b/tfworker/commands/version.py index 7c35e59..e8c1c52 100644 --- a/tfworker/commands/version.py +++ b/tfworker/commands/version.py @@ -11,19 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -from pkg_resources import DistributionNotFound, get_distribution - from tfworker.commands.base import BaseCommand +from tfworker.util.system import get_version class VersionCommand(BaseCommand): def __init__(self): - try: - pkg_info = get_distribution("terraform-worker") - self._version = pkg_info.version - except DistributionNotFound: - self._version = "unknown" + self._version = get_version() def exec(self): print(f"terraform-worker version {self._version}") diff --git a/tfworker/util/system.py b/tfworker/util/system.py index c48596b..8e9ca9a 100644 --- a/tfworker/util/system.py +++ b/tfworker/util/system.py @@ -17,6 +17,7 @@ import subprocess import click +from pkg_resources import DistributionNotFound, get_distribution def strip_ansi(line): @@ -147,3 +148,13 @@ def is_exe(fpath): if is_exe(exe_file): return exe_file return None + +def get_version() -> str: + """ + Get the version of the current package + """ + try: + pkg_info = get_distribution("terraform-worker") + return pkg_info.version + except DistributionNotFound: + return "unknown"