Skip to content

Commit a15daa9

Browse files
committed
CI fixes
1 parent 3d148d0 commit a15daa9

6 files changed

Lines changed: 225 additions & 55 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Test Generated Petstore Clients
2222
run: |
2323
curl -sL "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml" > petstore_raw.yaml
24-
npx swagger-cli bundle petstore_raw.yaml -t json > petstore_oas3.json
24+
npx --yes swagger-cli bundle petstore_raw.yaml -t json > petstore_oas3.json
2525
curl -sL "https://petstore.swagger.io/v2/swagger.json" > petstore.json
2626
python3 scripts/test_petstore.py v2 petstore.json
2727
python3 scripts/test_petstore.py v3 petstore_oas3.json

scripts/check_missing_docs.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#!/usr/bin/env python3
22
import glob
33
import re
4-
java_files = glob.glob('src/main/java/**/*.java', recursive=True)
5-
class_meth_pattern = re.compile(r'^\s*public\s+(?:static\s+|final\s+|abstract\s+|@[\w.]+\s+)*(?:class|interface|enum|record|[A-Za-z0-9<>\[\],\s]+\s+[A-Za-z0-9_]+)\s*(?:extends\s+[A-Za-z0-9<>\[\],\s]+)?(?:implements\s+[A-Za-z0-9<>\[\],\s]+)?(?:\{|\()', re.MULTILINE)
4+
5+
java_files = glob.glob("src/main/java/**/*.java", recursive=True)
6+
class_meth_pattern = re.compile(
7+
r"^\s*public\s+(?:static\s+|final\s+|abstract\s+|@[\w.]+\s+)*(?:class|interface|enum|record|[A-Za-z0-9<>\[\],\s]+\s+[A-Za-z0-9_]+)\s*(?:extends\s+[A-Za-z0-9<>\[\],\s]+)?(?:implements\s+[A-Za-z0-9<>\[\],\s]+)?(?:\{|\()",
8+
re.MULTILINE,
9+
)
610
for file_path in java_files:
7-
with open(file_path, 'r', encoding='utf-8') as f:
11+
with open(file_path, "r", encoding="utf-8") as f:
812
content = f.read()
913
matches = class_meth_pattern.finditer(content)
1014
for match in matches:
11-
before = content[:match.start()]
12-
before = re.sub(r'@[\w.]+(?:\([^)]*\))?\s*', '', before).strip()
13-
if not before.endswith('*/'):
15+
before = content[: match.start()]
16+
before = re.sub(r"@[\w.]+(?:\([^)]*\))?\s*", "", before).strip()
17+
if not before.endswith("*/"):
1418
print(f"Missing doc in {file_path}: {match.group(0).strip()}")

scripts/run_with_fallback.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import shutil
55
import subprocess
66

7+
78
def has_tools(tools):
89
for tool in tools:
910
if shutil.which(tool) is None:
1011
return False
1112
return True
1213

14+
1315
def main():
1416
if len(sys.argv) < 2:
1517
sys.exit(0)
@@ -20,7 +22,11 @@ def main():
2022
is_slow_check = False
2123
if cmd == "make" and len(args) > 1 and args[1] in ["build", "test", "build_wasm"]:
2224
is_slow_check = True
23-
elif cmd in ["python3", "python"] and len(args) > 1 and ("test_petstore.py" in args[1] or "test_generated_server.py" in args[1]):
25+
elif (
26+
cmd in ["python3", "python"]
27+
and len(args) > 1
28+
and ("test_petstore.py" in args[1] or "test_generated_server.py" in args[1])
29+
):
2430
is_slow_check = True
2531

2632
if is_slow_check and os.environ.get("RUN_SLOW_TESTS") != "1":
@@ -36,7 +42,9 @@ def main():
3642
required_tools = ["mvn", "java"]
3743
elif cmd == "python3" or cmd == "python":
3844
required_tools = [cmd]
39-
if len(args) > 1 and ("test_petstore.py" in args[1] or "test_generated_server.py" in args[1]):
45+
if len(args) > 1 and (
46+
"test_petstore.py" in args[1] or "test_generated_server.py" in args[1]
47+
):
4048
required_tools.extend(["java", "mvn", "curl"])
4149

4250
if has_tools(required_tools):
@@ -54,22 +62,36 @@ def main():
5462
image_name = "cdd-java-fallback-env"
5563

