1
1
from __future__ import annotations
2
2
3
+ import os
4
+ import sys
5
+
3
6
import nox
4
7
8
+ # Control factors for finding pieces of the module
5
9
MODULE_NAME = "module_name"
6
- COVERAGE_FAIL_UNDER = "--fail-under=50"
10
+ TESTS_PATH = "tests"
11
+ COVERAGE_FAIL_UNDER = 50
12
+ REQUIREMENT_IN_FILES = [
13
+ os .path .join ("requirements" , "requirements.in" ),
14
+ os .path .join ("requirements" , "requirements-dev.in" ),
15
+ os .path .join ("requirements" , "requirements-test.in" ),
16
+ ]
17
+
18
+ # What are we allowed to clean (delete)?
19
+ CLEAN_DIRS = [
20
+ "__pycache__" ,
21
+ ".mypy_cache" ,
22
+ ".pytest_cache" ,
23
+ ".coverage" ,
24
+ ".nox" ,
25
+ "dist" ,
26
+ "build" ,
27
+ ]
28
+ CLEAN_FILES = [
29
+ "*.pyc" ,
30
+ "*.pyo" ,
31
+ "coverage.json" ,
32
+ ".coverage.*" ,
33
+ ]
34
+
35
+
36
+ # Define the default sessions run when `nox` is called on the CLI
37
+ nox .options .sessions = [
38
+ "tests_with_coverage" ,
39
+ "coverage_combine_and_report" ,
40
+ "mypy_check" ,
41
+ ]
7
42
8
43
9
44
@nox .session (
@@ -14,17 +49,19 @@ def tests_with_coverage(session: nox.Session) -> None:
14
49
print_standard_logs (session )
15
50
16
51
session .install (".[test]" )
17
- session .run ("coverage" , "run" , "-p" , "-m" , "pytest" , "tests/" )
52
+ session .run ("coverage" , "run" , "-p" , "-m" , "pytest" , TESTS_PATH )
18
53
19
54
20
55
@nox .session ()
21
56
def coverage_combine_and_report (session : nox .Session ) -> None :
22
57
"""Combine all coverage partial files and generate JSON report."""
23
58
print_standard_logs (session )
24
59
60
+ fail_under = f"--fail-under={ COVERAGE_FAIL_UNDER } "
61
+
25
62
session .install (".[test]" )
26
63
session .run ("python" , "-m" , "coverage" , "combine" )
27
- session .run ("python" , "-m" , "coverage" , "report" , "-m" , COVERAGE_FAIL_UNDER )
64
+ session .run ("python" , "-m" , "coverage" , "report" , "-m" , fail_under )
28
65
session .run ("python" , "-m" , "coverage" , "json" )
29
66
30
67
@@ -38,6 +75,69 @@ def mypy_check(session: nox.Session) -> None:
38
75
session .run ("mypy" , "-p" , MODULE_NAME , "--no-incremental" )
39
76
40
77
78
+ @nox .session (python = False )
79
+ def coverage (session : nox .Session ) -> None :
80
+ """Generate a coverage report. Does not use a venv."""
81
+ session .run ("coverage" , "erase" )
82
+ session .run ("coverage" , "run" , "-m" , "pytest" , TESTS_PATH )
83
+ session .run ("coverage" , "report" , "-m" )
84
+
85
+
86
+ @nox .session (python = False )
87
+ def docker (session : nox .Session ) -> None :
88
+ """Run tests in a docker container. Requires docker damon running."""
89
+ session .run ("docker" , "build" , "-t" , "pydocker-test" , "." )
90
+ session .run ("docker" , "run" , "-it" , "--rm" , "pydocker-test" )
91
+
92
+
93
+ @nox .session ()
94
+ def build (session : nox .Session ) -> None :
95
+ """Build distrobution files."""
96
+ print_standard_logs (session )
97
+
98
+ session .install ("build" )
99
+ session .run ("python" , "-m" , "build" )
100
+
101
+
102
+ @nox .session ()
103
+ def update (session : nox .Session ) -> None :
104
+ """Process requirement*.in files, updating only additions/removals."""
105
+ print_standard_logs (session )
106
+
107
+ session .install ("pip-tools" )
108
+ for filename in REQUIREMENT_IN_FILES :
109
+ session .run ("pip-compile" , "--no-emit-index-url" , filename )
110
+
111
+
112
+ @nox .session ()
113
+ def upgrade (session : nox .Session ) -> None :
114
+ """Process requirement*.in files and upgrade all libraries as possible."""
115
+ print_standard_logs (session )
116
+
117
+ session .install ("pip-tools" )
118
+ for filename in REQUIREMENT_IN_FILES :
119
+ session .run ("pip-compile" , "--no-emit-index-url" , "--upgrade" , filename )
120
+
121
+
122
+ @nox .session (python = False )
123
+ def clean (session : nox .Session ) -> None :
124
+ """Clean cache, .pyc, .pyo, and artifact files from project."""
125
+ if sys .platform .startswith ("win" ):
126
+ print ("Windows unsupported at this time." )
127
+ return None
128
+
129
+ elif sys .platform in ["linux" , "darwin" ]:
130
+ for dirpath in CLEAN_DIRS :
131
+ session .run ("find" , "." , "-name" , dirpath , "-exec" , "rm" , "-rf" , "{}" , "+" )
132
+
133
+ for file in CLEAN_FILES :
134
+ session .run ("find" , "." , "-name" , file , "-exec" , "rm" , "-f" , "{}" , "+" )
135
+
136
+ else :
137
+ print ("Unknown OS for this session." )
138
+ return None
139
+
140
+
41
141
def print_standard_logs (session : nox .Session ) -> None :
42
142
"""Reusable output for monitoring environment factors."""
43
143
version = session .run ("python" , "--version" , silent = True )
0 commit comments