Skip to content

Commit

Permalink
fix: ensure SI_TEST_USER stays the same across fixtures and includes
Browse files Browse the repository at this point in the history
  • Loading branch information
luto committed Dec 14, 2024
1 parent b84f966 commit 344d967
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/shellinspector/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def include_file(

try:
with open(include_path) as f:
return parse(include_path, f)
return parse(include_path, f, specfile.environment.get("SI_TEST_USER"))
except FileNotFoundError:
continue

Expand Down Expand Up @@ -290,7 +290,11 @@ def parse_global_config(
return {}, None


def parse(path: typing.Union[str, Path], stream: typing.IO) -> Specfile:
def parse(
path: typing.Union[str, Path],
stream: typing.IO,
si_test_user_name=None,
) -> Specfile:
path = Path(path)
specfile = Specfile(path)

Expand Down Expand Up @@ -334,6 +338,11 @@ def parse(path: typing.Union[str, Path], stream: typing.IO) -> Specfile:

setattr(specfile.settings, key.name, value)

if not si_test_user_name:
si_test_user_name = f"t{math.floor(random.uniform(0, 9999999)):>07}"

specfile.environment.setdefault("SI_TEST_USER", si_test_user_name)

if specfile.fixture:
specfile.fixture_specfile_pre = include_file(
specfile,
Expand All @@ -343,9 +352,6 @@ def parse(path: typing.Union[str, Path], stream: typing.IO) -> Specfile:
Path(f"{specfile.fixture}_pre.ispec"),
)

random_username = f"t{math.floor(random.uniform(0, 9999999)):>07}"
specfile.environment.setdefault("SI_TEST_USER", random_username)

parse_commands(specfile, commands)

if specfile.fixture:
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/900_fixtures.ispec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ settings:
fixture_dirs:
- fixtures
---
% test "$FIXTURE_SI_TEST_USER" = "$SI_TEST_USER"
% echo $USRNAME
create_user
%~ id $USRNAME
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/fixtures/create_user_pre.ispec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
% export USRNAME=create_user
% adduser -D $USRNAME
% export FIXTURE_SI_TEST_USER=$SI_TEST_USER
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,6 @@ def test_fixture():
assert specfile.fixture == "e2e/fixtures/create_user"
assert not specfile.errors, specfile.errors
assert specfile.fixture_specfile_pre
assert len(specfile.fixture_specfile_pre.commands) == 2
assert len(specfile.fixture_specfile_pre.commands) == 3
assert specfile.fixture_specfile_post
assert len(specfile.fixture_specfile_post.commands) == 1

0 comments on commit 344d967

Please sign in to comment.