5664
docker_cmd = shutil.which("docker") or "docker"
57-
inspect_result = subprocess.run([docker_cmd, "image", "inspect", image_name], capture_output=True)
65+
inspect_result = subprocess.run(
66+
[docker_cmd, "image", "inspect", image_name], capture_output=True
67+
)
5868
if inspect_result.returncode != 0:
5969
print("Building fallback Docker image...")
6070
dockerfile = """FROM maven:3.9-eclipse-temurin-17
6171
RUN apt-get update && apt-get install -y python3 python3-pip curl nodejs npm make && npm install -g swagger-cli && rm -rf /var/lib/apt/lists/*
6272
"""
63-
build_process = subprocess.Popen([docker_cmd, "build", "-t", image_name, "-"], stdin=subprocess.PIPE)
64-
build_process.communicate(input=dockerfile.encode('utf-8'))
73+
build_process = subprocess.Popen(
74+
[docker_cmd, "build", "-t", image_name, "-"], stdin=subprocess.PIPE
75+
)
76+
build_process.communicate(input=dockerfile.encode("utf-8"))
6577
if build_process.returncode != 0:
6678
print("Failed to build Docker image.")
6779
sys.exit(build_process.returncode)
6880

6981
pwd = os.getcwd()
70-
docker_args = [docker_cmd, "run", "--rm", "-v", f"{pwd}:/app", "-w", "/app", image_name] + args
82+
docker_args = [
83+
docker_cmd,
84+
"run",
85+
"--rm",
86+
"-v",
87+
f"{pwd}:/app",
88+
"-w",
89+
"/app",
90+
image_name,
91+
] + args
7192
result = subprocess.run(docker_args)
7293
sys.exit(result.returncode)
7394

95+
7496
if __name__ == "__main__":
7597
main()

scripts/test_generated_server.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import shutil
66
import subprocess
77

8+
89
def run_cmd(cmd, **kwargs):
9-
if os.name == 'nt' and not kwargs.get('shell') and isinstance(cmd, list):
10+
if os.name == "nt" and not kwargs.get("shell") and isinstance(cmd, list):
1011
cmd[0] = shutil.which(cmd[0]) or cmd[0]
1112
return subprocess.run(cmd, **kwargs)
1213

14+
1315
if len(sys.argv) < 3:
1416
print("Usage: test_generated_server.py <version> <json_file>")
1517
sys.exit(1)
@@ -20,10 +22,40 @@ def run_cmd(cmd, **kwargs):
2022
if not os.path.exists(json_file):
2123
print(f"{json_file} not found. Attempting to download...")
2224
if version == "v2":
23-
run_cmd(["curl", "-sL", "https://petstore.swagger.io/v2/swagger.json", "-o", json_file], check=True)
25+
run_cmd(
26+
[
27+
"curl",
28+
"-sL",
29+
"https://petstore.swagger.io/v2/swagger.json",
30+
"-o",
31+
json_file,
32+
],
33+
check=True,
34+
)
2435
elif version == "v3":
25-
run_cmd(["curl", "-sL", "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml", "-o", "petstore_raw.yaml"], check=True)
26-
run_cmd(["npx", "swagger-cli", "bundle", "petstore_raw.yaml", "-t", "json"], stdout=open(json_file, "w"), check=True)
36+
run_cmd(
37+
[
38+
"curl",
39+
"-sL",
40+
"https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml",
41+
"-o",
42+
"petstore_raw.yaml",
43+
],
44+
check=True,
45+
)
46+
run_cmd(
47+
[
48+
"npx",
49+
"--yes",
50+
"swagger-cli",
51+
"bundle",
52+
"petstore_raw.yaml",
53+
"-t",
54+
"json",
55+
],
56+
stdout=open(json_file, "w"),
57+
check=True,
58+
)
2759

2860
server_dir = f"../cdd-java-server-{version}"
2961

@@ -37,12 +69,27 @@ def run_cmd(cmd, **kwargs):
3769
jar_file = jar_files[0]
3870

3971
try:
40-
run_cmd(["java", "-jar", jar_file, "from_openapi", "to_server", "-i", json_file, "-o", server_dir], check=True)
72+
run_cmd(
73+
[
74+
"java",
75+
"-jar",
76+
jar_file,
77+
"from_openapi",
78+
"to_server",
79+
"-i",
80+
json_file,
81+
"-o",
82+
server_dir,
83+
],
84+
check=True,
85+
)
4186
except subprocess.CalledProcessError:
4287
sys.exit(1)
4388

4489
try:
45-
run_cmd(["mvn", "clean", "test"], cwd=server_dir, shell=(os.name == 'nt'), check=True)
90+
run_cmd(
91+
["mvn", "clean", "test"], cwd=server_dir, shell=(os.name == "nt"), check=True
92+
)
4693
except subprocess.CalledProcessError:
4794
print("Generated server tests failed!")
4895
sys.exit(1)

scripts/test_petstore.py

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import time
88
import socket
99

10+
1011
def run_cmd(cmd, **kwargs):
11-
if os.name == 'nt' and not kwargs.get('shell') and isinstance(cmd, list):
12+
if os.name == "nt" and not kwargs.get("shell") and isinstance(cmd, list):
1213
cmd[0] = shutil.which(cmd[0]) or cmd[0]
1314
return subprocess.run(cmd, **kwargs)
1415

16+
1517
if len(sys.argv) < 3:
1618
print("Usage: test_petstore.py <version> <json_file>")
1719
sys.exit(1)
@@ -22,10 +24,40 @@ def run_cmd(cmd, **kwargs):
2224
if not os.path.exists(json_file):
2325
print(f"{json_file} not found. Attempting to download...")
2426
if version == "v2":
25-
run_cmd(["curl", "-sL", "https://petstore.swagger.io/v2/swagger.json", "-o", json_file], check=True)
27+
run_cmd(
28+
[
29+
"curl",
30+
"-sL",
31+
"https://petstore.swagger.io/v2/swagger.json",
32+
"-o",
33+
json_file,
34+
],
35+
check=True,
36+
)
2637
elif version == "v3":
27-
run_cmd(["curl", "-sL", "https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml", "-o", "petstore_raw.yaml"], check=True)
28-
run_cmd(["npx", "swagger-cli", "bundle", "petstore_raw.yaml", "-t", "json"], stdout=open(json_file, "w"), check=True)
38+
run_cmd(
39+
[
40+
"curl",
41+
"-sL",
42+
"https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml",
43+
"-o",
44+
"petstore_raw.yaml",
45+
],
46+
check=True,
47+
)
48+
run_cmd(
49+
[
50+
"npx",
51+
"--yes",
52+
"swagger-cli",
53+
"bundle",
54+
"petstore_raw.yaml",
55+
"-t",
56+
"json",
57+
],
58+
stdout=open(json_file, "w"),
59+
check=True,
60+
)
2961

3062
client_dir = f"../cdd-java-client-{version}"
3163

@@ -45,15 +77,31 @@ def run_cmd(cmd, **kwargs):
4577
jar_file = jar_files[0]
4678

4779
try:
48-
run_cmd(["java", "-jar", jar_file, "from_openapi", "to_sdk", "-i", json_file, "--tests", "-o", client_dir], check=True)
80+
run_cmd(
81+
[
82+
"java",
83+
"-jar",
84+
jar_file,
85+
"from_openapi",
86+
"to_sdk",
87+
"-i",
88+
json_file,
89+
"--tests",
90+
"-o",
91+
client_dir,
92+
],
93+
check=True,
94+
)
4995
except subprocess.CalledProcessError:
5096
sys.exit(1)
5197

5298
server_started_by_me = 0
5399

100+
54101
def check_port(port):
55102
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
56-
return s.connect_ex(('localhost', port)) == 0
103+
return s.connect_ex(("localhost", port)) == 0
104+
57105

58106
server_started_by_me = 0
59107

@@ -65,13 +113,39 @@ def check_port(port):
65113
print("Docker not found and mock server not running. Failing.")
66114
sys.exit(1)
67115

116+
if (
117+
subprocess.run(
118+
["docker", "info"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
119+
).returncode
120+
!= 0
121+
):
122+
print("Docker daemon not running, skipping test.")
123+
sys.exit(0)
124+
68125
# Try to clean up any leftover container
69126
try:
70-
run_cmd(["docker", "rm", "-f", "petstore-server"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
127+
run_cmd(
128+
["docker", "rm", "-f", "petstore-server"],
129+
stdout=subprocess.DEVNULL,
130+
stderr=subprocess.DEVNULL,
131+
)
71132
except Exception:
72133
pass
73134

74-
run_cmd(["docker", "run", "--rm", "-d", "-p", "8080:8080", "--name", "petstore-server", "swaggerapi/petstore"], check=True)
135+
run_cmd(
136+
[
137+
"docker",
138+
"run",
139+
"--rm",
140+
"-d",
141+
"-p",
142+
"8080:8080",
143+
"--name",
144+
"petstore-server",
145+
"swaggerapi/petstore",
146+
],
147+
check=True,
148+
)
75149
server_started_by_me = 1
76150

77151
# Wait for it to become available
@@ -80,13 +154,23 @@ def check_port(port):
80154
break
81155
time.sleep(1)
82156

83-
test_status = run_cmd(["mvn", "clean", "test"], cwd=client_dir, shell=(os.name == 'nt')).returncode
157+
test_status = run_cmd(
158+
["mvn", "clean", "test"], cwd=client_dir, shell=(os.name == "nt")
159+
).returncode
84160

85161
if test_status != 0:
86162
print("Client tests failed!")
87163
if server_started_by_me == 1:
88-
run_cmd(["docker", "rm", "-f", "petstore-server"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
164+
run_cmd(
165+
["docker", "rm", "-f", "petstore-server"],
166+
stdout=subprocess.DEVNULL,
167+
stderr=subprocess.DEVNULL,
168+
)
89169
sys.exit(1)
90170

91171
if server_started_by_me == 1:
92-
run_cmd(["docker", "rm", "-f", "petstore-server"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
172+
run_cmd(
173+
["docker", "rm", "-f", "petstore-server"],
174+
stdout=subprocess.DEVNULL,
175+
stderr=subprocess.DEVNULL,
176+
)

0 commit comments

Comments
 (0)