-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_style.py
131 lines (111 loc) · 3.78 KB
/
test_style.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""Tests ensuring codebase style compliance for Rust and Python."""
from subprocess import run, PIPE
import os
import platform
import pytest
import yaml
SUCCESS_CODE = 0
@pytest.mark.timeout(120)
def test_rust_style():
"""Fail if there's misbehaving Rust style in this repo."""
# Check that the output is empty.
process = run(
'cargo fmt --all -- --check',
shell=True,
check=True,
stdout=PIPE
)
# rustfmt prepends `"Diff in"` to the reported output.
assert "Diff in" not in process.stdout.decode('utf-8')
@pytest.mark.timeout(120)
@pytest.mark.skipif(
platform.machine() != "x86_64",
reason="no need to test it on multiple platforms"
)
def test_python_style():
"""Fail if there's misbehaving Python style in the test system."""
# Check style with pylint.
# We are using `xargs` for propagating error code triggered by the
# actual command to stderr.
cmd = r'find ../ -type f -iname "*.py" -not -path "../build/*" ' \
r'-print0 | ' \
r'xargs -0 -n1 ' \
r'python3 -m pylint --jobs=0 --persistent=no --score=no ' \
r'--output-format=colorized --attr-rgx="[a-z_][a-z0-9_]{1,30}$" ' \
r'--argument-rgx="[a-z_][a-z0-9_]{1,30}$" ' \
r'--variable-rgx="[a-z_][a-z0-9_]{1,30}$" --disable=' \
r'bad-continuation,fixme,too-many-instance-attributes,' \
r'too-many-locals,too-many-arguments'
run(
cmd,
shell=True,
check=True
)
# Check style with flake8.
# TODO: Uncomment this after https://gitlab.com/pycqa/flake8/issues/406 is
# fixed
# run('python3 -m flake8 ../', shell=True, check=True)
# Check style with pycodestyle.
cmd = r'python3 -m pycodestyle --show-pep8 --show-source ' \
r'--exclude=../build ../'
run(
cmd,
shell=True,
check=True
)
# Check style with pydocstyle.
# pydocstyle's --match-dir option appears to be broken, so we're using
# `find` here to exclude the build/ dir.
cmd = r'find ../ -type f -iname "*.py" -not -path "../build/*" ' \
r'-print0 | ' \
r'xargs -0 -n1 ' \
r'python3 -m pydocstyle --explain --source'
run(
cmd,
shell=True,
check=True
)
@pytest.mark.skipif(
platform.machine() != "x86_64",
reason="no need to test it on multiple platforms"
)
def test_rust_clippy():
"""Fails if clippy generates any error, warnings are ignored."""
run(
'cargo clippy --all --profile test -- -D warnings',
shell=True,
check=True,
stdout=PIPE
)
def check_swagger_style(yaml_spec):
"""Check if the swagger definition is correctly formatted."""
with open(yaml_spec, 'r') as file_stream:
try:
yaml.safe_load(file_stream)
# pylint: disable=broad-except
except Exception as exception:
print(str(exception))
@pytest.mark.skipif(
platform.machine() != "x86_64",
reason="no need to test it on multiple platforms"
)
def test_firecracker_swagger():
"""Fail if Firecracker swagger specification is malformed."""
yaml_spec = os.path.normpath(
os.path.join(os.getcwd(), '../api_server/swagger/firecracker.yaml')
)
check_swagger_style(yaml_spec)
@pytest.mark.skipif(
platform.machine() != "x86_64",
reason="no need to test it on multiple platforms"
)
def test_experimental_firecracker_swagger():
"""Fail if experimental Firecracker swagger specification is malformed."""
yaml_spec = os.path.normpath(
os.path.join(
os.getcwd(),
'../api_server/swagger/firecracker-experimental.yaml')
)
check_swagger_style(yaml_spec)