[beman-tidy] Implement cmake.project_name#313
Conversation
| if item.identifier == "project": | ||
| cmake_project_name = item.args[0].value |
There was a problem hiding this comment.
| if item.identifier == "project": | |
| cmake_project_name = item.args[0].value | |
| if item.identifier == "project": | |
| if item.args: | |
| cmake_project_name = item.args[0].value |
?
Same for get_cmake_library_name
| invalid_prefix = f"{test_data_prefix}/invalid" | ||
|
|
||
|
|
||
|
|
| @@ -1,2 +1,12 @@ | |||
| # missing 'project' from next line | |||
There was a problem hiding this comment.
I think I would prefer to keep the test cases not overlapping,
Rename existing 3 files to invalid-library_name-v1/2/3.txt, and add invalid-project_name-v1/2/3.txt
Let's keep testcases as simple as possible!
There was a problem hiding this comment.
Pull request overview
This PR adds a new Beman Standard check for validating the project() name in CMakeLists.txt (cmake.project_name) and extends the CMake test suite/data to cover valid/invalid cases.
Changes:
- Added
CMakeProjectNameCheckplus shared AST helper for extracting the CMake project name. - Made CMake parsing helpers accept a
skip_commentsparameter (defaulting to current behavior). - Added/updated pytest coverage and CMakeLists test fixtures for
cmake.project_name.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
beman_tidy/lib/checks/beman_standard/cmake.py |
Introduces cmake.project_name check and project-name extraction helper; adjusts parsing helper signatures. |
tests/lib/checks/beman_standard/cmake/test_cmake.py |
Adds tests for the new check and updates existing test comments. |
tests/lib/checks/beman_standard/cmake/data/invalid/invalid-CMakeLists-v1.txt |
Updates invalid fixture to represent “missing project()” scenario. |
tests/lib/checks/beman_standard/cmake/data/invalid/invalid-CMakeLists-v2.txt |
Updates invalid fixture to include an explicitly wrong project() name. |
tests/lib/checks/beman_standard/cmake/data/invalid/invalid-CMakeLists-v3.txt |
Updates invalid fixture to include an explicitly wrong project() name. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| valid_cmake_paths = [ | ||
| # CMakeLists.txt from beman.exemplar | ||
| Path(f"{valid_prefix}/CMakeLists-v1.txt") | ||
| ] | ||
|
|
| invalid_cmake_paths = [ | ||
| # CMakeLists.txt missing project 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 |
| # CMakeLists.txt with invalid library name | ||
| Path(f"{invalid_prefix}/invalid-CMakeLists-v2.txt"), | ||
| # CMakeLists.txt with another invalid library name |
|
|
||
| for item in ast: | ||
| if item.identifier == "project": | ||
| cmake_project_name = item.args[0].value |
| "Please update the CMakeLists.txt file so that the CMake project name is identical to the Beman library name. " | ||
| "See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakeproject_name for more information." | ||
| ) | ||
| return True |
| "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 |
|
|
||
| if cmake_project_name is None: | ||
| self.log("CMake project name not found. " | ||
| "Please update the CMakeLists.txt file according to the Beman Standard. " |
There was a problem hiding this comment.
| "Please update the CMakeLists.txt file according to the Beman Standard. " | |
| f"Expected project name: '{self.library_name}'" |
?
and in the next if can be Invalid CMake project name - got: {cmake_project_name} expected: {self.library_name}.
|
|
||
| if cmake_project_name is None: | ||
| self.log("CMake project name not found. " | ||
| f"Expected project name: '{self.library_name}'" |
| cmake_library_name2 = self.get_cmake_library_name(ast) | ||
|
|
||
| self.log(f"{cmake_library_name} -> {cmake_library_name2}") | ||
|
|
|
|
||
| if cmake_library_name is None: | ||
| self.log("CMake library target name not found. " | ||
| f"Expected library target name: '{self.library_name}'" |
| 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 |
| # Actual tested checks. | ||
| from beman_tidy.lib.checks.beman_standard.cmake import ( | ||
| CMakeProjectNameCheck, | ||
| CMakeLibraryNameCheck |
| valid_cmake_paths = [ | ||
| # CMakeLists.txt from beman.exemplar | ||
| Path(f"{valid_prefix}/CMakeLists-v1.txt") | ||
| ] |
| 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") | ||
| ] |
| # CMakeLists.txt with invalid library target name | ||
| Path(f"{invalid_prefix}/invalid-library_name-v2.txt"), | ||
| # CMakeLists.txt with another invalid library target name |
Implement: