I have a function that updates a rpm spec file for later on use. it has three parameters: the name of the application, the version we want to bump to, and where the spec file lives in the filesystem.
What I can't seem to do is have, say, a tests/files/package_name.spec file and test that it versions it correctly without changing the actual spec file. I don't want to change the actual spec file because it would make the test useless the second time it gets ran.
I have tried looking into the mock library and mock_open but nothing seems to fit this case.
def test_update_spec_file():
update_spec_file("package_name", "5.0.0", "files/package_name")
with open("files/package_name.spec", "r") as f:
contents = f.read()
version_regex = re.compile("^.*define _software_version.*$")
assert (
re.search(version_regex, contents).group(0)
== "%define _software_version 5.0.0"
)
Essentially what the update_spec_file
function does is find the %define _software_version 5.0.0
line using regex and use regex again to sub in a new version.
So in conclusion, would it be possible to have the file exist, open it with some sort of mock file with its contents, run the function on that, and assert it did its job without actually modifying the file in the file system?
Either that or reset the file after its modified.
Aucun commentaire:
Enregistrer un commentaire