Skip to content

fix(quality): resolve CodeQL unused local variables and add Python & Shell test suites - #222

Open
aghassemlouei wants to merge 6 commits into
mainfrom
fix/py-unused-local-variables
Open

fix(quality): resolve CodeQL unused local variables and add Python & Shell test suites#222
aghassemlouei wants to merge 6 commits into
mainfrom
fix/py-unused-local-variables

Conversation

@aghassemlouei

@aghassemlouei aghassemlouei commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Description

Resolves all 16 unused local variables identified across 6 Python files by GitHub CodeQL quality rule py/unused-local-variable, and introduces comprehensive automated testing and CI validation for both Python tools/CLI and Shell scripts across the repository.

CodeQL & Code Quality Remediations:

  1. blueprints/fedramp-high/gemini-enterprise/gem4gov-cli/auth.py:
    • Replaced unused project unpacking with discard variable credentials, _ = google.auth.default(...).
  2. blueprints/fedramp-high/gemini-enterprise/gem4gov-cli/data_stores.py:
    • Removed unused response variable assignment from request.execute() in create_data_store_schema().
  3. blueprints/fedramp-high/gemini-enterprise/gem4gov-cli/gem4gov.py:
    • Removed unused credentials assignment in init().
    • Removed unused response variable assignments from API calls in configure_identity_provider() and configure_cmek().
    • Removed unused eng_response in engine existence check within create_engine().
    • Removed unused inner_e exception binding in create_engine().
    • Removed unused headers dictionaries in configure_idp_for_widget() and disable_user_event_collection().
    • Removed unused engine_response assignments in configure_gemini_enterprise_for_fedramp_high(), configure_gemini_enterprise_for_il4(), and configure_gemini_enterprise_for_il5().
  4. tools/changelog.py:
    • Removed unused exception variable binding as e in main().
  5. tools/state_iam.py:
    • Removed dead local dictionary initialization org_ids = {} in get_bindings().
    • Converted regex string literals to raw strings (r"^service-\\d{8}", r"^bq-\\d{8}") to eliminate Python 3.12+ SyntaxWarning.
  6. tools/tfdoc.py:
    • Replaced unused exception bindings except (IOError, OSError) as e: with except (IOError, OSError): in parse_outputs(), parse_recipes(), and parse_variables().
    • Replaced unused dirnames loop variable with _ in os.walk(module_path).

New Automated Test Suites & CI Pipeline:

  1. Python Unit Tests (tests/):
    • tests/gem4gov_cli/test_auth.py: Tests check_roles permission evaluation via testIamPermissions, get_user_email resolution and error handling, and get_credentials.
    • tests/gem4gov_cli/test_data_stores.py: Tests generate_id format and uniqueness, parse_http_error JSON decoding and fallbacks, and validate_data_store schema validation.
    • tests/tools/test_check_boilerplate.py: Tests Apache 2.0 license boilerplate header detection and exclusions.
    • tests/tools/test_state_iam.py: Tests organization alias mapping and IAM binding resource extraction.
    • tests/tools/test_tfdoc.py: Tests regex pattern matching (COUNT_TEST_RE, TAG_RE) and HCL parsing.
  2. Shell Script Testing (tests/scripts/):
    • tests/scripts/test_shell_syntax.sh: Automated syntax validation (bash -n) across all 27 shell scripts in the repository.
    • tests/scripts/test_common_functions.sh: Unit tests for scripts/common-functions.sh verifying validate_env_vars, logging tags/formatting (log_info, log_warn, log_error, log_debug), and backup_config file operations.
    • tests/scripts/run_shell_tests.sh: Automated shell test orchestrator.
  3. Unified Test Runner:
    • tools/run_tests.sh: Single command running Python unit tests, shell script tests, and AST/CodeQL unused local variable verification.
  4. GitHub Actions CI Workflow (.github/workflows/ci.yml):
    • Added shell-tests job executing run_shell_tests.sh.
    • Added python-tests job setting up Python, running pyflakes (guaranteeing CodeQL quality compliance), and running pytest.

Fixes # N/A (CodeQL rule py/unused-local-variable)

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

Deployment & Compliance Impact

  • Applicable Regimes:
    • US Region Restricted (e.g., Access Policy constraint)
    • FedRAMP Moderate
    • FedRAMP High
    • DoD IL4
    • DoD IL5
    • General / All
  • NIST 800-53r5 Controls:
    • SI-2 (Flaw Remediation / Code Quality)
    • SA-11 (Developer Testing and Evaluation)
    • CA-7 (Continuous Monitoring / Automated CI Verification)

Checklist

