Skip to content

Commit 050f713

Browse files
committed
fix(72): Fix style and lint issues.
1 parent 5266172 commit 050f713

10 files changed

+156
-181
lines changed

examples/pkg1/test_mod1.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ def test_sleep1():
77
time.sleep(1)
88

99

10-
@pytest.mark.monitor_skip_test
10+
@pytest.mark.monitor_skip_test()
1111
def test_sleep2():
1212
time.sleep(2)
1313

1414

15-
@pytest.mark.parametrize(
16-
("range_max", "other"), [(10, "10"), (100, "100"), (1000, "1000"), (10000, "10000")]
17-
)
15+
@pytest.mark.parametrize(("range_max", "other"), [(10, "10"), (100, "100"), (1000, "1000"), (10000, "10000")])
1816
def test_heavy(range_max, other):
1917
assert len(["a" * i for i in range(range_max)]) == range_max

examples/pkg4/test_mod_a.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_not_monitored():
1515
b_continue = t_delta < 5
1616

1717

18-
@pytest.mark.monitor_test
18+
@pytest.mark.monitor_test()
1919
def test_force_monitor():
2020
t_a = time.time()
2121
b_continue = True

pytest_monitor/handler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def insert_execution_context(self, exc_context):
7777
exc_context.architecture,
7878
exc_context.system_info,
7979
exc_context.python_info,
80-
exc_context.hash(),
80+
exc_context.compute_hash(),
8181
),
8282
)
8383

pytest_monitor/pytest_monitor.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ def prof():
224224
def pytest_make_parametrize_id(config, val, argname):
225225
if config.option.mtr_want_explicit_ids:
226226
return f"{argname}={val}"
227+
return None
227228

228229

229230
@pytest.hookimpl(hookwrapper=True)
@@ -258,7 +259,7 @@ def pytest_sessionstart(session):
258259

259260

260261
@pytest.fixture(autouse=True, scope="module")
261-
def prf_module_tracer(request):
262+
def _prf_module_tracer(request):
262263
if not PYTEST_MONITORING_ENABLED:
263264
yield
264265
else:
@@ -287,7 +288,7 @@ def prf_module_tracer(request):
287288

288289

289290
@pytest.fixture(autouse=True)
290-
def prf_tracer(request):
291+
def _prf_tracer(request):
291292
if not PYTEST_MONITORING_ENABLED:
292293
yield
293294
else:

