from urllib.error import HTTPError from urllib.parse import parse_qs, urlsplit import pytest def fake(plugin, monkeypatch, data): calls = [] def fetch(url, **kwargs): calls.append(url) return data monkeypatch.setattr(plugin, "fetch_json", fetch) return calls @pytest.mark.parametrize( "state,color", [ ("success", "#2da44e"), ("failure", "#e05d44"), ("timed_out", "#e05d44"), ("cancelled", "#6a737d"), ("skipped", "#6a737d"), ("neutral", "#6a737d"), (None, "#6a737d"), ("future_state", "#6a737d"), ], ) def test_completed_conclusions(plugin, monkeypatch, state, color): fake( plugin, monkeypatch, {"workflow_runs": [{"status": "completed", "conclusion": state}]}, ) result = plugin.run("org/repo") assert result.status == (state or "unknown").replace("_", " ") assert result.secondary_color == color @pytest.mark.parametrize( "state", ["queued", "in_progress", "waiting", "pending", "requested"] ) def test_unfinished_is_not_success(plugin, monkeypatch, state): fake( plugin, monkeypatch, {"workflow_runs": [{"status": state, "conclusion": None}]} ) result = plugin.run("org/repo") assert result.status == state.replace("_", " ") assert result.secondary_color == "#dfb317" @pytest.mark.parametrize( "state", [ "success", "failed", "canceled", "skipped", "running", "manual", "waiting_for_resource", "scheduled", ], ) def test_gitlab_states(plugin, monkeypatch, state): fake(plugin, monkeypatch, [{"status": state}]) assert plugin.run("org/repo", "gitlab").status == state.replace("_", " ") def test_workflow_and_branch_escaping(plugin, monkeypatch): calls = fake(plugin, monkeypatch, {"workflow_runs": []}) result = plugin.run("org/repo", workflow="build/test.yml", branch="feature/a&b") assert result.status == "no runs" assert "/workflows/build%2Ftest.yml/runs?" in calls[0] assert parse_qs(urlsplit(calls[0]).query) == { "per_page": ["1"], "branch": ["feature/a&b"], } def test_gitlab_latest_order_and_nested_project(plugin, monkeypatch): calls = fake(plugin, monkeypatch, []) assert plugin.run("org/sub/repo", "gitlab", branch="main").status == "no runs" assert "/projects/org%2Fsub%2Frepo/pipelines?" in calls[0] assert parse_qs(urlsplit(calls[0]).query) == { "per_page": ["1"], "order_by": ["id"], "sort": ["desc"], "ref": ["main"], } @pytest.mark.parametrize( "data", [ {}, {"workflow_runs": None}, [], {"workflow_runs": [42]}, {"workflow_runs": [{"status": 42}]}, ], ) def test_malformed_is_not_no_runs(plugin, monkeypatch, data): fake(plugin, monkeypatch, data) with pytest.raises(ValueError): plugin.run("org/repo") @pytest.mark.parametrize( "repository,provider,workflow", [ ("../repo", "github", ""), ("org/repo", "gitlab", "ci.yml"), ("org/repo", "unknown", ""), ("org/sub/repo", "github", ""), ], ) def test_bad_arguments_before_fetch( plugin, monkeypatch, repository, provider, workflow ): calls = fake(plugin, monkeypatch, []) with pytest.raises(ValueError): plugin.run(repository, provider, workflow) assert calls == [] def test_error_propagates(plugin, monkeypatch): def fail(*args, **kwargs): raise HTTPError("https://gitlab.com", 429, "rate limited", {}, None) monkeypatch.setattr(plugin, "fetch_json", fail) with pytest.raises(HTTPError): plugin.run("org/repo", "gitlab") @pytest.mark.parametrize("state", ["success", "failure", "running", "future_state"]) def test_github_conclusion_in_status_field_is_not_trusted(plugin, monkeypatch, state): fake( plugin, monkeypatch, {"workflow_runs": [{"status": state, "conclusion": "success"}]}, ) result = plugin.run("org/repo") assert result.status == state.replace("_", " ") assert result.secondary_color == "#6a737d"