diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-03 23:00:39 -0500 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-03 23:00:39 -0500 |
| commit | 3d87cf730f8b9f6da35e41d3a1ce8c1dbe4f6392 (patch) | |
| tree | 0cb60d5458a556ae126a1c692b32e1535536b23f | |
| parent | Remove bandit and use basedpyright (diff) | |
| download | dotfiles-3d87cf730f8b9f6da35e41d3a1ce8c1dbe4f6392.zip | |
Add django-test helper
| -rwxr-xr-x | bin/.local/bin/django-test | 52 | ||||
| -rw-r--r-- | nvim/.config/nvim/plugin/20_keymaps.lua | 20 |
2 files changed, 72 insertions, 0 deletions
diff --git a/bin/.local/bin/django-test b/bin/.local/bin/django-test new file mode 100755 index 0000000..f5081cf --- /dev/null +++ b/bin/.local/bin/django-test @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +import ast +import subprocess +import sys +from pathlib import Path +from typing import override + +if len(sys.argv) < 2: + raise SystemExit("usage: django-test [--list] <func_name>") + +list_only = sys.argv[1] == "--list" +func_name = sys.argv[2] if list_only else sys.argv[1] + +repo_root = Path(subprocess.check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip()) +files = subprocess.check_output(["git", "ls-files", "*.py"], text=True, cwd=repo_root).splitlines() +hits: list[str] = [] + +for rel_path in files: + path = repo_root / rel_path + s = path.read_text(encoding="utf-8") + + if func_name not in s: + continue + t = ast.parse(s, filename=str(rel_path)) + mod = rel_path[:-3].replace("/", ".").replace("\\", ".") + + class V(ast.NodeVisitor): + cls: str | None = None + + @override + def visit_ClassDef(self, node: ast.ClassDef) -> None: + old = self.cls + self.cls = node.name + self.generic_visit(node) + self.cls = old + + @override + def visit_FunctionDef(self, node: ast.FunctionDef) -> None: + if node.name == func_name: + hits.append(f"{sys.executable} manage.py test {mod}.{self.cls+'.' if self.cls else ''}{func_name}") + self.generic_visit(node) + + V().visit(t) + +hits = sorted(set(hits)) + +if list_only or len(hits) != 1: + [print(h) for h in hits] +else: + cmd = hits[0] + print(cmd) + _ = subprocess.run(cmd, shell=True, check=True, cwd=repo_root) diff --git a/nvim/.config/nvim/plugin/20_keymaps.lua b/nvim/.config/nvim/plugin/20_keymaps.lua index 7efcaa5..3e35681 100644 --- a/nvim/.config/nvim/plugin/20_keymaps.lua +++ b/nvim/.config/nvim/plugin/20_keymaps.lua @@ -22,3 +22,23 @@ map('n', '<C-h>', '<C-w><C-h>', { desc = 'Move focus to the left window' }) map('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right window' }) map('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' }) map('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' }) + +map('n', '<leader>rdt', function() + local func_name = vim.fn.expand '<cword>' + local output = vim.fn.systemlist('django-test --list ' .. func_name) + if #output == 0 then + vim.notify('No tests found for: ' .. func_name, vim.log.levels.WARN) + elseif #output == 1 then + -- Single match, run directly + vim.cmd('split | terminal ' .. output[1]) + else + -- Multiple matches, prompt user + vim.ui.select(output, { + prompt = 'Select test to run:', + }, function(choice) + if choice then + vim.cmd('split | terminal ' .. choice) + end + end) + end +end, { desc = '[R]un [D]jango [T]est for word under cursor' }) |