Skip to content

Test/semgrep malformed json fallback 1658 - #2470

Open
Khanvilkarshravani27 wants to merge 3 commits into
utksh1:mainfrom
Khanvilkarshravani27:test/semgrep-malformed-json-fallback-1658
Open

Test/semgrep malformed json fallback 1658#2470
Khanvilkarshravani27 wants to merge 3 commits into
utksh1:mainfrom
Khanvilkarshravani27:test/semgrep-malformed-json-fallback-1658

Conversation

@Khanvilkarshravani27

Copy link
Copy Markdown
Collaborator

Description

Extends testing/backend/unit/test_semgrep_scanner_plugin.py with a new TestSemgrepParserMalformedJsonFallback test class that verifies the Semgrep parser's silent fallback behaviour when given malformed or structurally unexpected JSON input.

The parser wraps all parsing logic in a broad except Exception: pass block, silently returning {"count": 0, "findings": []} on any error. These tests confirm that this fallback is deterministic and consistent across four distinct malformed-input categories.

Tests added:

  • test_truncated_json_returns_empty_findings — truncated JSON ({"results": [{) simulating a mid-write process kill → count=0, findings=[]
  • test_mixed_stdout_with_json_fragment_returns_deterministic_empty_result — log lines mixed with a JSON fragment, called twice to assert determinism → count=0, findings=[]
  • test_valid_json_missing_top_level_results_key_returns_empty — syntactically valid JSON but no results key → count=0, findings=[]
  • test_valid_json_with_null_in_critical_fields_returns_empty_without_crash{"results": null} which triggers TypeError on iteration → count=0, findings=[]

All 8 tests in the file (4 existing + 4 new) pass: 8 passed in 2.11s

Related Issues

Closes #1658

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Test coverage improvement (adds missing edge-case tests for existing behaviour)

How Has This Been Tested?

Run the full test file locally:

python -m pytest testing/backend/unit/test_semgrep_scanner_plugin.py -v

Result: 8 passed, 1 warning in 2.11s — all pre-existing tests continue to pass alongside the four new fallback tests.

Checklist

  • My code follows the code style of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation. (N/A — test-only change)
  • My changes generate no new warnings.

…1862)

totalItems was only set from the first /findings call. Subsequent loadMore
fetches never updated it, so the 'Load More (X/Y)' guard used a stale total
whenever filters changed the server-side count between pages.

Changes:
- Import FindingsResponse type and use it instead of �ny in the initial
  load callback; filter findings to those with string ids for safety
- Add setTotalItems(data.total ?? moreFindings.length) inside loadMore after
  each successful paginated fetch, matching the same pattern already used on
  initial load; also apply the id-string filter to moreFindings
- Add two unit tests for the totalItems sync: one verifies the button hides
  when totalItems drops to match findings.length after loadMore; the other
  verifies the counter keeps updating correctly across pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test : add malformed JSON fallback tests for Semgrep scanner parser

1 participant