Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/buildkite_test_collector/collector/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..pytest_plugin.logger import logger

from .instant import Instant
from .run_env import RuntimeEnvironment
from .run_env import RunEnv

JsonValue = Union[str, int, float, bool, 'JsonDict', Tuple['JsonValue']]
JsonDict = Dict[str, JsonValue]
Expand Down Expand Up @@ -209,14 +209,14 @@ def as_json(self, started_at: Instant) -> JsonDict:
@dataclass(frozen=True)
class Payload:
"""The full test analytics payload"""
run_env: RuntimeEnvironment
run_env: RunEnv
data: Tuple[TestData]
started_at: Optional[Instant]
finished_at: Optional[Instant]

@classmethod
def init(cls, run_env: RuntimeEnvironment) -> 'Payload':
"""Create a new instance of payload with the provided runtime environment"""
def init(cls, run_env: RunEnv) -> 'Payload':
"""Create a new instance of payload with the provided RunEnv"""

return cls(
run_env=run_env,
Expand Down
24 changes: 12 additions & 12 deletions src/buildkite_test_collector/collector/run_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Runtime environment detection"""
"""Test Engine run_env"""

from dataclasses import dataclass
from typing import Dict, Optional
Expand All @@ -17,13 +17,13 @@ def __get_env(name: str) -> Optional[str]:
return var


def __buildkite_env() -> Optional['RuntimeEnvironment']:
def __buildkite_env() -> Optional['RunEnv']:
build_id = __get_env("BUILDKITE_BUILD_ID")

if build_id is None:
return None

return RuntimeEnvironment(
return RunEnv(
ci="buildkite",
key=build_id,
url=__get_env("BUILDKITE_BUILD_URL"),
Expand All @@ -35,7 +35,7 @@ def __buildkite_env() -> Optional['RuntimeEnvironment']:
)


def __github_actions_env() -> Optional['RuntimeEnvironment']:
def __github_actions_env() -> Optional['RunEnv']:
action = __get_env("GITHUB_ACTION")
run_number = __get_env("GITHUB_RUN_NUMBER")
run_attempt = __get_env("GITHUB_RUN_ATTEMPT")
Expand All @@ -46,7 +46,7 @@ def __github_actions_env() -> Optional['RuntimeEnvironment']:
repo = __get_env("GITHUB_REPOSITORY")
run_id = __get_env("GITHUB_RUN_ID")

return RuntimeEnvironment(
return RunEnv(
ci="github_actions",
key=f"{action}-{run_number}-{run_attempt}",
url=f"https://github.com/{repo}/actions/runs/{run_id}",
Expand All @@ -58,14 +58,14 @@ def __github_actions_env() -> Optional['RuntimeEnvironment']:
)


def __circle_ci_env() -> Optional['RuntimeEnvironment']:
def __circle_ci_env() -> Optional['RunEnv']:
build_num = __get_env("CIRCLE_BUILD_NUM")
workflow_id = __get_env("CIRCLE_WORKFLOW_ID")

if (build_num is None or workflow_id is None):
return None

return RuntimeEnvironment(
return RunEnv(
ci="circleci",
key=f"{workflow_id}-{build_num}",
url=__get_env("CIRCLE_BUILD_URL"),
Expand All @@ -77,8 +77,8 @@ def __circle_ci_env() -> Optional['RuntimeEnvironment']:
)


def __generic_env() -> 'RuntimeEnvironment':
return RuntimeEnvironment(
def __generic_env() -> 'RunEnv':
return RunEnv(
ci="generic",
key=str(uuid4()),
url=None,
Expand All @@ -91,8 +91,8 @@ def __generic_env() -> 'RuntimeEnvironment':


@dataclass(frozen=True)
class RuntimeEnvironment:
"""The detected RuntimeEnvironment"""
class RunEnv:
"""The detected RunEnv"""
ci: str
key: str
number: Optional[str]
Expand Down Expand Up @@ -120,7 +120,7 @@ def as_json(self) -> Dict[str, str]:
return {k: v for k, v in attrs.items() if v is not None}


def detect_env() -> RuntimeEnvironment:
def detect_env() -> RunEnv:
"""Attempt to detect the CI system we're running in"""
return __buildkite_env() or \
__github_actions_env() or \
Expand Down
74 changes: 37 additions & 37 deletions tests/buildkite_test_collector/collector/test_run_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ def test_detect_env_with_buildkite_api_env_vars_returns_the_correct_environment(
"BUILDKITE_MESSAGE": "All we are is dust in the wind, dude.",
}
with mock.patch.dict(os.environ, env, clear=True):
runtime_env = detect_env()
run_env = detect_env()

assert runtime_env.ci == "buildkite"
assert runtime_env.key == id
assert runtime_env.url == "https://example.test/buildkite"
assert runtime_env.branch == "rufus"
assert runtime_env.commit_sha == commit
assert runtime_env.number == number
assert runtime_env.job_id == job_id
assert runtime_env.message == "All we are is dust in the wind, dude."
assert run_env.ci == "buildkite"
assert run_env.key == id
assert run_env.url == "https://example.test/buildkite"
assert run_env.branch == "rufus"
assert run_env.commit_sha == commit
assert run_env.number == number
assert run_env.job_id == job_id
assert run_env.message == "All we are is dust in the wind, dude."


def test_detect_env_with_github_actions_env_vars_returns_the_correct_environment():
Expand All @@ -53,16 +53,16 @@ def test_detect_env_with_github_actions_env_vars_returns_the_correct_environment
}

