| 1 | tmpdir creation fails when the username contains illegal characters for directory names |
| 2 | `tmpdir`, `tmpdir_factory` and `tmp_path_factory` rely on `getpass.getuser()` for determining the `basetemp` directory. I found that the user name returned by `getpass.getuser()` may return characters that are not allowed for directory names. This may lead to errors while creating the temporary directory. |
| 3 | |
| 4 | The situation in which I reproduced this issue was while being logged in through an ssh connection into my Windows 10 x64 Enterprise version (1909) using an OpenSSH_for_Windows_7.7p1 server. In this configuration the command `python -c "import getpass; print(getpass.getuser())"` returns my domain username e.g. `contoso\john_doe` instead of `john_doe` as when logged in regularly using a local session. |
| 5 | |
| 6 | When trying to create a temp directory in pytest through e.g. `tmpdir_factory.mktemp('foobar')` this fails with the following error message: |
| 7 | ``` |
| 8 | self = WindowsPath('C:/Users/john_doe/AppData/Local/Temp/pytest-of-contoso/john_doe') |
| 9 | mode = 511, parents = False, exist_ok = True |
| 10 | |
| 11 | def mkdir(self, mode=0o777, parents=False, exist_ok=False): |
| 12 | """ |
| 13 | Create a new directory at this given path. |
| 14 | """ |
| 15 | if self._closed: |
| 16 | self._raise_closed() |
| 17 | try: |
| 18 | > self._accessor.mkdir(self, mode) |
| 19 | E FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\john_doe\\AppData\\Local\\Temp\\pytest-of-contoso\\john_doe' |
| 20 | |
| 21 | C:\Python38\lib\pathlib.py:1266: FileNotFoundError |
| 22 | ``` |
| 23 | |
| 24 | I could also reproduce this without the complicated ssh/windows setup with pytest 6.2.2 using the following commands from a `cmd`: |
| 25 | ```bat |
| 26 | echo def test_tmpdir(tmpdir):>test_tmp.py |
| 27 | echo pass>>test_tmp.py |
| 28 | set LOGNAME=contoso\john_doe |
| 29 | py.test test_tmp.py |
| 30 | ``` |
| 31 | |
| 32 | Thanks for having a look at this! |