pytest_monitor/session.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def process(self):
5050
def get_env_id(self, env):
5151
db, remote = None, None
5252
if self.__db:
53-
row = self.__db.query("SELECT ENV_H FROM EXECUTION_CONTEXTS WHERE ENV_H= ?", (env.hash(),))
53+
row = self.__db.query("SELECT ENV_H FROM EXECUTION_CONTEXTS WHERE ENV_H= ?", (env.compute_hash(),))
5454
db = row[0] if row else None
5555
if self.__remote:
56-
r = requests.get(f"{self.__remote}/contexts/{env.hash()}")
56+
r = requests.get(f"{self.__remote}/contexts/{env.compute_hash()}")
5757
remote = None
5858
if r.status_code == HTTPStatus.OK:
5959
remote = json.loads(r.text)
@@ -92,12 +92,12 @@ def compute_info(self, description, tags):
9292
if self.__remote:
9393
r = requests.post(
9494
f"{self.__remote}/sessions/",
95-
json=dict(
96-
session_h=self.__session,
97-
run_date=run_date,
98-
scm_ref=scm,
99-
description=json.loads(description),
100-
),
95+
json={
96+
"session_h": self.__session,
97+
"run_date": run_date,
98+
"scm_ref": scm,
99+
"description": json.loads(description),
100+
},
101101
)
102102
if r.status_code != HTTPStatus.CREATED:
103103
self.__remote = ""
@@ -109,7 +109,7 @@ def set_environment_info(self, env):
109109
db_id, remote_id = self.__eid
110110
if self.__db and db_id is None:
111111
self.__db.insert_execution_context(env)
112-
db_id = self.__db.query("select ENV_H from EXECUTION_CONTEXTS where ENV_H = ?", (env.hash(),))[0]
112+
db_id = self.__db.query("select ENV_H from EXECUTION_CONTEXTS where ENV_H = ?", (env.compute_hash(),))[0]
113113
if self.__remote and remote_id is None:
114114
# We must postpone that to be run at the end of the pytest session.
115115
r = requests.post(f"{self.__remote}/contexts/", json=env.to_dict())
@@ -170,22 +170,22 @@ def add_test_info(
170170
if self.__remote and self.remote_env_id is not None:
171171
r = requests.post(
172172
f"{self.__remote}/metrics/",
173-
json=dict(
174-
session_h=self.__session,
175-
context_h=self.remote_env_id,
176-
item_start_time=item_start_time,
177-
item_path=item_path,
178-
item=item,
179-
item_variant=item_variant,
180-
item_fs_loc=item_loc,
181-
kind=kind,
182-
component=final_component,
183-
total_time=total_time,
184-
user_time=user_time,
185-
kernel_time=kernel_time,
186-
cpu_usage=cpu_usage,
187-
mem_usage=mem_usage,
188-
),
173+
json={
174+
"session_h": self.__session,
175+
"context_h": self.remote_env_id,
176+
"item_start_time": item_start_time,
177+
"item_path": item_path,
178+
"item": item,
179+
"item_variant": item_variant,
180+
"item_fs_loc": item_loc,
181+
"kind": kind,
182+
"component": final_component,
183+
"total_time": total_time,
184+
"user_time": user_time,
185+
"kernel_time": kernel_time,
186+
"cpu_usage": cpu_usage,
187+
"mem_usage": mem_usage,
188+
},
189189
)
190190
if r.status_code != HTTPStatus.CREATED:
191191
self.__remote = ""

pytest_monitor/sys_utils.py

+44-46
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,43 @@
1111

1212

1313
def collect_ci_info():
14-
d = dict()
1514
# Test for jenkins
16-
if "BUILD_NUMBER" in os.environ:
17-
if "BRANCH_NAME" in os.environ or "JOB_NAME" in os.environ:
18-
br = os.environ["BRANCH_NAME"] if "BRANCH_NAME" in os.environ else os.environ["JOB_NAME"]
19-
d = dict(
20-
pipeline_branch=br,
21-
pipeline_build_no=os.environ["BUILD_NUMBER"],
22-
__ci__="jenkinsci",
23-
)
15+
if "BUILD_NUMBER" in os.environ and ("BRANCH_NAME" in os.environ or "JOB_NAME" in os.environ):
16+
br = os.environ["BRANCH_NAME"] if "BRANCH_NAME" in os.environ else os.environ["JOB_NAME"]
17+
return {
18+
"pipeline_branch": br,
19+
"pipeline_build_no": os.environ["BUILD_NUMBER"],
20+
"__ci__": "jenkinsci",
21+
}
2422
# Test for CircleCI
2523
if "CIRCLE_JOB" in os.environ and "CIRCLE_BUILD_NUM" in os.environ:
26-
d = dict(
27-
pipeline_branch=os.environ["CIRCLE_JOB"],
28-
pipeline_build_no=os.environ["CIRCLE_BUILD_NUM"],
29-
__ci__="circleci",
30-
)
24+
return {
25+
"pipeline_branch": os.environ["CIRCLE_JOB"],
26+
"pipeline_build_no": os.environ["CIRCLE_BUILD_NUM"],
27+
"__ci__": "circleci",
28+
}
3129
# Test for TravisCI
3230
if "TRAVIS_BUILD_NUMBER" in os.environ and "TRAVIS_BUILD_ID" in os.environ:
33-
d = dict(
34-
pipeline_branch=os.environ["TRAVIS_BUILD_ID"],
35-
pipeline_build_no=os.environ["TRAVIS_BUILD_NUMBER"],
36-
__ci__="travisci",
37-
)
31+
return {
32+
"pipeline_branch": os.environ["TRAVIS_BUILD_ID"],
33+
"pipeline_build_no": os.environ["TRAVIS_BUILD_NUMBER"],
34+
"__ci__": "travisci",
35+
}
3836
# Test for DroneCI
3937
if "DRONE_REPO_BRANCH" in os.environ and "DRONE_BUILD_NUMBER" in os.environ:
40-
d = dict(
41-
pipeline_branch=os.environ["DRONE_REPO_BRANCH"],
42-
pipeline_build_no=os.environ["DRONE_BUILD_NUMBER"],
43-
__ci__="droneci",
44-
)
38+
return {
39+
"pipeline_branch": os.environ["DRONE_REPO_BRANCH"],
40+
"pipeline_build_no": os.environ["DRONE_BUILD_NUMBER"],
41+
"__ci__": "droneci",
42+
}
4543
# Test for Gitlab CI
4644
if "CI_JOB_NAME" in os.environ and "CI_PIPELINE_ID" in os.environ:
47-
d = dict(
48-
pipeline_branch=os.environ["CI_JOB_NAME"],
49-
pipeline_build_no=os.environ["CI_PIPELINE_ID"],
50-
__ci__="gitlabci",
51-
)
52-
return d
45+
return {
46+
"pipeline_branch": os.environ["CI_JOB_NAME"],
47+
"pipeline_build_no": os.environ["CI_PIPELINE_ID"],
48+
"__ci__": "gitlabci",
49+
}
50+
return {}
5351

5452

5553
def determine_scm_revision():
@@ -68,9 +66,9 @@ def _get_cpu_string():
6866
if platform.system().lower() == "darwin":
6967
old_path = os.environ["PATH"]
7068
os.environ["PATH"] = old_path + ":" + "/usr/sbin"
71-
ret = subprocess.check_output("sysctl -n machdep.cpu.brand_string", shell=True).decode().strip()
69+
ret = subprocess.check_output("sysctl -n machdep.cpu.brand_string", shell=True)
7270
os.environ["PATH"] = old_path
73-
return ret
71+
return ret.decode().strip()
7472
if platform.system().lower() == "linux":
7573
with open("/proc/cpuinfo", "r", encoding="utf-8") as f:
7674
lines = [i for i in f if i.startswith("model name")]
@@ -107,19 +105,19 @@ def _read_cpu_freq_from_env(self):
107105
self.__cpu_freq_base = 0.0
108106

109107
def to_dict(self):
110-
return dict(
111-
cpu_count=self.cpu_count,
112-
cpu_frequency=self.cpu_frequency,
113-
cpu_type=self.cpu_type,
114-
cpu_vendor=self.cpu_vendor,
115-
ram_total=self.ram_total,
116-
machine_node=self.fqdn,
117-
machine_type=self.machine,
118-
machine_arch=self.architecture,
119-
system_info=self.system_info,
120-
python_info=self.python_info,
121-
h=self.hash(),
122-
)
108+
return {
109+
"cpu_count": self.cpu_count,
110+
"cpu_frequency": self.cpu_frequency,
111+
"cpu_type": self.cpu_type,
112+
"cpu_vendor": self.cpu_vendor,
113+
"ram_total": self.ram_total,
114+
"machine_node": self.fqdn,
115+
"machine_type": self.machine,
116+
"machine_arch": self.architecture,
117+
"system_info": self.system_info,
118+
"python_info": self.python_info,
119+
"h": self.compute_hash(),
120+
}
123121

124122
@property
125123
def cpu_count(self):
@@ -161,7 +159,7 @@ def system_info(self):
161159
def python_info(self):
162160
return self.__py_ver
163161

164-
def hash(self):
162+
def compute_hash(self):
165163
hr = hashlib.md5()
166164
hr.update(str(self.__cpu_count).encode())
167165
hr.update(str(self.__cpu_freq_base).encode())

tests/test_monitor.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ def test_ok():
3737
db = sqlite3.connect(str(pymon_path))
3838
cursor = db.cursor()
3939
cursor.execute("SELECT ITEM FROM TEST_METRICS;")
40-
assert 1 == len(cursor.fetchall()) # current test
40+
assert len(cursor.fetchall()) == 1
4141
cursor = db.cursor()
42-
tags = json.loads(
43-
cursor.execute("SELECT RUN_DESCRIPTION FROM TEST_SESSIONS;").fetchone()[0]
44-
)
42+
tags = json.loads(cursor.execute("SELECT RUN_DESCRIPTION FROM TEST_SESSIONS;").fetchone()[0])
4543
assert "description" not in tags
4644
assert "version" in tags
4745
assert tags["version"] == "12.3.5"
@@ -64,9 +62,7 @@ def test_ok():
6462
)
6563

