fix: use a temp_chdir context manager instead of os.chdir
- os.chdir leaves the cwd after each test, the temp_chdir context
manager fixes that.
- Also, implement context manager following the example in
https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
more carefully, releasing the resources in a finally: block in each
case.
- And disabled the chmod test on Windows since chmod doesn't work on
Windows.