From a0ec4c149460948ea433eaca4e336a5e02ed77e4 Mon Sep 17 00:00:00 2001 From: Sergio LIVI Date: Mon, 11 Aug 2025 11:53:22 +0200 Subject: [PATCH 1/2] ENH: Cache schemas for taplo lint taplo lint supports the --cache-path argument to cache downloaded schemas. This adds the parameter if not passed already, using a directory in the activated venv. --- .gitignore | 1 + .pre-commit-hooks.yaml | 2 +- cached_taplo_lint.py | 18 ++++++++++++++++++ pyproject.toml | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 cached_taplo_lint.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11041c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.egg-info diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 39da6db..9c2c05a 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -13,7 +13,7 @@ - id: taplo-lint name: taplo-lint description: Lint TOML documents - entry: taplo lint + entry: cached-taplo-lint language: python types: [toml] args: [--default-schema-catalogs] diff --git a/cached_taplo_lint.py b/cached_taplo_lint.py new file mode 100644 index 0000000..edcb487 --- /dev/null +++ b/cached_taplo_lint.py @@ -0,0 +1,18 @@ +import sys +import subprocess +from pathlib import Path + +cache_path = Path(sys.prefix) / 'taplo-cache' + +def main(): + args = sys.argv[1:] + + if '--cache-path' not in args: + cache_path.mkdir(parents=True, exist_ok=True) + args.extend(['--cache-path', str(cache_path)]) + + proc = subprocess.run(["taplo", "lint", *args]) + sys.exit(proc.returncode) + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml index cc58025..6862d94 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,3 +3,6 @@ dependencies = ["taplo==0.9.3"] name = "pre_commit_placeholder_package" requires-python = ">=3.8" version = "0.0.0" + +[project.scripts] +cached-taplo-lint = "cached_taplo_lint:main" From 62f82a9e15ccc22e9097c77728400ef68f060978 Mon Sep 17 00:00:00 2001 From: Sergio LIVI Date: Mon, 11 Aug 2025 15:33:19 +0200 Subject: [PATCH 2/2] Check also --cache-path=... and add tests --- cached_taplo_lint.py | 12 +++-- test_cached_taplo_lint.py | 98 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 test_cached_taplo_lint.py diff --git a/cached_taplo_lint.py b/cached_taplo_lint.py index edcb487..9a6fc9c 100644 --- a/cached_taplo_lint.py +++ b/cached_taplo_lint.py @@ -1,18 +1,22 @@ -import sys import subprocess +import sys from pathlib import Path -cache_path = Path(sys.prefix) / 'taplo-cache' +cache_path = Path(sys.prefix) / "taplo-cache" + def main(): args = sys.argv[1:] - if '--cache-path' not in args: + if not any( + arg == "--cache-path" or arg.startswith("--cache-path=") for arg in args + ): cache_path.mkdir(parents=True, exist_ok=True) - args.extend(['--cache-path', str(cache_path)]) + args.extend(["--cache-path", str(cache_path)]) proc = subprocess.run(["taplo", "lint", *args]) sys.exit(proc.returncode) + if __name__ == "__main__": main() diff --git a/test_cached_taplo_lint.py b/test_cached_taplo_lint.py new file mode 100644 index 0000000..5965651 --- /dev/null +++ b/test_cached_taplo_lint.py @@ -0,0 +1,98 @@ +import unittest +from unittest import mock + +import cached_taplo_lint + + +@mock.patch("subprocess.run") +class TestCachedTaploLint(unittest.TestCase): + def test_success(self, mock_run: mock.Mock): + mock_run.return_value.returncode = 0 + + try: + cached_taplo_lint.main() + except SystemExit as e: + self.assertEqual(e.code, 0) + + mock_run.assert_called() + + def test_failure(self, mock_run: mock.Mock): + mock_run.return_value.returncode = 1 + + with self.assertRaises(SystemExit) as cm: + cached_taplo_lint.main() + self.assertNotEqual(cm.exception.code, 0) + + mock_run.assert_called() + + def test_add_cache_argument(self, mock_run: mock.Mock): + mock_run.return_value.returncode = 0 + + test_args = ["cached-taplo-lint", "--foo", "bar"] + with mock.patch("sys.argv", test_args): + try: + cached_taplo_lint.main() + except SystemExit as e: + self.assertEqual(e.code, 0) + + called_args = mock_run.call_args[0][0] + self.assertEqual( + called_args, + [ + "taplo", + "lint", + "--foo", + "bar", + "--cache-path", + mock.ANY, + ], + ) + + def test_keep_cache_argument(self, mock_run: mock.Mock): + mock_run.return_value.returncode = 0 + + test_args = ["cached-taplo-lint", "--foo", "bar", "--cache-path", "/tmp/cache"] + with mock.patch("sys.argv", test_args): + try: + cached_taplo_lint.main() + except SystemExit as e: + self.assertEqual(e.code, 0) + + called_args = mock_run.call_args[0][0] + self.assertEqual( + called_args, + [ + "taplo", + "lint", + "--foo", + "bar", + "--cache-path", + "/tmp/cache", + ], + ) + + def test_keep_cache_argument_with_equals(self, mock_run: mock.Mock): + mock_run.return_value.returncode = 0 + + test_args = ["cached-taplo-lint", "--foo", "bar", "--cache-path=/tmp/cache"] + with mock.patch("sys.argv", test_args): + try: + cached_taplo_lint.main() + except SystemExit as e: + self.assertEqual(e.code, 0) + + called_args = mock_run.call_args[0][0] + self.assertEqual( + called_args, + [ + "taplo", + "lint", + "--foo", + "bar", + "--cache-path=/tmp/cache", + ], + ) + + +if __name__ == "__main__": + unittest.main()