Skip to content

Commit

Permalink
Linting with Ruff
Browse files Browse the repository at this point in the history
Adding basic linting with Ruff.

[#10]

Signed-off-by: Geoffrey Wiseman <geoffrey.wiseman@codiform.com>
  • Loading branch information
geoffreywiseman committed Mar 30, 2023
1 parent 1df5664 commit 666d2dd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: snok/install-poetry@v1

- name: Install Dependencies
run: |
poetry install
run: poetry install

- name: Test
run: |
poetry run pytest
run: poetry run pytest
7 changes: 4 additions & 3 deletions awswl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

def parse_args(args):
parser = argparse.ArgumentParser(
description='Maintains a list of whitelisted CIDR blocks granted SSH access to AWS via a '
'security group.'
description='Maintains a list of whitelisted CIDR blocks granted SSH access to '
'AWS via a security group.'
)
parser.add_argument(
'--list', action='append_const', dest='actions', const='cmd_list',
Expand All @@ -18,7 +18,8 @@ def parse_args(args):
help='Adds the current IP address to the whitelist.'
)
parser.add_argument(
'--remove-current', action='append_const', dest='actions', const='cmd_remove_current',
'--remove-current', action='append_const', dest='actions',
const='cmd_remove_current',
help='Remove the current IP address from the whitelist.'
)
parser.add_argument(
Expand Down
9 changes: 6 additions & 3 deletions awswl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ def has_action(options):
def validate_options(options):
if not options.sgid:
print(
"No security group specified as an argument with --sgid or in the environment "
"as AWSWL_SGID. Cannot proceed.")
"No security group specified as an argument with --sgid or in the "
"environment as AWSWL_SGID. Cannot proceed."
)
return False
elif has_action(options):
print(
"You haven't asked AWSWL to do anything. Try `awswl --help` to get started?.")
"You haven't asked AWSWL to do anything. "
"Try `awswl --help` to get started?"
)
return False
return True
29 changes: 28 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ requests = "^2.28.2"
pytest = "^7.2.2"
moto = "^4.1.6"
mock = "^5.0.1"
ruff = "^0.0.260"

[build-system]
requires = ["poetry-core"]
Expand Down
21 changes: 13 additions & 8 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def test_add_adds_specified_permission(mock_stdout, region, security_group):

@patch('awswl.externalip.get_external_ip', return_value='192.0.2.1')
@patch('sys.stdout', new_callable=io.StringIO)
def test_remove_current_removes_permission(mock_stdout, exip_method, region, security_group):
def test_remove_current_removes_permission(mock_stdout, exip_method, region,
security_group):
security_group.authorize_ingress(IpPermissions=[{
'IpRanges': [
{'CidrIp': '192.0.2.1/32'},
Expand All @@ -160,15 +161,19 @@ def test_remove_current_removes_permission(mock_stdout, exip_method, region, sec

after_group = boto3.resource('ec2').SecurityGroup(security_group.id)
assert not after_group.ip_permissions
assert "Removed current external IP address as a CIDR block" in mock_stdout.getvalue()
assert "Removed current external IP address as a CIDR block" \
in mock_stdout.getvalue()


@patch('awswl.externalip.get_external_ip', return_value='192.0.2.1')
@patch('sys.stdout', new_callable=io.StringIO)
def test_remove_current_indicates_notfound(mock_stdout, exip_method, region, security_group):
def test_remove_current_indicates_notfound(mock_stdout, exip_method, region,
security_group):
opt = options(security_group)
commands.cmd_remove_current(opt)
assert "Current external IP address as a CIDR block does not seem to be whitelisted." in mock_stdout.getvalue()
assert \
"Current external IP address as a CIDR block does not seem to be whitelisted." \
in mock_stdout.getvalue()


@patch('sys.stdout', new_callable=io.StringIO)
Expand All @@ -182,7 +187,7 @@ def test_remove_removes_specified(mock_stdout, region, security_group):
'ToPort': 22
}])
opt = options(security_group)
commands.cmd_remove(opt,'192.0.2.1/32')
commands.cmd_remove(opt, '192.0.2.1/32')

after_group = boto3.resource('ec2').SecurityGroup(security_group.id)
assert not after_group.ip_permissions
Expand All @@ -192,6 +197,6 @@ def test_remove_removes_specified(mock_stdout, region, security_group):
@patch('sys.stdout', new_callable=io.StringIO)
def test_remove_specified_indicates_notfound(mock_stdout, region, security_group):
opt = options(security_group)
commands.cmd_remove(opt,'192.0.2.1/32')
assert "Specified CIDR block does not seem to be whitelisted." in mock_stdout.getvalue()

commands.cmd_remove(opt, '192.0.2.1/32')
assert "Specified CIDR block does not seem to be whitelisted." \
in mock_stdout.getvalue()

0 comments on commit 666d2dd

Please sign in to comment.