Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions beman_tidy/lib/checks/beman_standard/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,71 @@ class CMakeBaseCheck(FileBaseCheck):
def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config, "CMakeLists.txt")

def get_cmake_parse_raw(self):
return cmake_parser.parser.parse_raw(self.read(), skip_comments=True)
def get_cmake_parse_raw(self, skip_comments=True):
return cmake_parser.parser.parse_raw(self.read(), skip_comments=skip_comments)

def get_cmake_parse_tree(self):
return cmake_parser.parser.parse_tree(self.read(), skip_comments=True)
def get_cmake_parse_tree(self, skip_comments=True):
return cmake_parser.parser.parse_tree(self.read(), skip_comments=skip_comments)

def get_cmake_library_name(self, ast):
cmake_library_name = None

for item in ast:
if item.identifier == "add_library":
cmake_library_name = item.args[0].value
break
if item.args:
cmake_library_name = item.args[0].value
break

return cmake_library_name

def get_cmake_project_name(self, ast):
cmake_project_name = None

for item in ast:
if item.identifier == "project":
if item.args:
cmake_project_name = item.args[0].value
break

return cmake_project_name


# TODO cmake.default


# TODO cmake.use_find_package


# TODO cmake.project_name
@register_beman_standard_check("cmake.project_name")
class CMakeProjectNameCheck(CMakeBaseCheck):
def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

def check(self):
ast = self.get_cmake_parse_raw()
cmake_project_name = self.get_cmake_project_name(ast)

if cmake_project_name is None:
self.log("CMake project name not found. "
f"Expected project name: '{self.library_name}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
Comment thread
neatudarius marked this conversation as resolved.
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakeproject_name for more information.")
return False

if cmake_project_name != self.library_name:
self.log(f"Invalid CMake project name - got: '{cmake_project_name}', expected: '{self.library_name}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakeproject_name for more information.")
return False

return True

def fix(self):
self.log(
"Please update the CMakeLists.txt file so that the CMake project name is identical to the Beman project name. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakeproject_name for more information."
)
return False
Comment thread
neatudarius marked this conversation as resolved.


# TODO cmake.passive_projects
Expand All @@ -82,12 +123,13 @@ def check(self):

if cmake_library_name is None:
self.log("CMake library target name not found. "
f"Expected library target name: '{self.library_name}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_name for more information.")
return False

if cmake_library_name != self.library_name:
self.log(f"CMake library target name: {cmake_library_name} does not match library name: {self.library_name}. "
self.log(f"Invalid CMake library target name - got: '{cmake_library_name}', expected: '{self.library_name}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_name for more information.")
return False
Expand All @@ -96,10 +138,10 @@ def check(self):

def fix(self):
self.log(
"Please update the CMakeLists.txt file so that the CMake library target's name is identical to the library name. "
"Please update the CMakeLists.txt file so that the CMake library target's name is identical to the Beman library name. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_name for more information."
)
return True
return False


# TODO cmake.library_alias
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# wrong library target name: beman.exemplar2
add_library(beman.exemplar2 INTERFACE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# wrong library target name: beman_exemplar
add_library(beman_exemplar INTERFACE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# missing 'project' from next line
#[===[
project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 2.4.0
)
]===]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# wrong project name: beman.exemplar2
project(
beman.exemplar2
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 2.4.0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# wrong project name: beman_exemplar
project(
beman_exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 2.4.0
)
66 changes: 58 additions & 8 deletions tests/lib/checks/beman_standard/cmake/test_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,71 @@

# Actual tested checks.
from beman_tidy.lib.checks.beman_standard.cmake import (
CMakeLibraryNameCheck
CMakeProjectNameCheck,
CMakeLibraryNameCheck,
)

test_data_prefix = "tests/lib/checks/beman_standard/cmake/data"
valid_prefix = f"{test_data_prefix}/valid"
invalid_prefix = f"{test_data_prefix}/invalid"


def test__cmake_project_name__valid(repo_info, beman_standard_check_config):
"""
Test that a valid CMakeLists.txt file passes the cmake.project_name check.
"""
valid_cmake_paths = [
# CMakeLists.txt from beman.exemplar
Path(f"{valid_prefix}/CMakeLists-v1.txt"),
]
Comment on lines +26 to +29

Comment on lines +26 to +30
run_check_for_each_path(
True,
valid_cmake_paths,
CMakeProjectNameCheck,
repo_info,
beman_standard_check_config,
)


def test__cmake_project_name__invalid(repo_info, beman_standard_check_config):
"""
Test that an invalid CMakeLists.txt file fails the cmake.project_name check.
"""
invalid_cmake_paths = [
# CMakeLists.txt missing project name
Path(f"{invalid_prefix}/invalid-project_name-v1.txt"),
# CMakeLists.txt with invalid project name
Path(f"{invalid_prefix}/invalid-project_name-v2.txt"),
# CMakeLists.txt with another invalid project name
Path(f"{invalid_prefix}/invalid-project_name-v3.txt"),
]
Comment on lines +44 to +51

run_check_for_each_path(
False,
invalid_cmake_paths,
CMakeProjectNameCheck,
repo_info,
beman_standard_check_config,
)


@pytest.mark.skip(reason="not implemented")
def test__cmake_project_name__fix_inplace(repo_info, beman_standard_check_config):
"""
Test that the fix method corrects an invalid CMakeLists.txt file.
Note: Skipping this test as it is not implemented.
"""
pass


def test__cmake_library_name__valid(repo_info, beman_standard_check_config):
"""
Test that a valid CMakeLists.txt file passes the cmake.library_name check.
"""
valid_cmake_paths = [
# CMakeLists.txt from beman.exemplar
Path(f"{valid_prefix}/CMakeLists-v1.txt")
Path(f"{valid_prefix}/CMakeLists-v1.txt"),
]

run_check_for_each_path(
Expand All @@ -41,12 +91,12 @@ def test__cmake_library_name__invalid(repo_info, beman_standard_check_config):
Test that an invalid CMakeLists.txt file fails the cmake.library_name check.
"""
invalid_cmake_paths = [
# CMakeLists.txt missing library name
Path(f"{invalid_prefix}/invalid-CMakeLists-v1.txt"),
# CMakeLists.txt with invalid library name
Path(f"{invalid_prefix}/invalid-CMakeLists-v2.txt"),
# CMakeLists.txt with another invalid library name
Path(f"{invalid_prefix}/invalid-CMakeLists-v3.txt")
# CMakeLists.txt missing library target name
Path(f"{invalid_prefix}/invalid-library_name-v1.txt"),
# CMakeLists.txt with invalid library target name
Path(f"{invalid_prefix}/invalid-library_name-v2.txt"),
# CMakeLists.txt with another invalid library target name
Comment on lines +96 to +98
Path(f"{invalid_prefix}/invalid-library_name-v3.txt"),
]

run_check_for_each_path(
Expand Down
Loading