Skip to content

Commit 6bdc53d

Browse files
pb8owearyzen
authored andcommitted
style: only test for files in the repo
Add a helper function to only consider files in the repo. This helps if the local workspace has files coming from other sources, like the linux kernel, or uncommitted changes. Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent e3b93fa commit 6bdc53d

File tree

5 files changed

+34
-49
lines changed

5 files changed

+34
-49
lines changed

tests/framework/utils.py

-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Generic utility functions that are used in the framework."""
44
import functools
5-
import glob
65
import json
76
import logging
87
import os
@@ -419,34 +418,6 @@ def search_output_from_cmd(cmd: str, find_regex: typing.Pattern) -> typing.Match
419418
)
420419

421420

422-
def get_files_from(
423-
find_path: str, pattern: str, exclude_names: list = None, recursive: bool = True
424-
):
425-
"""
426-
Return a list of files from a given path, recursively.
427-
428-
:param find_path: path where to look for files
429-
:param pattern: what pattern to apply to file names
430-
:param exclude_names: folder names to exclude
431-
:param recursive: do a recursive search for the given pattern
432-
:return: list of found files
433-
"""
434-
found = []
435-
# For each directory in the given path
436-
for path_dir in os.scandir(find_path):
437-
# Check if it should be skipped
438-
if path_dir.name in exclude_names or os.path.isfile(path_dir):
439-
continue
440-
# Run glob inside the folder with the given pattern
441-
found.extend(
442-
glob.glob(f"{find_path}/{path_dir.name}/**/{pattern}", recursive=recursive)
443-
)
444-
# scandir will not look at the files matching the pattern in the
445-
# current directory.
446-
found.extend(glob.glob(f"{find_path}/./{pattern}"))
447-
return found
448-
449-
450421
def get_free_mem_ssh(ssh_connection):
451422
"""
452423
Get how much free memory in kB a guest sees, over ssh.

tests/framework/utils_repo.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Utilities to deal with the git repo."""
5+
6+
import subprocess
7+
from fnmatch import fnmatch
8+
from pathlib import Path
9+
10+
11+
def git_repo_files(root: str = ".", glob: str = "*"):
12+
"""
13+
Return a list of files in the git repo from a given path
14+
15+
:param root: path where to look for files, defaults to the current dir
16+
:param glob: what pattern to apply to file names
17+
:return: list of found files
18+
"""
19+
files = subprocess.check_output(
20+
["git", "ls-files", root],
21+
encoding="ascii",
22+
).splitlines()
23+
for file in files:
24+
if fnmatch(file, glob):
25+
yield Path(file)

tests/integration_tests/style/test_licenses.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import datetime
66

7-
from framework import utils
7+
from framework import utils_repo
88

99
AMAZON_COPYRIGHT_YEARS = range(2018, datetime.datetime.now().year + 1)
1010
AMAZON_COPYRIGHT = (
@@ -24,8 +24,6 @@
2424
ALIBABA_COPYRIGHT = "Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved."
2525
ALIBABA_LICENSE = "SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause"
2626

27-
EXCLUDE = ["build", ".kernel", ".git"]
28-
2927

3028
def _has_amazon_copyright(string):
3129
for year in AMAZON_COPYRIGHT_YEARS:
@@ -90,16 +88,10 @@ def test_for_valid_licenses():
9088
"""
9189
Test that all *.py, *.rs and *.sh files contain a valid license.
9290
"""
93-
python_files = utils.get_files_from(
94-
find_path="..", pattern="*.py", exclude_names=EXCLUDE
95-
)
96-
rust_files = utils.get_files_from(
97-
find_path="..", pattern="*.rs", exclude_names=EXCLUDE
98-
)
99-
bash_files = utils.get_files_from(
100-
find_path="..", pattern="*.sh", exclude_names=EXCLUDE
101-
)
10291

92+
python_files = list(utils_repo.git_repo_files(root="..", glob="*.py"))
93+
rust_files = list(utils_repo.git_repo_files(root="..", glob="*.rs"))
94+
bash_files = list(utils_repo.git_repo_files(root="..", glob="*.sh"))
10395
all_files = rust_files + python_files + bash_files
10496
error_msg = []
10597
for file in all_files:

tests/integration_tests/style/test_markdown.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Tests for markdown style checks."""
44

5-
from framework import utils
5+
from framework import utils, utils_repo
66

77

88
def test_markdown_style():
99
"""
1010
Test that markdown files adhere to the style rules.
1111
"""
1212
# Get all *.md files from the project
13-
md_files = utils.get_files_from(
14-
find_path="..", pattern="*.md", exclude_names=["build"]
15-
)
13+
md_files = list(utils_repo.git_repo_files(root="..", glob="*.md"))
1614

1715
# Assert if somehow no markdown files were found.
1816
assert len(md_files) != 0

tests/integration_tests/style/test_repo.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import yaml
1111

12+
from framework import utils_repo
13+
1214

1315
def test_repo_no_spaces_in_paths():
1416
"""
@@ -30,10 +32,7 @@ def test_repo_validate_yaml():
3032
Ensure all YAML files are valid
3133
"""
3234

33-
repo_root = Path("..")
34-
for path in repo_root.rglob("*.y*ml"):
35-
if str(path).startswith("../build/"):
36-
continue
35+
for path in utils_repo.git_repo_files(root="..", glob="*.y*ml"):
3736
yaml.safe_load(path.open(encoding="utf-8"))
3837

3938

0 commit comments

Comments
 (0)