pytest-html-plus 0.4.2__tar.gz → 0.4.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pytest-html-plus
3
- Version: 0.4.2
3
+ Version: 0.4.3
4
4
  Summary: Get started with rich pytest reports in under 3 seconds. Just install the plugin — no setup required. The simplest, fastest reporter for pytest.
5
5
  License: MIT
6
6
  Keywords: pytest,pytest-html-plus,pytest-plugin,html-test-report,beautiful-test-report,shareable-test-results,test-report,test-results,unit-test-report,functional-test-report,test-summary,reporting,python-testing,automated-testing,test-runner,report-generator,continuous-integration,ci-cd,github-actions,jenkins,pytest-html,pytest-report
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pytest-html-plus"
3
- version = "0.4.2"
3
+ version = "0.4.3"
4
4
  description = "Get started with rich pytest reports in under 3 seconds. Just install the plugin — no setup required. The simplest, fastest reporter for pytest."
5
5
  readme = "README.md"
6
6
  authors = ["reporterplus"]
@@ -0,0 +1,99 @@
1
+ import os
2
+ import threading
3
+ import subprocess
4
+ from typing import Tuple, Optional
5
+
6
+ _CACHE_LOCK = threading.Lock()
7
+ _CACHED_REPO_INFO: Optional[Tuple[str, str]] = None
8
+
9
+ # Env vars we treat as authoritative when present (CI) — cheap checks
10
+ _CI_BRANCH_VARS = [
11
+ "GITHUB_HEAD_REF", "GITHUB_REF_NAME",
12
+ "CI_COMMIT_REF_NAME", "BITBUCKET_BRANCH",
13
+ "BUILD_SOURCEBRANCHNAME", "CIRCLE_BRANCH",
14
+ "BRANCH_NAME", "TRAVIS_BRANCH", "GIT_BRANCH"
15
+ ]
16
+ _CI_COMMIT_VARS = [
17
+ "GITHUB_SHA", "CI_COMMIT_SHA", "BITBUCKET_COMMIT",
18
+ "BUILD_SOURCEVERSION", "CIRCLE_SHA1", "TRAVIS_COMMIT"
19
+ ]
20
+
21
+ def _run_git(cmd: list, timeout: float = 1.0) -> Optional[str]:
22
+ try:
23
+ out = subprocess.check_output(cmd, stderr=subprocess.DEVNULL, timeout=timeout)
24
+ return out.decode().strip()
25
+ except Exception:
26
+ return None
27
+
28
+ def get_repo_info() -> Tuple[str, str]:
29
+ """
30
+ Return (branch, commit_sha).
31
+ - Branch is a readable branch name or "NA".
32
+ - Commit is full SHA or "NA".
33
+ Behavior:
34
+ 1) If REPORTER_BRANCH / REPORTER_COMMIT provided -> use them (manual override).
35
+ 2) Else read common CI env vars (cheap).
36
+ 3) Else call git once to get commit, and try to get branch (symbolic-ref / points-at).
37
+ Result is cached per-process.
38
+ """
39
+ global _CACHED_REPO_INFO
40
+
41
+ # 0) Manual overrides (explicit, highest priority) - zero cost
42
+ manual_branch = os.getenv("REPORTER_BRANCH")
43
+ manual_commit = os.getenv("REPORTER_COMMIT")
44
+ if manual_branch or manual_commit:
45
+ branch = manual_branch or "NA"
46
+ commit = manual_commit or _run_git(["git", "rev-parse", "HEAD"]) or "NA"
47
+ with _CACHE_LOCK:
48
+ _CACHED_REPO_INFO = (branch, commit)
49
+ return _CACHED_REPO_INFO
50
+
51
+ # 1) CI envs (cheap, no subprocess)
52
+ for var in _CI_COMMIT_VARS:
53
+ val = os.getenv(var)
54
+ if val:
55
+ commit = val
56
+ # try branch envs (if any)
57
+ branch = next((os.getenv(bv) for bv in _CI_BRANCH_VARS if os.getenv(bv)), "NA")
58
+ with _CACHE_LOCK:
59
+ _CACHED_REPO_INFO = (branch, commit)
60
+ return _CACHED_REPO_INFO
61
+
62
+ for var in _CI_BRANCH_VARS:
63
+ val = os.getenv(var)
64
+ if val:
65
+ # branch available but commit not set in CI envs -> get commit cheaply
66
+ commit = _run_git(["git", "rev-parse", "HEAD"]) or "NA"
67
+ with _CACHE_LOCK:
68
+ _CACHED_REPO_INFO = (val, commit)
69
+ return _CACHED_REPO_INFO
70
+
71
+ # 2) Return cached if already computed
72
+ with _CACHE_LOCK:
73
+ if _CACHED_REPO_INFO is not None:
74
+ return _CACHED_REPO_INFO
75
+
76
+ # 3) Final: use git (one or two quick calls), cached afterwards
77
+ commit = _run_git(["git", "rev-parse", "HEAD"]) or "NA"
78
+ branch = _run_git(["git", "symbolic-ref", "--short", "-q", "HEAD"])
79
+
80
+ # If symbolic-ref didn't return and commit is present, try local branches pointing to HEAD
81
+ if not branch and commit != "NA":
82
+ branches = _run_git(["git", "branch", "--points-at", "HEAD", "--format=%(refname:short)"])
83
+ if branches:
84
+ branch = branches.splitlines()[0].strip()
85
+
86
+ if not branch:
87
+ branch = "NA"
88
+
89
+ with _CACHE_LOCK:
90
+ _CACHED_REPO_INFO = (branch, commit)
91
+ return _CACHED_REPO_INFO
92
+
93
+ def display_repo_ref(short_len: int = 7) -> str:
94
+ """Return a friendly display like 'feature/foo (5bb4c87)' or '5bb4c87'."""
95
+ branch, commit = get_repo_info()
96
+ commit_short = commit[:short_len] if commit and commit != "NA" else "NA"
97
+ if branch and branch != "NA":
98
+ return f"{branch} ({commit_short})"
99
+ return commit_short
@@ -1,18 +1,19 @@
1
1
  import json