6664
# run pytest with the following cmd args
67-
result = testdir.runpytest(
68-
"-vv", "--description", '"Test"', "--tag", "version=12.3.5"
69-
)
65+
result = testdir.runpytest("-vv", "--description", '"Test"', "--tag", "version=12.3.5")
7066

7167
# fnmatch_lines does an assertion internally
7268
result.stdout.fnmatch_lines(["*::test_ok PASSED*"])
@@ -80,11 +76,9 @@ def test_ok():
8076
db = sqlite3.connect(str(pymon_path))
8177
cursor = db.cursor()
8278
cursor.execute("SELECT ITEM FROM TEST_METRICS;")
83-
assert 1 == len(cursor.fetchall()) # current test
79+
assert len(cursor.fetchall()) == 1
8480
cursor = db.cursor()
85-
tags = json.loads(
86-
cursor.execute("SELECT RUN_DESCRIPTION FROM TEST_SESSIONS;").fetchone()[0]
87-
)
81+
tags = json.loads(cursor.execute("SELECT RUN_DESCRIPTION FROM TEST_SESSIONS;").fetchone()[0])
8882
assert "description" in tags
8983
assert tags["description"] == '"Test"'
9084
assert "version" in tags
@@ -182,9 +176,7 @@ def test_ok():
182176
result = testdir.runpytest("-v")
183177

