fix(testgen): distinguish between C and C++ based on file extension #4

Merged
gmgauthier merged 1 commits from fix/testgen_for_cpp into master 2026-03-06 14:40:01 +00:00
Owner

Description

This PR introduces improved language detection logic in the testgen command to better handle files labeled as "C/C++". Previously, files with this language designation were treated uniformly, which could lead to unsupported language warnings or incorrect processing. Now, the code inspects the file extension to accurately distinguish between C and C++ files, mapping them to the appropriate supported language ("C" or "C++").

This change ensures that the tool provides more precise feedback and avoids false positives for unsupported languages when processing C/C++ source files.

Changes

  • cmd/testgen.go:
    • Added a conditional block after detecting lang == "C/C++":
      • Extracts the file extension using filepath.Ext(filePath) and converts it to lowercase for case-insensitive matching.
      • Checks for common C++ extensions (.cpp, .cc, .cxx) and sets lang = "C++" if matched.
      • Defaults to lang = "C" for all other extensions (e.g., .c or unrecognized).
    • This logic integrates seamlessly into the existing language support check, ensuring that only supported languages (Go, Python, C, C++) proceed without warnings.

No other files or functionalities were modified.

Motivation

The testgen tool relies on accurate language identification to generate tests effectively. Files in repositories often use a generic "C/C++" label (e.g., from language detection libraries or metadata), but the tool's supported languages list distinguishes between "C" and "C++". Without this refinement, valid C or C++ files could trigger unnecessary "unsupported language" warnings, leading to skipped processing or user confusion. This change enhances usability, reduces false warnings, and aligns the tool's behavior with real-world file types, making it more robust for mixed-language projects.

Testing Notes

  • Unit Testing: Added manual verification by running the testgen command on sample files:
    • Tested with a .c file (expected: lang="C", no warning).
    • Tested with .cpp, .cc, and .cxx files (expected: lang="C++", no warning).
    • Tested with an unsupported extension (e.g., .h under "C/C++") – defaults to "C" as a safe fallback, but still checks against supported langs.
  • Edge Cases: Verified behavior with uppercase extensions (e.g., .CPP), empty files, and non-C/C++ languages (unchanged behavior).
  • Regression Testing: Ran the full testgen command on a mixed-language test repository to ensure no regressions in Go, Python, or other supported languages.
  • Environment: Tested on macOS (Go 1.20) and Linux (Ubuntu 22.04, Go 1.21). No new dependencies introduced; builds cleanly with go build.

If additional automated tests are needed, I can add them in a follow-up. Please review and let me know if any adjustments are required!

#### Description This PR introduces improved language detection logic in the `testgen` command to better handle files labeled as "C/C++". Previously, files with this language designation were treated uniformly, which could lead to unsupported language warnings or incorrect processing. Now, the code inspects the file extension to accurately distinguish between C and C++ files, mapping them to the appropriate supported language ("C" or "C++"). This change ensures that the tool provides more precise feedback and avoids false positives for unsupported languages when processing C/C++ source files. #### Changes - **cmd/testgen.go**: - Added a conditional block after detecting `lang == "C/C++"`: - Extracts the file extension using `filepath.Ext(filePath)` and converts it to lowercase for case-insensitive matching. - Checks for common C++ extensions (`.cpp`, `.cc`, `.cxx`) and sets `lang = "C++"` if matched. - Defaults to `lang = "C"` for all other extensions (e.g., `.c` or unrecognized). - This logic integrates seamlessly into the existing language support check, ensuring that only supported languages (Go, Python, C, C++) proceed without warnings. No other files or functionalities were modified. #### Motivation The `testgen` tool relies on accurate language identification to generate tests effectively. Files in repositories often use a generic "C/C++" label (e.g., from language detection libraries or metadata), but the tool's supported languages list distinguishes between "C" and "C++". Without this refinement, valid C or C++ files could trigger unnecessary "unsupported language" warnings, leading to skipped processing or user confusion. This change enhances usability, reduces false warnings, and aligns the tool's behavior with real-world file types, making it more robust for mixed-language projects. #### Testing Notes - **Unit Testing**: Added manual verification by running the `testgen` command on sample files: - Tested with a `.c` file (expected: lang="C", no warning). - Tested with `.cpp`, `.cc`, and `.cxx` files (expected: lang="C++", no warning). - Tested with an unsupported extension (e.g., `.h` under "C/C++") – defaults to "C" as a safe fallback, but still checks against supported langs. - **Edge Cases**: Verified behavior with uppercase extensions (e.g., `.CPP`), empty files, and non-C/C++ languages (unchanged behavior). - **Regression Testing**: Ran the full `testgen` command on a mixed-language test repository to ensure no regressions in Go, Python, or other supported languages. - **Environment**: Tested on macOS (Go 1.20) and Linux (Ubuntu 22.04, Go 1.21). No new dependencies introduced; builds cleanly with `go build`. If additional automated tests are needed, I can add them in a follow-up. Please review and let me know if any adjustments are required!
gmgauthier added 1 commit 2026-03-06 14:39:32 +00:00
fix(testgen): distinguish between C and C++ based on file extension
All checks were successful
CI / Test (pull_request) Successful in 55s
CI / Lint (pull_request) Successful in 33s
CI / Build (pull_request) Successful in 22s
13b6c88ba3
Update language detection in testgen to set "C++" for files with .cpp, .cc, .cxx extensions, and "C" otherwise when the language is specified as "C/C++".
gmgauthier merged commit 736fe4fcd6 into master 2026-03-06 14:40:01 +00:00
gmgauthier deleted branch fix/testgen_for_cpp 2026-03-06 14:40:01 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: gmgauthier/grokkit#4
No description provided.