Skip to content

Commit

Permalink
feat: Add black formatter to the project (dragonflydb#1544)
Browse files Browse the repository at this point in the history
Add black formatter and run it on pytests
  • Loading branch information
kostasrim authored Jul 17, 2023
1 parent 9448220 commit 7944af3
Show file tree
Hide file tree
Showing 21 changed files with 796 additions and 569 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ repos:
hooks:
- id: clang-format
name: Clang formatting

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.black]
line-length = 100
include = '\.py$'
extend-exclude = '''
/(
| .git
| .__pycache__
| build-dbg
| build-opt
| helio
)/
'''
30 changes: 17 additions & 13 deletions tests/dragonfly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, params: DflyParams, args):
self.args = args
self.params = params
self.proc = None
self._client : Optional[RedisClient] = None
self._client: Optional[RedisClient] = None

def client(self) -> RedisClient:
return RedisClient(port=self.port)
Expand Down Expand Up @@ -70,8 +70,7 @@ def _start(self):
return
base_args = [f"--{v}" for v in self.params.args]
all_args = self.format_args(self.args) + base_args
print(
f"Starting instance on {self.port} with arguments {all_args} from {self.params.path}")
print(f"Starting instance on {self.port} with arguments {all_args} from {self.params.path}")

run_cmd = [self.params.path, *all_args]
if self.params.gdb:
Expand All @@ -82,8 +81,7 @@ def _check_status(self):
if not self.params.existing_port:
return_code = self.proc.poll()
if return_code is not None:
raise Exception(
f"Failed to start instance, return code {return_code}")
raise Exception(f"Failed to start instance, return code {return_code}")

def __getitem__(self, k):
return self.args.get(k)
Expand All @@ -93,11 +91,13 @@ def port(self) -> int:
if self.params.existing_port:
return self.params.existing_port
return int(self.args.get("port", "6379"))

@property
def admin_port(self) -> int:
if self.params.existing_admin_port:
return self.params.existing_admin_port
return int(self.args.get("admin_port", "16379"))

@property
def mc_port(self) -> int:
if self.params.existing_mc_port:
Expand All @@ -107,7 +107,7 @@ def mc_port(self) -> int:
@staticmethod
def format_args(args):
out = []
for (k, v) in args.items():
for k, v in args.items():
out.append(f"--{k}")
if v is not None:
out.append(str(v))
Expand All @@ -118,7 +118,10 @@ async def metrics(self):
resp = await session.get(f"http://localhost:{self.port}/metrics")
data = await resp.text()
await session.close()
return {metric_family.name : metric_family for metric_family in text_string_to_metric_families(data)}
return {
metric_family.name: metric_family
for metric_family in text_string_to_metric_families(data)
}


class DflyInstanceFactory:
Expand All @@ -142,7 +145,7 @@ def create(self, **kwargs) -> DflyInstance:
return instance

def start_all(self, instances):
""" Start multiple instances in parallel """
"""Start multiple instances in parallel"""
for instance in instances:
instance._start()

Expand All @@ -162,17 +165,17 @@ def __str__(self):


def dfly_args(*args):
""" Used to define a singular set of arguments for dragonfly test """
"""Used to define a singular set of arguments for dragonfly test"""
return pytest.mark.parametrize("df_factory", args, indirect=True)


def dfly_multi_test_args(*args):
""" Used to define multiple sets of arguments to test multiple dragonfly configurations """
"""Used to define multiple sets of arguments to test multiple dragonfly configurations"""
return pytest.mark.parametrize("df_factory", args, indirect=True)


class PortPicker():
""" A simple port manager to allocate available ports for tests """
class PortPicker:
"""A simple port manager to allocate available ports for tests"""

def __init__(self):
self.next_port = 5555
Expand All @@ -185,5 +188,6 @@ def get_available_port(self):

def is_port_available(self, port):
import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) != 0
return s.connect_ex(("localhost", port)) != 0
Loading

0 comments on commit 7944af3

Please sign in to comment.