184178
# fnmatch_lines does an assertion internally
185-
result.stdout.fnmatch_lines(
186-
["*::test_ok PASSED*", "*Nothing known about marker monitor_bad_marker*"]
187-
)
179+
result.stdout.fnmatch_lines(["*::test_ok PASSED*", "*Nothing known about marker monitor_bad_marker*"])
188180

189181
pymon_path = pathlib.Path(str(testdir)) / ".pymon"
190182
assert pymon_path.exists()
@@ -195,7 +187,7 @@ def test_ok():
195187
db = sqlite3.connect(str(pymon_path))
196188
cursor = db.cursor()
197189
cursor.execute("SELECT ITEM FROM TEST_METRICS;")
198-
assert 1 == len(cursor.fetchall()) # current test
190+
assert len(cursor.fetchall()) == 1
199191

200192

201193
def test_monitor_skip_module(testdir):
@@ -306,9 +298,7 @@ def test_monitored():
306298
result = testdir.runpytest("-v")
307299

308300
# fnmatch_lines does an assertion internally
309-
result.stdout.fnmatch_lines(
310-
["*::test_not_monitored PASSED*", "*::test_monitored PASSED*"]
311-
)
301+
result.stdout.fnmatch_lines(["*::test_not_monitored PASSED*", "*::test_monitored PASSED*"])
312302

313303
pymon_path = pathlib.Path(str(testdir)) / ".pymon"
314304
assert pymon_path.exists()
@@ -319,7 +309,7 @@ def test_monitored():
319309
db = sqlite3.connect(str(pymon_path))
320310
cursor = db.cursor()
321311
cursor.execute("SELECT ITEM FROM TEST_METRICS;")
322-
assert 1 == len(cursor.fetchall())
312+
assert len(cursor.fetchall()) == 1
323313

324314

325315
def test_monitor_no_db(testdir):

0 commit comments

Comments
 (0)