Skip to content

Commit

Permalink
refactor get_version out of a the Version command
Browse files Browse the repository at this point in the history
The version should generally be set, not just when executing the version
command from the CLI. This commit refactors the get_version command into
system.util; and updates the corresponding tests.

This also adds additional test coverage for other minor commands.
  • Loading branch information
ephur committed May 25, 2024
1 parent 0f87a5d commit e2f2a32
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
14 changes: 4 additions & 10 deletions tests/commands/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
43 changes: 42 additions & 1 deletion tests/util/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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"
4 changes: 2 additions & 2 deletions tfworker/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions tfworker/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
11 changes: 11 additions & 0 deletions tfworker/util/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import subprocess

import click
from pkg_resources import DistributionNotFound, get_distribution


def strip_ansi(line):
Expand Down Expand Up @@ -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"

0 comments on commit e2f2a32

Please sign in to comment.