From d753a079a1774c927cac09d29be7aa874c31bf48 Mon Sep 17 00:00:00 2001 From: Zach Carrington Date: Thu, 3 Dec 2020 09:35:14 -0600 Subject: [PATCH] Fix all failing unit tests on Windows Fixes #11 Fixes #24 --- src/awscli_login/util.py | 3 ++- src/tests/base.py | 35 ++++++++++++++++++++++++++++++----- src/tests/test_util.py | 6 ++++-- tox.ini | 2 ++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/awscli_login/util.py b/src/awscli_login/util.py index f2f151ec..747950f7 100644 --- a/src/awscli_login/util.py +++ b/src/awscli_login/util.py @@ -166,5 +166,6 @@ def secure_touch(path): path - A path to a file. """ fd = os.open(path, os.O_CREAT | os.O_RDONLY, mode=0o600) - os.fchmod(fd, 0o600) + if hasattr(os, "fchmod"): + os.fchmod(fd, 0o600) os.close(fd) diff --git a/src/tests/base.py b/src/tests/base.py index 9948a7b0..142d7826 100644 --- a/src/tests/base.py +++ b/src/tests/base.py @@ -9,8 +9,6 @@ from typing import Any, Callable, List, Optional # NOQA from unittest.mock import patch -from wurlitzer import pipes - from awscli_login.config import CONFIG_FILE from .util import exec_awscli, isFileChangedBy, isFileTouchedBy, tree @@ -603,9 +601,36 @@ def assertAwsCliReturns(self, *args, stdout='', stderr='', code=0): Raises: AssertionError """ - try: - with pipes() as (out, err): - with self.assertRaises(SystemExit) as e: + t_out, t_err, t_code, cmd = _assertAwsCliReturns(args, calls) + + mesg = "Error: ran '%s', on %s expected output: %s" + self.assertEqual( + t_out, + stdout, + mesg % (cmd, 'stdout', stdout) + ) + self.assertEqual( + t_err, + stderr, + mesg % (cmd, 'stderr', stderr) + ) + self.assertEqual( + t_code, + code, + "Error: '%s' returned %d, expected: %d" % + (cmd, t_code, code) + ) + + +@fork() +def _assertAwsCliReturns(args, calls): + t_code = None + + try: + from wurlitzer import pipes + with pipes() as (out, err): + try: + with patch('builtins.input', return_value='') as mock: exec_awscli(*args) import sys diff --git a/src/tests/test_util.py b/src/tests/test_util.py index 4ac2fbcc..93599c3c 100755 --- a/src/tests/test_util.py +++ b/src/tests/test_util.py @@ -270,7 +270,8 @@ def test_secure_touch_function_creates_file(self): secure_touch(path) self.assertTrue(isfile(path), 'Failed to create file!') - self.assertHasFilePerms(path, owner='rw') + if os.name == 'posix': + self.assertHasFilePerms(path, owner='rw') def test_secure_touch_function_changes_perms(self): """File with perms 0x644 should be 0x600 after secure_touch. """ @@ -280,7 +281,8 @@ def test_secure_touch_function_changes_perms(self): os.close(fd) secure_touch(path) - self.assertHasFilePerms(path, owner='rw') + if os.name == 'posix': + self.assertHasFilePerms(path, owner='rw') if __name__ == '__main__': diff --git a/tox.ini b/tox.ini index 08c0344e..ef1695f3 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,8 @@ envlist = py35,py36,py37,py38,py39 [testenv] +# getpass.getuser fails on Windows if these envs are not passed in +passenv = LOGNAME USER LNAME USERNAME # https://packaging.python.org/guides/index-mirrors-and-caches/#caching-with-pip install_command=python -m pip install --disable-pip-version-check --find-links=cache {opts} {packages} commands = python -m unittest discover -s src {posargs}