11"""Nox sessions."""
2- import hashlib
32import shutil
43import sys
54from pathlib import Path
65from textwrap import dedent
76
87import nox
8+ import nox_poetry .patch
99from nox .sessions import Session
1010
1111
1212package = "humanizer_portugues"
13- # python_versions = ["3.9", "3.8", "3.7"]
14- python_versions = ["3.8" , "3.7" ]
13+ python_versions = ["3.9" , "3.8" , "3.7" ]
1514nox .options .sessions = (
1615 "pre-commit" ,
1716 "safety" ,
2221)
2322
2423
25- class Poetry :
26- """Helper class for invoking Poetry inside a Nox session.
27-
28- Attributes:
29- session: The Session object.
30- """
31-
32- def __init__ (self , session : Session ) -> None :
33- """Constructor."""
34- self .session = session
35-
36- def export (self , path : Path , * , dev : bool ) -> None :
37- """Export the lock file to requirements format.
38-
39- Args:
40- path: The destination path.
41- dev: If True, include development dependencies.
42- """
43- options = ["--dev" ] if dev else []
44- self .session .run (
45- "poetry" ,
46- "export" ,
47- "--format=requirements.txt" ,
48- f"--output={ path } " ,
49- * options ,
50- external = True ,
51- )
52-
53- def build (self , * args : str ) -> str :
54- """Build the package.
55-
56- Args:
57- args: Command-line arguments for ``poetry build``.
58-
59- Returns:
60- The basename of the wheel built by Poetry.
61- """
62- output = self .session .run (
63- "poetry" , "build" , * args , external = True , silent = True , stderr = None
64- )
65- assert isinstance (output , str ) # noqa: S101
66- return output .split ()[- 1 ]
67-
68-
69- def export_requirements (session : Session , * , dev : bool ) -> Path :
70- """Export the lock file to requirements format.
71-
72- Args:
73- session: The Session object.
74- dev: If True, include development dependencies.
75-
76- Returns:
77- The path to the requirements file.
78- """
79- tmpdir = Path (session .create_tmp ())
80- name = "dev-requirements.txt" if dev else "requirements.txt"
81- path = tmpdir / name
82- hashfile = tmpdir / f"{ name } .hash"
83-
84- lockdata = Path ("poetry.lock" ).read_bytes ()
85- digest = hashlib .blake2b (lockdata ).hexdigest ()
86-
87- if not hashfile .is_file () or hashfile .read_text () != digest :
88- Poetry (session ).export (path , dev = dev )
89- hashfile .write_text (digest )
90-
91- return path
92-
93-
94- def install_package (session : Session ) -> None :
95- """Build and install the package.
96-
97- Build a wheel from the package, and install it into the virtual environment
98- of the specified Nox session.
99-
100- The package requirements are installed using the versions specified in
101- Poetry's lock file.
102-
103- Args:
104- session: The Session object.
105- """
106- poetry = Poetry (session )
107- wheel = poetry .build ("--format=wheel" )
108- requirements = export_requirements (session , dev = False )
109-
110- session .install (f"--requirement={ requirements } " )
111- session .install ("--no-deps" , "--force-reinstall" , f"dist/{ wheel } " )
112-
113-
114- def install (session : Session , * args : str ) -> None :
115- """Install development dependencies into the session's virtual environment.
116-
117- This function is a wrapper for nox.sessions.Session.install.
118-
119- The packages must be managed as development dependencies in Poetry.
120-
121- Args:
122- session: The Session object.
123- args: Command-line arguments for ``pip install``.
124- """
125- requirements = export_requirements (session , dev = True )
126- session .install (f"--constraint={ requirements } " , * args )
127-
128-
12924def activate_virtualenv_in_precommit_hooks (session : Session ) -> None :
13025 """Activate virtualenv in hooks installed by pre-commit.
13126
@@ -177,12 +72,11 @@ def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
17772 hook .write_text ("\n " .join (lines ))
17873
17974
180- @nox .session (name = "pre-commit" , python = "3.8 " )
75+ @nox .session (name = "pre-commit" , python = "3.9 " )
18176def precommit (session : Session ) -> None :
18277 """Lint using pre-commit."""
18378 args = session .posargs or ["run" , "--all-files" , "--show-diff-on-failure" ]
184- install (
185- session ,
79+ session .install (
18680 "black" ,
18781 "darglint" ,
18882 "flake8" ,
@@ -200,20 +94,20 @@ def precommit(session: Session) -> None:
20094 activate_virtualenv_in_precommit_hooks (session )
20195
20296
203- @nox .session (python = "3.8 " )
97+ @nox .session (python = "3.9 " )
20498def safety (session : Session ) -> None :
20599 """Scan dependencies for insecure packages."""
206- install (session , "safety" )
207- requirements = export_requirements ( session , dev = True )
100+ requirements = nox_poetry . export_requirements (session )
101+ session . install ( "safety" )
208102 session .run ("safety" , "check" , f"--file={ requirements } " , "--bare" )
209103
210104
211105@nox .session (python = python_versions )
212106def mypy (session : Session ) -> None :
213107 """Type-check using mypy."""
214108 args = session .posargs or ["src" , "tests" , "docs/conf.py" ]
215- install_package ( session )
216- install (session , "mypy " )
109+ session . install ( "." )
110+ session . install ("mypy" , "pytest " )
217111 session .run ("mypy" , * args )
218112 if not session .posargs :
219113 session .run ("mypy" , f"--python-executable={ sys .executable } " , "noxfile.py" )
@@ -222,12 +116,13 @@ def mypy(session: Session) -> None:
222116@nox .session (python = python_versions )
223117def tests (session : Session ) -> None :
224118 """Run the test suite."""
225- install_package ( session )
226- install (session , "coverage[toml]" , "pygments " , "pytest " )
119+ session . install ( "." )
120+ session . install ("coverage[toml]" , "pytest " , "pygments " )
227121 try :
228122 session .run ("coverage" , "run" , "--parallel" , "-m" , "pytest" , * session .posargs )
229123 finally :
230- session .notify ("coverage" )
124+ if session .interactive :
125+ session .notify ("coverage" )
231126
232127
233128@nox .session
@@ -237,7 +132,7 @@ def coverage(session: Session) -> None:
237132 has_args = session .posargs and len (session ._runner .manifest ) == 1
238133 args = session .posargs if has_args else ["report" ]
239134
240- install (session , "coverage[toml]" )
135+ session . install ("coverage[toml]" )
241136
242137 if not has_args and any (Path ().glob (".coverage.*" )):
243138 session .run ("coverage" , "combine" )
@@ -248,26 +143,26 @@ def coverage(session: Session) -> None:
248143@nox .session (python = python_versions )
249144def typeguard (session : Session ) -> None :
250145 """Runtime type checking using Typeguard."""
251- install_package ( session )
252- install (session , "pygments " , "pytest " , "typeguard " )
146+ session . install ( "." )
147+ session . install ("pytest " , "typeguard " , "pygments " )
253148 session .run ("pytest" , f"--typeguard-packages={ package } " , * session .posargs )
254149
255150
256151@nox .session (python = python_versions )
257152def xdoctest (session : Session ) -> None :
258153 """Run examples with xdoctest."""
259154 args = session .posargs or ["all" ]
260- install_package ( session )
261- install (session , "xdoctest" )
155+ session . install ( "." )
156+ session . install ("xdoctest[colors] " )
262157 session .run ("python" , "-m" , "xdoctest" , package , * args )
263158
264159
265- @nox .session (name = "docs-build" , python = "3.8 " )
160+ @nox .session (name = "docs-build" , python = "3.9 " )
266161def docs_build (session : Session ) -> None :
267162 """Build the documentation."""
268163 args = session .posargs or ["docs" , "docs/_build" ]
269- install_package ( session )
270- install (session , "sphinx" )
164+ session . install ( "." )
165+ session . install ("sphinx" , "sphinx-click" , "sphinx-rtd-theme " )
271166
272167 build_dir = Path ("docs" , "_build" )
273168 if build_dir .exists ():
@@ -276,12 +171,12 @@ def docs_build(session: Session) -> None:
276171 session .run ("sphinx-build" , * args )
277172
278173
279- @nox .session (python = "3.8 " )
174+ @nox .session (python = "3.9 " )
280175def docs (session : Session ) -> None :
281176 """Build and serve the documentation with live reloading on file changes."""
282177 args = session .posargs or ["--open-browser" , "docs" , "docs/_build" ]
283- install_package ( session )
284- install (session , "sphinx" , "sphinx-autobuild " )
178+ session . install ( "." )
179+ session . install ("sphinx" , "sphinx-autobuild " , "sphinx-click" , "sphinx-rtd-theme " )
285180
286181 build_dir = Path ("docs" , "_build" )
287182 if build_dir .exists ():
0 commit comments