Code Quality & Reusability

  • My code adheres to the Maximize Reusability principle. I have not redefined common elements and have reused existing base configurations and modules where possible.
  • I have checked that no existing module or configuration in modules/ or fast/ can be leveraged for this change.
  • My code follows the established naming conventions outlined in documentation/naming-convention.md.

Documentation

  • I have updated the README.md of the modified module or blueprint. (N/A - Internal code quality and test harness addition)
  • I have added/updated documentation for inputs (variables) and outputs. (N/A)

Security

  • My change adheres to GCP security best practices and the principle of least privilege.
  • I have ensured compliance with the targeted regime (FedRAMP Moderate, FedRAMP High, IL5, etc.).

Testing

  • I have tested my changes locally.
  • I have included details of my testing in this PR.

Testing Performed

  1. Python Unit Tests:
    • Executed python3 -m unittest discover -s tests -t . -p "test_*.py" -v: 28 tests ran and passed (0 errors, 0 failures).
  2. Shell Script Tests:
    • Executed bash tests/scripts/run_shell_tests.sh: 27 shell scripts passed syntax checks (bash -n), and 12 unit tests in test_common_functions.sh passed.
  3. Unified Test Suite:
    • Executed bash tools/run_tests.sh: All 3 stages (Python unit tests, Shell test suites, AST/CodeQL compilation checks) passed with 0 errors.
  4. CodeQL py/unused-local-variable Verification:
    • Scanned all modified files with pyflakes and custom AST symbol visitor: 0 unused local variables remaining.

…local-variable)

Removes and cleans up unused local variables across tools and CLI modules
flagged by CodeQL rule py/unused-local-variable:
- auth.py: ignore unused project tuple element from google.auth.default
- data_stores.py: remove unused response assignment in create_data_store_schema
- gem4gov.py: remove unused response, engine_response, headers, and exception variables
- changelog.py: remove unused exception variable in main
- state_iam.py: remove unused org_ids variable in get_bindings
- tfdoc.py: remove unused exception variables and unused dirnames loop variable

Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@users.noreply.github.com>
@aghassemlouei aghassemlouei added bug Something isn't working gemini enterprise Gemini Enterprise (GE) related gemini for government Gemini for Government (G4G) related Level of Effort - Low Quick, well-defined tasks with no unknowns; takes a few hours up to one day to complete Priority - Low Minor polish, typo fixes, or technical debt; nice to have; pick up when time permits labels Sep 6, 2026
Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@google.com>
@aghassemlouei
aghassemlouei enabled auto-merge (rebase) September 6, 2026 18:52
…dation

Adds comprehensive automated testing for Python CLI/tools and shell scripts:
- tests/gem4gov_cli/: unit tests for auth and data_stores (mocking discovery and auth APIs)
- tests/tools/: unit tests for check_boilerplate, state_iam, and tfdoc
- tests/scripts/: automated shell syntax validation (bash -n across all 27 scripts)
  and unit tests for common-functions.sh (env var validation, logging, backups)
- tools/run_tests.sh: unified test runner executing Python unit tests, shell tests,
  and CodeQL unused local variable checks
- .github/workflows/ci.yml: added shell-tests and python-tests jobs to CI pipeline
- tools/state_iam.py: converted regex strings to raw strings to avoid SyntaxWarning

Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@users.noreply.github.com>
Comment thread tests/gem4gov_cli/test_auth.py Fixed
@aghassemlouei aghassemlouei changed the title fix(quality): resolve CodeQL unused local variable issues (py/unused-local-variable) fix(quality): resolve CodeQL unused local variables and add Python & Shell test suites Sep 6, 2026
…on 3.11 compatibility

- .github/workflows/ci.yml: restrict top-level workflow permissions to contents: read,
  scope elevated permissions (pull-requests: write, security-events: write) to the
  security-scan job, and set persist-credentials: false on actions/checkout to satisfy Zizmor
- gem4gov.py: fix f-string quote nesting on lines 400-414 for Python 3.11 compatibility
- data_stores.py, setup.py, check_names.py, check_boilerplate.py, tfdoc.py,
  check_documentation.py: remove unused imports and unneeded f-string prefixes flagged by pyflakes
- tests/__init__.py: use dynamic importlib loading to prevent pyflakes unused import warnings

Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@users.noreply.github.com>
…gem4gov.py

Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@users.noreply.github.com>
…rison in test_auth.py

Resolves CodeQL imprecise assert alert on line 25 of test_auth.py.

Signed-off-by: Alijohn Ghassemlouei <aghassemlouei@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working gemini enterprise Gemini Enterprise (GE) related gemini for government Gemini for Government (G4G) related Level of Effort - Low Quick, well-defined tasks with no unknowns; takes a few hours up to one day to complete Priority - Low Minor polish, typo fixes, or technical debt; nice to have; pick up when time permits

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants