From 5751b2a8ffbc0f504c48dd8ab4a46b520e1b51b6 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sun, 16 Jul 2023 00:58:50 +0300 Subject: [PATCH] Make it easier to add new envfile tests We have 2 tests using the valid yaml. Both test need to create fake addons directories in a temporary directory. This makes it harder than needed to create new tests. Change the valid_yaml to pytest fixture returning an Env namedtuple with an open file object with the valid yaml and the path of the addons directory created by the fixture. With this creating new test requires one line to create an env. Signed-off-by: Nir Soffer --- test/drenv/envfile_test.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/test/drenv/envfile_test.py b/test/drenv/envfile_test.py index de957e8ee..7d3b76a80 100644 --- a/test/drenv/envfile_test.py +++ b/test/drenv/envfile_test.py @@ -3,9 +3,16 @@ import io import pytest +from collections import namedtuple + from . import envfile -valid_yaml = """ +Env = namedtuple("Env", "file,addons_root") + + +@pytest.fixture +def valid_env(tmpdir): + yaml = """ name: test templates: @@ -57,17 +64,14 @@ args: [] """ - -def create_addons(tmpdir): for i in range(1, 7): tmpdir.mkdir(f"addon{i}") + return Env(file=io.StringIO(yaml), addons_root=str(tmpdir)) -def test_valid(tmpdir): - create_addons(tmpdir) - f = io.StringIO(valid_yaml) - env = envfile.load(f, addons_root=str(tmpdir)) +def test_valid(valid_env): + env = envfile.load(valid_env.file, addons_root=valid_env.addons_root) # profile dr1 @@ -122,10 +126,12 @@ def test_valid(tmpdir): assert worker["addons"][0]["args"] == [] -def test_name_prefix(tmpdir): - create_addons(tmpdir) - f = io.StringIO(valid_yaml) - env = envfile.load(f, name_prefix="prefix-", addons_root=str(tmpdir)) +def test_name_prefix(valid_env): + env = envfile.load( + valid_env.file, + name_prefix="prefix-", + addons_root=valid_env.addons_root, + ) # env