Skip to content

Commit 8b0a4af

Browse files
committed
Merge branch '4-add-pytest' into 'master'
Resolve "Add pytest" Closes #4 See merge request ShellLogger/ShellLogger!5
2 parents 06c7381 + 6a80d00 commit 8b0a4af

File tree

8 files changed

+226
-14
lines changed

8 files changed

+226
-14
lines changed

.coveragerc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[run]
2+
omit =
3+
*/tests/*
4+
*/venv*
5+
branch = True
6+
dynamic_context = test_function
7+
8+
[paths]
9+
source = shelllogger
10+
11+
[report]
12+
skip_covered = False
13+
fail_under = 85
14+
show_missing = True
15+
exclude_lines =
16+
pragma: no cover
17+
if 0:
18+
if False:
19+
20+
[html]
21+
directory = tests/htmlcov
22+
title = ShellLogger Coverage Report
23+
show_contexts = True
24+
25+
[xml]
26+
output = tests/coverage.xml
27+
28+
[json]
29+
output = tests/coverage.json
30+
pretty_print = True
31+
show_contexts = True

.gitlab-ci.yml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# This file controls the GitLab CI pipeline for the `ShellLogger`
2+
# project. See here for the keyword reference:
3+
# https://docs.gitlab.com/ee/ci/yaml/.
4+
5+
6+
#image: alpine:latest
7+
8+
9+
#variables:
10+
#HTTP_PROXY: http://wwwproxy.sandia.gov:80
11+
#HTTPS_PROXY: http://wwwproxy.sandia.gov:80
12+
#NO_PROXY: 127.0.0.1,localhost,.sandia.gov,/var/run/docker.sock
13+
14+
15+
# Set up a global cache to pass information across stages.
16+
cache: &global_cache
17+
key: "${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
18+
paths:
19+
- tests/htmlcov/
20+
- venv-clean-python/
21+
policy: pull-push
22+
23+
24+
# Set up the pipeline stages.
25+
stages:
26+
- prepare
27+
- test
28+
- deploy
29+
- examples
30+
- documentation
31+
- publish
32+
- finish
33+
34+
35+
# Run this before every job.
36+
before_script:
37+
- |-
38+
export LSB_RELEASE=$(which lsb_release)
39+
if [ -x ${LSB_RELEASE} ]; then
40+
${LSB_RELEASE} -a
41+
fi
42+
- |-
43+
if [ -d "venv-clean-python" ]; then
44+
source venv-clean-python/bin/activate
45+
else
46+
echo "Virtual Environment 'venv-clean-python' was not found"
47+
exit 1
48+
fi
49+
echo "-----"
50+
echo "VIRTUAL_ENV = ${VIRTUAL_ENV:?}"
51+
echo "-----"
52+
53+
54+
# Run this after every job.
55+
after_script:
56+
- |-
57+
if [ ! -z ${VIRTUAL_ENV} ]; then
58+
deactivate
59+
fi
60+
61+
62+
# Create the virtual environment, install the requirements, and save the
63+
# environment to the cache.
64+
Install Requirements:
65+
stage: prepare
66+
timeout: 10m
67+
before_script:
68+
- python3 -m venv venv-clean-python
69+
cache:
70+
<<: *global_cache
71+
when: on_success
72+
script:
73+
- source venv-clean-python/bin/activate
74+
75+
# Remove shelllogger if it's been installed.
76+
- python3 -m pip uninstall -y shelllogger
77+
78+
# Install required packages.
79+
- python3 -m pip install wheel
80+
-r requirements.txt
81+
-r tests/requirements.txt
82+
-r doc/requirements.txt
83+
84+
85+
# Execute the unit tests.
86+
pytest:
87+
stage: test
88+
needs: ["Install Requirements"]
89+
timeout: 5m
90+
cache:
91+
<<: *global_cache
92+
script:
93+
- python3 -m pytest
94+
--cov=src.shelllogger
95+
--cov-config=.coveragerc
96+
--cov-report=html
97+
--cov-report=term
98+
--cov-report=xml
99+
--full-trace
100+
tests/
101+
coverage: '/TOTAL\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*[0-9]*\s*(\d+%)/'
102+
artifacts:
103+
reports:
104+
cobertura: tests/coverage.xml
105+
junit: tests/coverage.xml
106+
107+
108+
# Test building a distribution.
109+
Build Distribution:
110+
stage: deploy
111+
needs: ["Install Requirements"]
112+
timeout: 5m
113+
cache:
114+
<<: *global_cache
115+
policy: pull
116+
script:
117+
- python3 -m pip wheel --no-deps -w dist .
118+
artifacts:
119+
name: "shelllogger-dist"
120+
paths:
121+
- dist/shelllogger*.whl
122+
expire_in: 6 weeks
123+
124+
125+
# Test installation of the package.
126+
Install Package:
127+
stage: deploy
128+
needs: ["Install Requirements"]
129+
timeout: 5m
130+
cache:
131+
<<: *global_cache
132+
script:
133+
- python3 -m pip install .
134+
135+
136+
# Publish coverage data (if on the main branch).
137+
Pages:
138+
stage: publish
139+
needs: ["pytest"]
140+
timeout: 5m
141+
cache:
142+
<<: *global_cache
143+
policy: pull
144+
rules:
145+
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
146+
script:
147+
- mkdir -p public
148+
- mv tests/htmlcov public/.
149+
artifacts:
150+
paths:
151+
- public
152+
153+
154+
# Test that uninstalling from a virtual environment works.
155+
Uninstall Package:
156+
stage: finish
157+
timeout: 5m
158+
cache:
159+
<<: *global_cache
160+
script:
161+
- python3 -m pip uninstall -y shelllogger

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![pipeline status](https://internal.gitlab.server/ShellLogger/ShellLogger/badges/master/pipeline.svg)](https://internal.gitlab.server/ShellLogger/ShellLogger/pipelines)
2+
[![coverage report](https://internal.gitlab.server/ShellLogger/ShellLogger/badges/master/coverage.svg)](http://10.202.36.47:8080/ShellLogger/coverage/index.html)
3+
14
# ShellLogger
25

36
FILL THIS OUT

doc/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Requirements for building the `ShellLogger` documentation.
2+
3+
sphinx
4+
sphinx-argparse
5+
sphinx-rtd-theme

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ classifiers = [
3131
"Topic :: System :: Logging",
3232
"Topic :: System :: Shells",
3333
"Topic :: System :: System Shells",
34-
# "Typing :: Typed" UNCOMMENT AFTER ADDING TYPE HINTS
34+
"Typing :: Typed"
3535
]
3636
packages = [
3737
{ include = "shelllogger", from = "src" },

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Main requirements for `ShellLogger`.

tests/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Requirements for testing `ShellLogger`.
2+
3+
mock >= 4
4+
psutil
5+
pytest >= 6.2
6+
pytest-cov >= 2.12
7+
pytest-mock >= 3.6

tests/test_ShellLogger.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import json
55
import os
66
from pathlib import Path
7-
import psutil
87
import pytest
98
import re
109
from src.shelllogger import ShellLogger, ShellLoggerDecoder
10+
try:
11+
import psutil
12+
except ModuleNotFoundError:
13+
psutil = None
1114

1215

1316
@pytest.fixture(autouse=True)
@@ -412,6 +415,7 @@ def test_syntax_error() -> None:
412415
assert "There was a problem running the command" in excinfo.value.args[0]
413416

414417

418+
@pytest.mark.skipif(psutil is None, reason="`psutil` is unavailable")
415419
def test_logger_does_not_store_stdout_string_by_default() -> None:
416420
"""
417421
Ensure we don't hold a commands ``stdout`` in memory by default.
@@ -640,12 +644,12 @@ def test_trace_and_stats() -> None:
640644
expression="setlocale", summary=True)
641645
assert "setlocale" in result.trace
642646
assert "sleep" not in result.trace
643-
assert len(result.stats["memory"]) > 8
644-
assert len(result.stats["memory"]) < 30
645-
assert len(result.stats["cpu"]) > 8
646-
assert len(result.stats["cpu"]) < 30
647-
assert len(result.stats["disk"]["/"]) > 8
648-
assert len(result.stats["disk"]["/"]) < 30
647+
assert len(result.stats["memory"]) > 5
648+
assert len(result.stats["memory"]) < 50
649+
assert len(result.stats["cpu"]) > 5
650+
assert len(result.stats["cpu"]) < 50
651+
assert len(result.stats["disk"]["/"]) > 5
652+
assert len(result.stats["disk"]["/"]) < 50
649653
else:
650654
print(f"Warning: uname is not 'Linux': {os.uname()}; ltrace not "
651655
"tested.")
@@ -697,12 +701,12 @@ def test_log_book_trace_and_stats() -> None:
697701
trace="ltrace", expression="setlocale", summary=True)
698702
assert "setlocale" in logger.log_book[0]["trace"]
699703
assert "sleep" not in logger.log_book[0]["trace"]
700-
assert len(logger.log_book[0]["stats"]["memory"]) > 8
701-
assert len(logger.log_book[0]["stats"]["memory"]) < 30
702-
assert len(logger.log_book[0]["stats"]["cpu"]) > 8
703-
assert len(logger.log_book[0]["stats"]["cpu"]) < 30
704-
assert len(logger.log_book[0]["stats"]["disk"]["/"]) > 8
705-
assert len(logger.log_book[0]["stats"]["disk"]["/"]) < 30
704+
assert len(logger.log_book[0]["stats"]["memory"]) > 5
705+
assert len(logger.log_book[0]["stats"]["memory"]) < 50
706+
assert len(logger.log_book[0]["stats"]["cpu"]) > 5
707+
assert len(logger.log_book[0]["stats"]["cpu"]) < 50
708+
assert len(logger.log_book[0]["stats"]["disk"]["/"]) > 5
709+
assert len(logger.log_book[0]["stats"]["disk"]["/"]) < 50
706710
else:
707711
print(f"Warning: uname is not 'Linux': {os.uname()}; ltrace not "
708712
"tested.")

0 commit comments

Comments
 (0)