Skip to content

Commit fe57e13

Browse files
n-thumanngreenbonebot
authored andcommitted
Add: Unit tests for uv mode
1 parent 329294f commit fe57e13

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

tests/test_config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ def test_get_mode_poetry_multiline(self):
129129

130130
self.assertEqual(len(config.get_pre_commit_script_names()), 0)
131131

132+
def test_get_mode_uv_multiline(self):
133+
config = AutohooksConfig.from_dict(
134+
{"tool": {"autohooks": {"mode": "uv_multiline"}}}
135+
)
136+
137+
self.assertTrue(config.has_autohooks_config())
138+
self.assertEqual(config.get_mode(), Mode.UV_MULTILINE)
139+
140+
self.assertEqual(len(config.get_pre_commit_script_names()), 0)
141+
142+
def test_get_mode_uv(self):
143+
config = AutohooksConfig.from_dict(
144+
{"tool": {"autohooks": {"mode": "uv"}}}
145+
)
146+
147+
self.assertTrue(config.has_autohooks_config())
148+
self.assertEqual(config.get_mode(), Mode.UV)
149+
150+
self.assertEqual(len(config.get_pre_commit_script_names()), 0)
151+
132152
def test_get_mode_pythonpath(self):
133153
config = AutohooksConfig.from_dict(
134154
{"tool": {"autohooks": {"mode": "pythonpath"}}}

tests/test_hooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
POETRY_SHEBANG,
2020
PYTHON3_SHEBANG,
2121
TEMPLATE_VERSION,
22+
UV_MULTILINE_SHEBANG,
23+
UV_SHEBANG,
2224
PreCommitTemplate,
2325
)
2426
from autohooks.utils import exec_git
@@ -161,6 +163,12 @@ def test_poetry_mode(self):
161163

162164
self.assertEqual(pre_commit_hook.read_mode(), Mode.POETRY)
163165

166+
def test_uv_mode(self):
167+
path = FakeReadPath(f"#!{UV_SHEBANG}")
168+
pre_commit_hook = PreCommitHook(path)
169+
170+
self.assertEqual(pre_commit_hook.read_mode(), Mode.UV)
171+
164172
def test_poetry_mode_with_python3(self):
165173
path = FakeReadPath(f"#!{POETRY_SHEBANG}3")
166174
pre_commit_hook = PreCommitHook(path)
@@ -179,6 +187,12 @@ def test_poetry_multiline_mode(self):
179187

180188
self.assertEqual(pre_commit_hook.read_mode(), Mode.POETRY_MULTILINE)
181189

190+
def test_uv_multiline_mode(self):
191+
path = FakeReadPath(f"#!{UV_MULTILINE_SHEBANG}")
192+
pre_commit_hook = PreCommitHook(path)
193+
194+
self.assertEqual(pre_commit_hook.read_mode(), Mode.UV_MULTILINE)
195+
182196
def test_pythonpath_mode(self):
183197
path = FakeReadPath(f"#!{PYTHON3_SHEBANG}")
184198
pre_commit_hook = PreCommitHook(path)
@@ -211,6 +225,18 @@ def test_poetry_mode(self):
211225
text = args[0]
212226
self.assertRegex(text, f"^#!{POETRY_SHEBANG} *")
213227

228+
def test_uv_mode(self):
229+
write_path = Mock()
230+
pre_commit_hook = PreCommitHook(write_path)
231+
pre_commit_hook.write(mode=Mode.UV)
232+
233+
write_path.chmod.assert_called_with(0o775)
234+
self.assertTrue(write_path.write_text.called)
235+
236+
args, _kwargs = write_path.write_text.call_args
237+
text = args[0]
238+
self.assertRegex(text, f"^#!{UV_SHEBANG} *")
239+
214240
def test_pythonpath_mode(self):
215241
write_path = Mock()
216242
pre_commit_hook = PreCommitHook(write_path)

tests/test_settings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def test_get_effective_mode(self):
2222
self.assertEqual(
2323
Mode.POETRY_MULTILINE.get_effective_mode(), Mode.POETRY_MULTILINE
2424
)
25+
self.assertEqual(Mode.UV.get_effective_mode(), Mode.UV)
26+
self.assertEqual(
27+
Mode.UV_MULTILINE.get_effective_mode(), Mode.UV_MULTILINE
28+
)
2529
self.assertEqual(Mode.UNDEFINED.get_effective_mode(), Mode.PYTHONPATH)
2630
self.assertEqual(Mode.UNKNOWN.get_effective_mode(), Mode.PYTHONPATH)
2731

@@ -53,6 +57,18 @@ def test_get_poetry_multiline_mode_from_string(self):
5357
Mode.from_string("POETRY_MULTILINE"), Mode.POETRY_MULTILINE
5458
)
5559

60+
def test_get_uv_mode_from_string(self):
61+
self.assertEqual(Mode.from_string("uv"), Mode.UV)
62+
self.assertEqual(Mode.from_string("UV"), Mode.UV)
63+
64+
def test_get_uv_multiline_mode_from_string(self):
65+
self.assertEqual(
66+
Mode.from_string("uv_multiline"), Mode.UV_MULTILINE
67+
)
68+
self.assertEqual(
69+
Mode.from_string("UV_MULTILINE"), Mode.UV_MULTILINE
70+
)
71+
5672
def test_get_invalid_mode_from_string(self):
5773
self.assertEqual(Mode.from_string("foo"), Mode.UNKNOWN)
5874
self.assertEqual(Mode.from_string(None), Mode.UNDEFINED)

tests/test_template.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def test_should_render_mode_poetry(self):
6060
"/usr/bin/env -S poetry run python",
6161
)
6262

63+
def test_should_render_mode_uv(self):
64+
path = FakeTemplatePath("$SHEBANG")
65+
template = PreCommitTemplate(path)
66+
self.assertEqual(
67+
template.render(mode=Mode.UV),
68+
"/usr/bin/env -S uv run python",
69+
)
70+
6371
def test_should_render_mode_pipenv_multiline(self):
6472
path = FakeTemplatePath("$SHEBANG")
6573
template = PreCommitTemplate(path)
@@ -88,6 +96,20 @@ def test_should_render_mode_poetry_multiline(self):
8896
),
8997
)
9098

99+
def test_should_render_mode_uv_multiline(self):
100+
path = FakeTemplatePath("$SHEBANG")
101+
template = PreCommitTemplate(path)
102+
self.assertEqual(
103+
template.render(mode=Mode.UV_MULTILINE),
104+
(
105+
"/bin/sh\n"
106+
"\"true\" ''':'\n"
107+
'uv run python "$0" "$@"\n'
108+
'exit "$?"\n'
109+
"'''"
110+
),
111+
)
112+
91113
def test_should_render_mode_unknown(self):
92114
path = FakeTemplatePath("$SHEBANG")
93115
template = PreCommitTemplate(path)

0 commit comments

Comments
 (0)