Skip to content

Commit

Permalink
Fix all failing unit tests on Windows
Browse files Browse the repository at this point in the history
Fixes #11
Fixes #24
  • Loading branch information
zdc217 authored and ddriddle committed Jan 13, 2021
1 parent da61e5a commit d753a07
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/awscli_login/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 30 additions & 5 deletions src/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand All @@ -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__':
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit d753a07

Please sign in to comment.