with mock.patch.dict(os.environ, env, clear=True):
runtime_env = detect_env()
run_env = detect_env()

assert runtime_env.ci == "github_actions"
assert runtime_env.key == f"bring-about-world-peace-{run_number}-{run_attempt}"
assert runtime_env.url == f"https://github.com/bill-and-ted/phone-booth/actions/runs/{run_id}"
assert runtime_env.branch == "rufus"
assert runtime_env.commit_sha == commit
assert runtime_env.number == run_number
assert runtime_env.job_id is None
assert runtime_env.message == "excellent adventure"
assert run_env.ci == "github_actions"
assert run_env.key == f"bring-about-world-peace-{run_number}-{run_attempt}"
assert run_env.url == f"https://github.com/bill-and-ted/phone-booth/actions/runs/{run_id}"
assert run_env.branch == "rufus"
assert run_env.commit_sha == commit
assert run_env.number == run_number
assert run_env.job_id is None
assert run_env.message == "excellent adventure"

def test_detect_env_with_circle_ci_env_vars_returns_the_correct_environment():
build_num = str(randint(0, 1000))
Expand All @@ -79,31 +79,31 @@ def test_detect_env_with_circle_ci_env_vars_returns_the_correct_environment():
}

with mock.patch.dict(os.environ, env, clear=True):
runtime_env = detect_env()
run_env = detect_env()

assert runtime_env.ci == "circleci"
assert runtime_env.key == f"{workflow_id}-{build_num}"
assert runtime_env.url == "https://example.test/circle"
assert runtime_env.branch == "rufus"
assert runtime_env.commit_sha == commit
assert runtime_env.number == build_num
assert runtime_env.job_id is None
assert runtime_env.message == "excellent adventure"
assert run_env.ci == "circleci"
assert run_env.key == f"{workflow_id}-{build_num}"
assert run_env.url == "https://example.test/circle"
assert run_env.branch == "rufus"
assert run_env.commit_sha == commit
assert run_env.number == build_num
assert run_env.job_id is None
assert run_env.message == "excellent adventure"

def test_detect_env_with_generic_env_vars():
env = {}

with mock.patch.dict(os.environ, env, clear=True):
runtime_env = detect_env()

assert runtime_env.ci == "generic"
assert UUID(runtime_env.key)
assert runtime_env.url is None
assert runtime_env.branch is None
assert runtime_env.commit_sha is None
assert runtime_env.number is None
assert runtime_env.job_id is None
assert runtime_env.message is None
run_env = detect_env()

assert run_env.ci == "generic"
assert UUID(run_env.key)
assert run_env.url is None
assert run_env.branch is None
assert run_env.commit_sha is None
assert run_env.number is None
assert run_env.job_id is None
assert run_env.message is None

def test_env_as_json(fake_env):
json = fake_env.as_json()
Expand Down
6 changes: 3 additions & 3 deletions tests/buildkite_test_collector/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from buildkite_test_collector.collector.payload import TestData, TestResultPassed, TestHistory, Payload, TestResultFailed, TestResultSkipped
from buildkite_test_collector.collector.run_env import RuntimeEnvironment
from buildkite_test_collector.collector.run_env import RunEnv
from buildkite_test_collector.collector.instant import Instant


Expand Down Expand Up @@ -72,8 +72,8 @@ def history_finished() -> TestHistory:


@pytest.fixture
def fake_env() -> RuntimeEnvironment:
return RuntimeEnvironment(
def fake_env() -> RunEnv:
return RunEnv(
ci="example",
key=str(uuid4()),
number=str(randint(0, 1000)),
Expand Down