Files
trailofbits-buttercup/program-model/tests/java/test_commons_codec.py
Dan Guido 42691e50b4 style: apply ruff auto-fixes and formatting across entire codebase (#309)
* style: apply ruff auto-fixes and formatting across entire codebase

Applied safe auto-fixes from ruff v0.12.9 with --select ALL to improve code quality:
- Reorder imports (stdlib → third-party → local)
- Use modern type hints (collections.abc.Generator instead of typing.Generator)
- Add trailing commas for better diffs
- Format multi-line function parameters for readability
- Add strict=False to zip() calls for explicit behavior
- Simplify redundant elif to if after return statements
- Consistent code formatting with ruff format

These are all mechanical, non-controversial changes that improve code consistency
without altering functionality. Changes affect 180 files across all modules:
common, fuzzer, orchestrator, patcher, program-model, and seed-gen.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* re-applt ruff after merge

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Michael D Brown <michael.brown@trailofbits.com>
2025-08-22 10:30:12 -04:00

224 lines
6.3 KiB
Python

"""CodeQuery primitives testing"""
import pytest
from buttercup.common.challenge_task import ChallengeTask
from buttercup.program_model.codequery import CodeQuery
from ..common import (
TestCalleeInfo,
TestCallerInfo,
TestFunctionInfo,
TestTypeDefinitionInfo,
TestTypeUsageInfo,
TypeDefinitionType,
common_test_get_callees,
common_test_get_callers,
common_test_get_functions,
common_test_get_type_definitions,
common_test_get_type_usages,
)
@pytest.mark.parametrize(
"function_name,file_path,function_info",
[
(
"apply",
"/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
TestFunctionInfo(
num_bodies=1,
body_excerpts=[
"final LanguageSet languages = left.getLanguages().restrictTo(right.getLanguages());",
],
),
),
(
"invoke",
"/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
TestFunctionInfo(
num_bodies=1,
body_excerpts=[
"final List<Rule> rules = this.finalRules.get(input.subSequence(i, i+patternLength));",
],
),
),
],
)
@pytest.mark.integration
def test_get_functions(
commons_codec_oss_fuzz_task: ChallengeTask,
commons_codec_oss_fuzz_cq: CodeQuery,
function_name,
file_path,
function_info,
):
"""Test that we can get functions"""
common_test_get_functions(commons_codec_oss_fuzz_cq, function_name, file_path, function_info)
# From: https://github.com/apache/commons-codec/blob/44e4c4d778c3ab87db09c00e9d1c3260fd42dad5/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java#L206
@pytest.mark.parametrize(
"function_name,file_path,line_number,fuzzy,expected_callers,num_callers",
[
(
"apply",
"/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
None,
False,
[
TestCallerInfo(
name="invoke",
file_path="/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
start_line=206,
),
],
1,
),
],
)
@pytest.mark.skip(reason="Skipping test due to codequery issue")
@pytest.mark.integration
def test_get_callers(
commons_codec_oss_fuzz_task: ChallengeTask,
commons_codec_oss_fuzz_cq: CodeQuery,
function_name,
file_path,
line_number,
fuzzy,
expected_callers,
num_callers,
):
"""Test that we can get function callers"""
common_test_get_callers(
commons_codec_oss_fuzz_task,
commons_codec_oss_fuzz_cq,
function_name,
file_path,
line_number,
fuzzy,
expected_callers,
num_callers,
)
# From: https://github.com/apache/commons-codec/blob/44e4c4d778c3ab87db09c00e9d1c3260fd42dad5/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java#L206
@pytest.mark.parametrize(
"function_name,file_path,line_number,fuzzy,expected_callees,num_callees",
[
(
"invoke",
"/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
None,
False,
[
TestCalleeInfo(
name="apply",
file_path="/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
start_line=206,
),
],
5,
),
],
)
@pytest.mark.skip(reason="Skipping test due to codequery issue")
@pytest.mark.integration
def test_get_callees(
commons_codec_oss_fuzz_task: ChallengeTask,
commons_codec_oss_fuzz_cq: CodeQuery,
function_name,
file_path,
line_number,
fuzzy,
expected_callees,
num_callees,
):
"""Test that we can get function callees."""
common_test_get_callees(
commons_codec_oss_fuzz_task,
commons_codec_oss_fuzz_cq,
function_name,
file_path,
line_number,
fuzzy,
expected_callees,
num_callees,
)
@pytest.mark.parametrize(
"type_name,file_path,fuzzy,type_definition_info",
[
(
"PhonemeBuilder",
None,
False,
TestTypeDefinitionInfo(
name="PhonemeBuilder",
type=TypeDefinitionType.CLASS,
definition="static final class PhonemeBuilder {",
definition_line=64,
file_path="/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
),
),
],
)
@pytest.mark.integration
def test_get_type_definitions(
commons_codec_oss_fuzz_task: ChallengeTask,
commons_codec_oss_fuzz_cq: CodeQuery,
type_name,
file_path,
fuzzy,
type_definition_info,
):
"""Test that we can get type defs"""
common_test_get_type_definitions(
commons_codec_oss_fuzz_task,
commons_codec_oss_fuzz_cq,
type_name,
file_path,
fuzzy,
type_definition_info,
)
@pytest.mark.parametrize(
"type_name,file_path,fuzzy,type_usage_infos,num_type_usages",
[
(
"PhonemeBuilder",
"/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
False,
[
TestTypeUsageInfo(
file_path="/src/commons-codec/src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java",
line_number=75,
),
],
12,
),
],
)
@pytest.mark.integration
def test_get_type_usages(
commons_codec_oss_fuzz_task: ChallengeTask,
commons_codec_oss_fuzz_cq: CodeQuery,
type_name,
file_path,
fuzzy,
type_usage_infos,
num_type_usages,
):
"""Test that we can get type usages"""
common_test_get_type_usages(
commons_codec_oss_fuzz_task,
commons_codec_oss_fuzz_cq,
type_name,
file_path,
fuzzy,
type_usage_infos,
num_type_usages,
)