Merge pull request #102 from pre-commit/improve_error_message_aws_101

Improve error message when credentials file is not provided
This commit is contained in:
Anthony Sottile 2016-02-08 17:31:33 -08:00
commit 36214cb67b
2 changed files with 24 additions and 14 deletions

View file

@ -8,8 +8,8 @@ from six.moves import configparser # pylint: disable=import-error
def get_your_keys(credentials_file):
""" reads the secret keys in your credentials file in order to be able to look
for them in the submitted code.
"""reads the secret keys in your credentials file in order to be able to
look for them in the submitted code.
"""
aws_credentials_file_path = os.path.expanduser(credentials_file)
if not os.path.exists(aws_credentials_file_path):
@ -41,14 +41,22 @@ def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to run')
parser.add_argument(
"--credentials-file",
'--credentials-file',
default='~/.aws/credentials',
help="location of aws credentials file from which to get the secret "
"keys we're looking for",
help=(
'location of aws credentials file from which to get the secret '
"keys we're looking for"
),
)
args = parser.parse_args(argv)
keys = get_your_keys(args.credentials_file)
if not keys:
print(
'No aws keys were configured at {0}\n'
'Configure them with --credentials-file'.format(
args.credentials_file,
),
)
return 2
bad_filenames = check_file_for_aws_keys(args.filenames, keys)

View file

@ -12,10 +12,6 @@ TESTS = (
('ok_json.json', 0),
)
NO_CREDENTIALS_TEST = (
('with_secrets.txt', 2),
)
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
def test_detect_aws_credentials(filename, expected_retval):
@ -26,10 +22,16 @@ def test_detect_aws_credentials(filename, expected_retval):
assert ret == expected_retval
@pytest.mark.parametrize(('filename', 'expected_retval'), NO_CREDENTIALS_TEST)
def test_non_existent_credentials(filename, expected_retval):
def test_non_existent_credentials(capsys):
# with a non-existent credentials file
ret = main(
[get_resource_path(filename), "--credentials-file=testing/resources/credentailsfilethatdoesntexist"]
ret = main((
get_resource_path('with_secrets.txt'),
"--credentials-file=testing/resources/credentailsfilethatdoesntexist"
))
assert ret == 2
out, _ = capsys.readouterr()
assert out == (
'No aws keys were configured at '
'testing/resources/credentailsfilethatdoesntexist\n'
'Configure them with --credentials-file\n'
)
assert ret == expected_retval