Skip to content

Commit 488f09f

Browse files
committed
cleanup of the test Python code, fixes many CodeQL warnings
1 parent f3661af commit 488f09f

File tree

8 files changed

+135
-87
lines changed

8 files changed

+135
-87
lines changed

tests/args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import json
44
import os
5+
56
args = [os.path.basename(a) for a in sys.argv[1:]]
67
with open("cwl.output.json", "w") as f:
78
json.dump({"args": args}, f)

tests/index.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,44 @@
55

66
import sys
77
import os
8+
from pathlib import Path
89

910
words = {}
10-
1111
mainfile = sys.argv[1]
1212
indexfile = sys.argv[1] + ".idx1"
13-
14-
main = open(mainfile)
15-
index = open(indexfile, "w")
16-
1713
linenum = 0
18-
for l in main:
19-
linenum += 1
20-
l = l.rstrip().lower().replace(".", "").replace(",", "").replace(";", "").replace("-", " ")
21-
for w in l.split(" "):
22-
if w:
23-
if w not in words:
24-
words[w] = set()
25-
words[w].add(linenum)
26-
27-
for w in sorted(words.keys()):
28-
index.write("%s: %s" % (w, ", ".join((str(i) for i in sorted(words[w])))) + "\n")
2914

30-
open(os.path.splitext(sys.argv[1])[0] + ".idx2", "w")
31-
open(sys.argv[1] + ".idx3", "w")
32-
open(sys.argv[1] + ".idx4", "w")
33-
open(sys.argv[1] + ".idx5", "w")
34-
open(os.path.splitext(sys.argv[1])[0] + ".idx6" + os.path.splitext(sys.argv[1])[1], "w")
35-
open(sys.argv[1] + ".idx7", "w")
36-
os.mkdir(sys.argv[1] + "_idx8")
37-
open(sys.argv[1] + "_idx8/index", "w")
15+
with open(mainfile) as main:
16+
for line in main:
17+
linenum += 1
18+
line = (
19+
line.rstrip()
20+
.lower()
21+
.replace(".", "")
22+
.replace(",", "")
23+
.replace(";", "")
24+
.replace("-", " ")
25+
)
26+
for word in line.split(" "):
27+
if word:
28+
if word not in words:
29+
words[word] = set()
30+
words[word].add(linenum)
31+
32+
with open(indexfile, "w") as index:
33+
for w in sorted(words.keys()):
34+
index.write(
35+
"%s: %s" % (w, ", ".join((str(i) for i in sorted(words[w])))) + "\n"
36+
)
37+
38+
Path(os.path.splitext(sys.argv[1])[0] + ".idx2").touch(exist_ok=False)
39+
Path(sys.argv[1] + ".idx3").touch(exist_ok=False)
40+
Path(sys.argv[1] + ".idx4").touch(exist_ok=False)
41+
Path(sys.argv[1] + ".idx5").touch(exist_ok=False)
42+
Path(
43+
os.path.splitext(sys.argv[1])[0] + ".idx6" + os.path.splitext(sys.argv[1])[1]
44+
).touch(exist_ok=False)
45+
Path(sys.argv[1] + ".idx7").touch(exist_ok=False)
46+
subdir = Path(sys.argv[1] + "_idx8")
47+
subdir.mkdir()
48+
(subdir / "index").touch(exist_ok=False)

tests/iwd/loadit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22
import sys
3-
j = json.load(open(sys.argv[1]))
4-
json.dump({"class": j["class"],
5-
"basename": j["basename"]}, sys.stdout, sort_keys=True)
3+
4+
with open(sys.argv[1]) as target:
5+
j = json.load(target)
6+
7+
json.dump({"class": j["class"], "basename": j["basename"]}, sys.stdout, sort_keys=True)

tests/json_schema/test_cwl_schema.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
from ruamel.yaml.scanner import ScannerError
2020

2121
# https://raw.githubusercontent.com/common-workflow-language/cwl-v1.2/1.2.1_proposed/conformance_tests.yaml
22-
CONFORMANCE_TESTS_FILE = os.path.join(os.path.dirname(__file__), "../../conformance_tests.yaml")
22+
CONFORMANCE_TESTS_FILE = os.path.join(
23+
os.path.dirname(__file__), "../../conformance_tests.yaml"
24+
)
2325
# https://raw.githubusercontent.com/common-workflow-language/cwl-v1.2/1.2.1_proposed/json-schema/cwl.yaml
24-
CWL_JSON_SCHEMA_FILE = os.path.join(os.path.dirname(__file__), "../../json-schema/cwl.yaml")
26+
CWL_JSON_SCHEMA_FILE = os.path.join(
27+
os.path.dirname(__file__), "../../json-schema/cwl.yaml"
28+
)
2529
CWL_JSON_SCHEMA_REF = f"{CWL_JSON_SCHEMA_FILE}#/$defs/CWL"
2630

