-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
152 lines (117 loc) · 4.34 KB
/
noxfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Noxfile meant for the cookiecutter repository itself."""
from pathlib import Path
import os
import shutil
import tempfile
import nox
nox.options.sessions = ["test"]
nox.options.error_on_external_run = True
@nox.session
def lint(session: nox.Session):
"""Run linters. Since cookiecutter formatting (double curly braces) is not
supported by ruff, we will escape those values specifically for ruff."""
session.install("pre-commit")
session.run_install("pre-commit", "install")
session.run("pre-commit", "run", "--all-files")
@nox.session
def initialize_commit_hooks(session: nox.Session):
"""Run pre-commit to install various hooks.
The hooks are in `.pre-commit-config.yaml`.
"""
session.install("pre-commit")
session.run(
"pre-commit",
"install",
"--install-hooks",
"--hook-type=pre-commit",
"--hook-type=commit-msg",
)
@nox.session(python=["3.10", "3.11", "3.12"])
def test(session: nox.Session):
"""Test the cookiecutter template."""
session.install("pytest", "pytest-cookies", "pytest-xdist", "nox")
session.run("pytest", "tests/", *session.posargs)
session.notify("test_filled_template")
session.notify("test_lint_run_template")
@nox.session(python=["3.10", "3.11", "3.12"])
def test_filled_template(session: nox.Session):
"""Test a freshly generated template."""
session.install("cookiecutter", "pre-commit", "nox")
tmp_dir = Path(session.create_tmp()).resolve()
if tmp_dir.exists():
all_dirs = [
Path(root) / dir for root, dirs, _ in os.walk(tmp_dir) for dir in dirs
]
for path in all_dirs:
if path.exists():
shutil.rmtree(path)
template_dir = Path(".").resolve()
with session.chdir(".."):
session.run(
"cookiecutter",
"--no-input",
"--default-config",
str(template_dir),
"-o",
str(tmp_dir),
)
new_package_path = tmp_dir / "default_instrument_name_dr_package"
with session.chdir(tmp_dir):
assert new_package_path.exists()
with session.chdir(new_package_path):
# Create a development environment and ensure packages are installed.
session.log(80 * "=")
session.log(" GENERATING DEVELOPMENT ENVIRONMENT")
session.log(80 * "=")
session.run("nox", "-s", "devenv")
venv_loc = Path("venv")
venv_python = venv_loc / "bin" / "python"
instrument = "default_instrument_name"
instrument_title = "Default_Instrument_Name"
test_imports = (
"import astrodata",
"import gemini_instruments",
"import geminidr",
"import gempy",
"import gemini_obs_db",
"import gemini_calmgr",
f"import {instrument}_instruments",
f"import {instrument}_instruments.{instrument}",
f"from {instrument}_instruments.{instrument} import AstroData{instrument_title}",
f"import {instrument}dr",
)
session.run(
str(venv_python),
"-c",
"\n".join(test_imports),
external=True,
)
session.log(80 * "=")
session.log(" RUNNING TESTS ON GENERATED TEMPLATE")
session.log(80 * "=")
# Run the tests.
session.run("nox", "-s", "tests")
@nox.session()
def test_lint_run_template(session: nox.Session):
"""Creates a new repo from template and lints it."""
session.install("cookiecutter", "pre-commit", "nox")
# Need to not use nox' temporary directories to avoid submodule issues.
with tempfile.TemporaryDirectory() as tmp_dir_name:
tmp_dir = Path(tmp_dir_name)
template_dir = Path(".").resolve()
with session.chdir(".."):
session.run(
"cookiecutter",
"--no-input",
"--default-config",
str(template_dir),
"-o",
str(tmp_dir),
)
new_package_path = tmp_dir / "default_instrument_name_dr_package"
with session.chdir(tmp_dir):
assert new_package_path.exists()
with session.chdir(new_package_path):
session.run("git", "init", external=True)
session.run("git", "add", ".", external=True)
session.run("nox", "-s", "lint")