From f274fb9efe7763f8b0744adc320a2a7cf808db07 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 1 Sep 2026 00:10:39 -0400 Subject: [PATCH] refactor: sonar fixes --- .github/workflows/ci.yml | 4 ++-- Dockerfile | 9 +++----- tests/conftest.py | 4 ++-- tests/unit/common/test_crypto.py | 3 +-- tests/unit/common/test_logging_config.py | 3 ++- tests/unit/common/test_webapp.py | 2 +- tests/unit/discord/test_rank_cog.py | 26 ++++++++++-------------- 7 files changed, 22 insertions(+), 29 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 729413e..f9c039c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: - name: Install Python Dependencies env: UV_PYTHON: ${{ steps.setup-python.outputs.python-path }} - run: uv sync --locked --extra dev + run: uv sync --no-build --no-install-project --locked --extra dev - name: Test with pytest id: test @@ -84,7 +84,7 @@ jobs: REDDIT_USERNAME: ${{ vars.REDDIT_USERNAME }} REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }} shell: bash - run: uv run --locked --extra dev pytest tests + run: uv run --no-build --no-sync python -m pytest tests - name: Upload test coverage # any except canceled or skipped diff --git a/Dockerfile b/Dockerfile index a11915c..b0ab314 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,14 +40,11 @@ rm -rf /var/lib/apt/lists/* useradd -m -u 1000 -s /bin/bash supportbot # write the version to the version file -cat > src/common/version.py < src/common/version.py # install python dependencies -uv sync --frozen --no-dev --no-install-project --python python --no-python-downloads +uv sync --frozen --no-build --no-dev --no-install-project --python python --no-python-downloads # set ownership of app and data directories mkdir -p /data diff --git a/tests/conftest.py b/tests/conftest.py index 0f4c548..d6a51d6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -34,7 +34,7 @@ def discord_bot(): globals.DISCORD_BOT = None -@pytest.fixture(scope='function') +@pytest.fixture def discord_db_users(discord_bot): with discord_bot.db as db: users_table = db.table('discord_users') @@ -57,7 +57,7 @@ def discord_db_users(discord_bot): discord_bot.oauth_states.clear() -@pytest.fixture(scope='function') +@pytest.fixture def no_github_token(): og_token = os.getenv('GITHUB_TOKEN') del os.environ['GITHUB_TOKEN'] diff --git a/tests/unit/common/test_crypto.py b/tests/unit/common/test_crypto.py index 293d6bf..0251583 100644 --- a/tests/unit/common/test_crypto.py +++ b/tests/unit/common/test_crypto.py @@ -23,11 +23,10 @@ def setup_certificates(): os.remove(KEY_FILE) -@pytest.fixture(scope='function') +@pytest.fixture def clear_certificates(): os.remove(CERT_FILE) os.remove(KEY_FILE) - yield def test_check_expiration(setup_certificates): diff --git a/tests/unit/common/test_logging_config.py b/tests/unit/common/test_logging_config.py index f91c3fb..cc97b9a 100644 --- a/tests/unit/common/test_logging_config.py +++ b/tests/unit/common/test_logging_config.py @@ -351,7 +351,8 @@ def test_log_format_includes_file_and_line_number(self, tmp_path): log_content = log_file.read_text() # Check format includes file and line number - assert "[" in log_content and "]" in log_content + assert "[" in log_content + assert "]" in log_content assert "test_format" in log_content assert "Test message" in log_content diff --git a/tests/unit/common/test_webapp.py b/tests/unit/common/test_webapp.py index 7c8bf5a..b8746f2 100644 --- a/tests/unit/common/test_webapp.py +++ b/tests/unit/common/test_webapp.py @@ -9,7 +9,7 @@ from src.common import webapp -@pytest.fixture(scope='function') +@pytest.fixture def test_client(): """Create a test client for testing webapp endpoints""" app = webapp.app diff --git a/tests/unit/discord/test_rank_cog.py b/tests/unit/discord/test_rank_cog.py index 2e69c8f..9967986 100644 --- a/tests/unit/discord/test_rank_cog.py +++ b/tests/unit/discord/test_rank_cog.py @@ -51,24 +51,20 @@ async def test_build_leaderboard_embed_discord(mocker): @pytest.mark.asyncio -async def test_build_leaderboard_embed_reddit(mocker): +async def test_build_leaderboard_embed_reddit(mocker, monkeypatch): cog = rank_cog(mocker) - original_reddit_bot = globals.REDDIT_BOT - globals.REDDIT_BOT = SimpleNamespace( + monkeypatch.setattr(globals, 'REDDIT_BOT', SimpleNamespace( subreddit=SimpleNamespace(community_icon='https://example.com/reddit.png'), - ) + )) - try: - embed = await cog.build_leaderboard_embed( - platform='reddit', - leaderboard_data=[{'user_id': 'abc', 'username': 'reddit_user', 'xp': 0}], - page=1, - total_pages=1, - total_users=1, - ctx=SimpleNamespace(guild=SimpleNamespace(icon=None)), - ) - finally: - globals.REDDIT_BOT = original_reddit_bot + embed = await cog.build_leaderboard_embed( + platform='reddit', + leaderboard_data=[{'user_id': 'abc', 'username': 'reddit_user', 'xp': 0}], + page=1, + total_pages=1, + total_users=1, + ctx=SimpleNamespace(guild=SimpleNamespace(icon=None)), + ) assert embed.title == '🏆 Reddit XP Leaderboard' assert '**u/reddit_user**' in embed.description