2731
LOGGER = logging.getLogger(__name__)
@@ -35,22 +39,30 @@
3539
_JSON: TypeAlias = "JSON"
3640
_JsonObjectItemAlias: TypeAlias = "_JsonObjectItem"
3741
_JsonListItemAlias: TypeAlias = "_JsonListItem"
38-
_JsonObjectItem = Dict[str, Union[AnyValueType, _JSON, _JsonObjectItemAlias, _JsonListItemAlias]]
42+
_JsonObjectItem = Dict[
43+
str, Union[AnyValueType, _JSON, _JsonObjectItemAlias, _JsonListItemAlias]
44+
]
3945
_JsonListItem = List[Union[AnyValueType, _JSON, _JsonObjectItem, _JsonListItemAlias]]
4046
_JsonItem = Union[AnyValueType, _JSON, _JsonObjectItem, _JsonListItem]
41-
JSON = Union[Dict[str, Union[_JSON, _JsonItem]], List[Union[_JSON, _JsonItem]], AnyValueType]
47+
JSON = Union[
48+
Dict[str, Union[_JSON, _JsonItem]], List[Union[_JSON, _JsonItem]], AnyValueType
49+
]
4250

4351
ConformanceTestDef = TypedDict(
4452
"ConformanceTestDef",
4553
{
4654
"id": str,
4755
"doc": str,
48-
"tags": List[str], # should contain 'json_schema_invalid' to XFAIL schema validation tests
56+
"tags": List[
57+
str
58+
], # should contain 'json_schema_invalid' to XFAIL schema validation tests
4959
"tool": str,
5060
"job": NotRequired[str], # not used, for running the actual CWL
5161
"output": JSON, # not used, output of CWL execution
52-
"should_fail": NotRequired[bool], # indicates failure as "execute failing", but potentially still valid CWL
53-
}
62+
"should_fail": NotRequired[
63+
bool
64+
], # indicates failure as "execute failing", but potentially still valid CWL
65+
},
5466
)
5567

5668

@@ -76,7 +88,7 @@ def load_file(file_path: str, text: bool = False) -> Union[JSON, str]:
7688
:returns: loaded contents either parsed and converted to Python objects or as plain text.
7789
:raises ValueError: if YAML or JSON cannot be parsed or loaded from location.
7890
"""
79-
yaml = YAML(typ='safe', pure=True)
91+
yaml = YAML(typ="safe", pure=True)
8092
try:
8193
if is_remote_file(file_path):
8294
headers = {"Accept": "text/plain"}
@@ -131,10 +143,11 @@ def load_conformance_tests(test_file: str) -> List[ConformanceTestDef]:
131143

132144

133145
@pytest.mark.parametrize(
134-
"conformance_test",
135-
load_conformance_tests(CONFORMANCE_TESTS_FILE)
146+
"conformance_test", load_conformance_tests(CONFORMANCE_TESTS_FILE)
136147
)
137-
def test_conformance(conformance_test: ConformanceTestDef, request: pytest.FixtureRequest) -> None:
148+
def test_conformance(
149+
conformance_test: ConformanceTestDef, request: pytest.FixtureRequest
150+
) -> None:
138151
LOGGER.debug(
139152
"Testing [%s] (%s) with [%s]",
140153
conformance_test["id"],
@@ -149,11 +162,17 @@ def test_conformance(conformance_test: ConformanceTestDef, request: pytest.Fixtu
149162
instance_file = conformance_test["tool"].rsplit("#")[0]
150163
instance = load_file(instance_file)
151164
instance_xfail = "json_schema_invalid" in conformance_test.get("tags", [])
152-
request.applymarker(pytest.mark.xfail(reason="Test tagged with 'json_schema_invalid'.", condition=instance_xfail))
165+
request.applymarker(
166+
pytest.mark.xfail(
167+
reason="Test tagged with 'json_schema_invalid'.", condition=instance_xfail
168+
)
169+
)
153170

154171
schema_uri, schema_base, schema_test = resolve_ref(CWL_JSON_SCHEMA_REF)
155172
validator: Type[Validator] = jsonschema.validators.validator_for(schema_base)
156-
validator.resolver = jsonschema.RefResolver(base_uri=schema_uri, referrer=schema_base)
173+
validator.resolver = jsonschema.RefResolver(
174+
base_uri=schema_uri, referrer=schema_base
175+
)
157176

158177
try:
159178
# similar to 'validate()' call that raises directly, but obtain the error for more context
@@ -164,15 +183,21 @@ def test_conformance(conformance_test: ConformanceTestDef, request: pytest.Fixtu
164183
if len(errors) == 1 and "oneOf" in errors[0].schema:
165184
error = errors[0]
166185
error.message += "\n\nFor each case under oneOf:\n" + "\n".join(
167-
f"- {err.message}"
168-
if not err.message.endswith("is not valid under any of the given schemas")
169-
else f"- all invalid under oneOf {err.validator_value}"
186+
(
187+
f"- {err.message}"
188+
if not err.message.endswith(
189+
"is not valid under any of the given schemas"
190+
)
191+
else f"- all invalid under oneOf {err.validator_value}"
192+
)
170193
for err in error.context
171194
)
172195
raise error
173196
main_error = cast(
174197
ValidationError,
175-
best_match(errors), # what 'validate()' normally does using 'iter_errors()'
198+
best_match(
199+
errors
200+
), # what 'validate()' normally does using 'iter_errors()'
176201
)
177202
raise main_error
178203
except Exception as exc:

tests/loadContents/mkfilelist.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2+
23
ls = []
34
for i in range(1, 10000):
45
ls.append("example_input_file%i.txt" % i)
5-
f = open("cwl.output.json", "w")
6-
json.dump({"filelist": ls, "bigstring": "\n".join(ls)}, f)
6+
with open("cwl.output.json", "w") as f:
7+
json.dump({"filelist": ls, "bigstring": "\n".join(ls)}, f)

tests/search.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
indexfile = sys.argv[1] + ".idx1"
1010
term = sys.argv[2]
1111

12-
main = open(mainfile)
13-
index = open(indexfile)
14-
1512
st = term + ": "
1613

17-
for a in index:
18-
if a.startswith(st):
19-
n = [int(i) for i in a[len(st):].split(", ") if i]
20-
linenum = 0
21-
for l in main:
22-
linenum += 1
23-
if linenum in n:
24-
print(f"{linenum} {l.rstrip()}")
25-
break
14+
with open(mainfile) as main:
15+
with open(indexfile) as index:
16+
for a in index:
17+
if a.startswith(st):
18+
n = [int(i) for i in a[len(st) :].split(", ") if i]
19+
linenum = 0
20+
for line in main:
21+
linenum += 1
22+
if linenum in n:
23+
print(f"{linenum} {line.rstrip()}")
24+
break

tests/subdirsecondaries/index.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,42 @@
55

66
import sys
77
import os
8+
from pathlib import Path
89

910
words = {}
10-
1111
mainfile = sys.argv[1]
1212
indexfile = sys.argv[1] + ".idx1"
13-
14-
main = open(mainfile)
15-
index = open(indexfile, "w")
16-
1713
linenum = 0
18-
for l in main:
19-
linenum += 1
20-
l = l.rstrip().lower().replace(".", "").replace(",", "").replace(";", "").replace("-", " ")
21-
for w in l.split(" "):
22-
if w:
23-
if w not in words:
24-
words[w] = set()
25-
words[w].add(linenum)
26-
27-
for w in sorted(words.keys()):
28-
index.write("%s: %s" % (w, ", ".join((str(i) for i in words[w]))) + "\n")
2914

30-
open(os.path.splitext(sys.argv[1])[0] + ".idx2", "w")
31-
open(sys.argv[1] + ".idx3", "w")
32-
open(sys.argv[1] + ".idx4", "w")
33-
open(sys.argv[1] + ".idx5", "w")
34-
open(os.path.splitext(sys.argv[1])[0] + ".idx6" + os.path.splitext(sys.argv[1])[1], "w")
35-
open(sys.argv[1] + ".idx7", "w")
36-
os.mkdir(sys.argv[1] + "_idx8")
37-
open(sys.argv[1] + "_idx8/index", "w")
15+
with open(mainfile) as main:
16+
for line in main:
17+
linenum += 1
18+
line = (
19+
line.rstrip()
20+
.lower()
21+
.replace(".", "")
22+
.replace(",", "")
23+
.replace(";", "")
24+
.replace("-", " ")
25+
)
26+
for word in line.split(" "):
27+
if word:
28+
if word not in words:
29+
words[word] = set()
30+
words[word].add(linenum)
31+
32+
with open(indexfile, "w") as index:
33+
for w in sorted(words.keys()):
34+
index.write("%s: %s" % (w, ", ".join((str(i) for i in words[w]))) + "\n")
35+
36+
Path(os.path.splitext(sys.argv[1])[0] + ".idx2").touch(exist_ok=False)
37+
Path(sys.argv[1] + ".idx3").touch(exist_ok=False)
38+
Path(sys.argv[1] + ".idx4").touch(exist_ok=False)
39+
Path(sys.argv[1] + ".idx5").touch(exist_ok=False)
40+
Path(
41+
os.path.splitext(sys.argv[1])[0] + ".idx6" + os.path.splitext(sys.argv[1])[1]
42+
).touch(exist_ok=False)
43+
Path(sys.argv[1] + ".idx7").touch(exist_ok=False)
44+
subdir = Path(sys.argv[1] + "_idx8")
45+
subdir.mkdir(exist_ok=False)
46+
(subdir / "index").touch(exist_ok=False)

tests/updateval.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
2-
f = open(sys.argv[1], "r+")
3-
val = int(f.read())
4-
f.seek(0)
5-
f.write(str(val+1))
6-
f.close()
2+
3+
with open(sys.argv[1], "r+") as f:
4+
val = int(f.read())
5+
f.seek(0)
6+
f.write(str(val + 1))

0 commit comments

Comments
 (0)