Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinse committed Sep 11, 2024
1 parent 5695f35 commit 17b1fa4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packaging/filereplacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def convert(infile, outfile):
out += os.environ[name]

else:
raise Exception(f"The variable {name} is not defined")
raise KeyError(f"The variable {name} is not defined")

if out:
with open(outfile, 'w', encoding="utf-8") as fout:
fout.write(out)
with open(outfile, 'w', encoding="utf-8") as fout:
fout.write(out)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectKey=Laerdal_python-objdictgen
sonar.organization=laerdal-foss
sonar.python.coverage.reportPaths=coverage.xml
sonar.exclusions = tests/**
sonar.exclusions = tests/**, packaging/**
38 changes: 38 additions & 0 deletions tests/test_installer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import pytest
from unittest import mock
import os
import sys

@pytest.fixture
def setenvvar(monkeypatch):
with mock.patch.dict(os.environ, {"TEST": "foobar"}):
yield


def test_filereplacer(basepath, wd, setenvvar):

sys.path.append(str(basepath / "packaging"))
os.chdir(basepath)
from filereplacer import convert

tests = [
(1, "Test data", "Test data"),
(2, "@@{name}", "objdictgen"),
(3, "@@{TEST}", "foobar"), # Read from the mocked environment variable
(4, "@@{nonexisting}", "non-existing"),
]

for i, data, result in tests:
infile = wd / "test.txt"
outfile = wd / "out.txt"
with open(infile, "w", encoding="utf-8") as f:
f.write(data)
if i == 4:
with pytest.raises(KeyError):
convert(infile, outfile)
continue
else:
convert(infile, outfile)
with open(outfile, "r", encoding="utf-8") as f:
assert f.read() == result

0 comments on commit 17b1fa4

Please sign in to comment.