2
2
  from datetime import datetime
3
3
 
4
- from pytest_html_plus.utils import is_main_worker, get_env_marker, get_git_branch, get_git_commit, get_report_title, \
4
+ from pytest_html_plus.utils import is_main_worker, get_env_marker, get_git_information, get_report_title, \
5
5
  get_python_version
6
6
 
7
7
 
8
8
  def write_plus_metadata_if_main_worker(config, report_path, output_path="plus_metadata.json"):
9
9
  if not is_main_worker():
10
10
  return
11
+ branch, commit = get_git_information()
11
12
  metadata = {
12
13
  "report_title": get_report_title(output_path=report_path),
13
14
  "environment": get_env_marker(config),
14
- "branch": get_git_branch(),
15
- "commit": get_git_commit(),
15
+ "branch": branch,
16
+ "commit": commit,
16
17
  "python_version": get_python_version(),
17
18
  "generated_at": datetime.now().isoformat()
18
19
  }
@@ -3,31 +3,11 @@ import subprocess
3
3
  import shutil
4
4
  import os
5
5
 
6
+ from pytest_html_plus.compute_git_branch import get_repo_info
6
7
 
7
- def get_git_commit():
8
- try:
9
- return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
10
- except Exception:
11
- return "NA"
12
-
13
- def get_git_branch():
14
- branch_env_vars = [
15
- "GITHUB_HEAD_REF", "GITHUB_REF_NAME",
16
- "CI_COMMIT_REF_NAME", "BITBUCKET_BRANCH",
17
- "BUILD_SOURCEBRANCHNAME", "CIRCLE_BRANCH",
18
- "BRANCH_NAME", "TRAVIS_BRANCH"
19
- ]
20
- for var in branch_env_vars:
21
- val = os.getenv(var)
22
- if val:
23
- return val
24
8
 
25
- try:
26
- return subprocess.check_output(
27
- ["git", "rev-parse", "--abbrev-ref", "HEAD"]
28
- ).decode().strip()
29
- except Exception:
30
- return "NA"
9
+ def get_git_information():
10
+ return get_repo_info()
31
11
 
32
12
  def get_env_marker(config):
33
13
  for arg in ("--env", "--environment"):