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..9a6fc9c --- /dev/null +++ b/cached_taplo_lint.py @@ -0,0 +1,22 @@ +import subprocess +import sys +from pathlib import Path + +cache_path = Path(sys.prefix) / "taplo-cache" + + +def main(): + args = sys.argv[1:] + + 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)]) + + 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" 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()