mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
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:
commit
36214cb67b
2 changed files with 24 additions and 14 deletions
|
|
@ -8,8 +8,8 @@ from six.moves import configparser # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
def get_your_keys(credentials_file):
|
def get_your_keys(credentials_file):
|
||||||
""" reads the secret keys in your credentials file in order to be able to look
|
"""reads the secret keys in your credentials file in order to be able to
|
||||||
for them in the submitted code.
|
look for them in the submitted code.
|
||||||
"""
|
"""
|
||||||
aws_credentials_file_path = os.path.expanduser(credentials_file)
|
aws_credentials_file_path = os.path.expanduser(credentials_file)
|
||||||
if not os.path.exists(aws_credentials_file_path):
|
if not os.path.exists(aws_credentials_file_path):
|
||||||
|
|
@ -41,14 +41,22 @@ def main(argv=None):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--credentials-file",
|
'--credentials-file',
|
||||||
default='~/.aws/credentials',
|
default='~/.aws/credentials',
|
||||||
help="location of aws credentials file from which to get the secret "
|
help=(
|
||||||
"keys we're looking for",
|
'location of aws credentials file from which to get the secret '
|
||||||
|
"keys we're looking for"
|
||||||
|
),
|
||||||
)
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
keys = get_your_keys(args.credentials_file)
|
keys = get_your_keys(args.credentials_file)
|
||||||
if not keys:
|
if not keys:
|
||||||
|
print(
|
||||||
|
'No aws keys were configured at {0}\n'
|
||||||
|
'Configure them with --credentials-file'.format(
|
||||||
|
args.credentials_file,
|
||||||
|
),
|
||||||
|
)
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
bad_filenames = check_file_for_aws_keys(args.filenames, keys)
|
bad_filenames = check_file_for_aws_keys(args.filenames, keys)
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,6 @@ TESTS = (
|
||||||
('ok_json.json', 0),
|
('ok_json.json', 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
NO_CREDENTIALS_TEST = (
|
|
||||||
('with_secrets.txt', 2),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
|
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
|
||||||
def test_detect_aws_credentials(filename, expected_retval):
|
def test_detect_aws_credentials(filename, expected_retval):
|
||||||
|
|
@ -26,10 +22,16 @@ def test_detect_aws_credentials(filename, expected_retval):
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'expected_retval'), NO_CREDENTIALS_TEST)
|
def test_non_existent_credentials(capsys):
|
||||||
def test_non_existent_credentials(filename, expected_retval):
|
|
||||||
# with a non-existent credentials file
|
# with a non-existent credentials file
|
||||||
ret = main(
|
ret = main((
|
||||||
[get_resource_path(filename), "--credentials-file=testing/resources/credentailsfilethatdoesntexist"]
|
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
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue