Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/style/test_rust.py
1958 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Tests ensuring codebase style compliance for Rust."""
4
5
import framework.utils as utils
6
7
8
def test_rust_style():
9
"""Fail if there's misbehaving Rust style in this repo."""
10
# Check that the output is empty.
11
_, stdout, _ = utils.run_cmd(
12
'cargo fmt --all -- --check')
13
14
# rustfmt prepends `"Diff in"` to the reported output.
15
assert "Diff in" not in stdout
16
17
18
def test_ensure_mod_tests():
19
"""Check that files containing unit tests have a 'tests' module defined."""
20
# List all source files containing rust #[test] attribute,
21
# (excluding generated files and integration test directories).
22
# Take the list and check each file contains 'mod tests {', output file
23
# name if it doesn't.
24
cmd = (
25
'/bin/bash '
26
'-c '
27
'"grep '
28
'--files-without-match '
29
'\'mod tests {\' '
30
'\\$(grep '
31
'--files-with-matches '
32
'--recursive '
33
'--exclude-dir=src/*_gen/* '
34
'\'\\#\\[test\\]\' ../src/*/src)" '
35
)
36
37
# The outer grep returns 0 even if it finds files without the match, so we
38
# ignore the return code.
39
result = utils.run_cmd(cmd, no_shell=False, ignore_return_code=True)
40
41
error_msg = (
42
'Tests found in files without a "tests" module:\n {}'
43
'To ensure code coverage is reported correctly, please check that '
44
'your tests are in a module named "tests".'.format(result.stdout)
45
)
46
47
assert not result.stdout, error_msg
48
49