Skip to content

Abstract os.environ so real env doesn't interfere with tests#61

Merged
pda merged 1 commit intomainfrom
abstract-os-env
Jul 18, 2025
Merged

Abstract os.environ so real env doesn't interfere with tests#61
pda merged 1 commit intomainfrom
abstract-os-env

Conversation

@pda
Copy link
Member

@pda pda commented Jul 17, 2025

Two parts of this collector need to access environment variables:

  • RunEnv uses it to discover & construct run_env (CI environment details e.g. build message)
  • submit() uses it to get API key/URL.

Previously we were reaching into os.environ throughout the code, and mocking it in tests like this:

with mock.patch.dict(os.environ, {"CI": "true", "BUILDKITE_ANALYTICS_TOKEN": ""}):

Now, we wrap those environment accesses behind a Mapping[str, Optional[str]] type; real code passes in os.environ, and tests pass in an arbitrary dict.

Diff best viewed with whitespace ignored, there's quite a bit of indentation change from removing the with mock…: blocks.

Related:

@pda pda requested review from a team and Copilot July 17, 2025 12:51
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR abstracts direct os.environ accesses into injectable mappings for both the CI environment detection and API client, allowing real code to use os.environ and tests to pass controlled dictionaries.

  • Introduces RunEnvBuilder to encapsulate environment‐based CI detection and removes the old detect_env function.
  • Adds an API class wrapping the previous submit function, taking an env mapping in its constructor.
  • Refactors tests and the pytest plugin to use the new builder and API classes instead of patching os.environ directly.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/buildkite_test_collector/collector/test_run_env.py Refactored calls to use RunEnvBuilder instead of detect_env
tests/buildkite_test_collector/collector/test_payload.py Changed import to only bring in timedelta
tests/buildkite_test_collector/collector/test_api.py Swapped detect_env/submit for RunEnvBuilder/API calls
src/buildkite_test_collector/pytest_plugin/init.py Updated plugin to build RunEnv via builder and submit via API
src/buildkite_test_collector/collector/run_env.py Added RunEnvBuilder, removed standalone detect_env
src/buildkite_test_collector/collector/api.py Replaced free submit with an API class
Comments suppressed due to low confidence (1)

tests/buildkite_test_collector/collector/test_payload.py:1

  • The test still references datetime but it was removed from the import. Re-add datetime alongside timedelta (e.g. from datetime import datetime, timedelta).
from datetime import timedelta

Base automatically changed from rename-runtime-environment-to-run-env to main July 18, 2025 01:34
@pda pda merged commit 390c3ae into main Jul 18, 2025
@pda pda deleted the abstract-os-env branch July 18, 2025 01:40
@pda pda mentioned this pull request Jul 21, 2025
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.

2 participants