Bug 8334 - Newer python unittest-module breaks with hyphen in script names
Summary: Newer python unittest-module breaks with hyphen in script names
Status: NEW
Alias: None
Product: ThinLinc
Classification: Unclassified
Component: Other (show other bugs)
Version: trunk
Hardware: PC Unknown
: P2 Normal
Target Milestone: 4.17.0
Assignee: Alexander Zeijlon
URL:
Keywords: prosaic
Depends on:
Blocks:
 
Reported: 2024-04-16 15:48 CEST by Alexander Zeijlon
Modified: 2024-04-16 16:45 CEST (History)
0 users

See Also:
Acceptance Criteria:
MUST: * Unittests should run, without errors, on both newer (3.12) and older (3.6) version of Python.


Attachments

Description Alexander Zeijlon cendio 2024-04-16 15:48:55 CEST
Some of our test cases break when run locally with Python >= 3.11 [1], specifically tests for our scripts.

[1] To our knowlege, did not test further back.

The root cause for this is that we are importing scripts with "-", and imports in python may not have "-" in their names.

We believe that the unittest-module in older Python versions (e.g. 3.6) let this pass.
Comment 1 Alexander Zeijlon cendio 2024-04-16 16:40:51 CEST
We need to replace "-" with e.g. "_", but not everywhere.

Lines like these need to remain consistent with the file hyphenated filename we are importing and testing ("tl-script-name" as an example):
> fn = os.path.join(testdir, "..", "scripts", "tl-script-name")
> loader = SourceFileLoader("tl-script-name", fn)
> spec = importlib.util.spec_from_loader("tl-script-name", loader)
> script = importlib.util.module_from_spec(spec)
> spec.loader.exec_module(script)
But all other instances of "tl-script-name" need to be replaced with "tl_script_name":
> sys.modules["tl_mount_localdrives"] = script
> [...]
> with patch("tl_script_name.SomeClass", MagicMock()) as some_mock:
>     script.main()
Comment 2 Alexander Zeijlon cendio 2024-04-16 16:45:22 CEST
There are also some instances of
> @patch("tl-script-name.open", ... )
where we could simply change the call to
> @patch("builtins.open", ... )
This would make it clearer that we are in fact mocking a built-in function.

Note You need to log in before you can comment on or make changes to this bug.