Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.egg-info
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- id: taplo-lint
name: taplo-lint
description: Lint TOML documents
entry: taplo lint
entry: cached-taplo-lint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to have a separate hook? I don't want to temper with the default behaviour of taplo-lint.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your concern, but I'd still "shadow" the original hook:

  • I suspect that most users just install the hook and forget, no fancy setup, no weird customizations, no reading docs when updating => having it "magically go faster" after a pre-commit autoupdate seems the best we can do.
  • Those who added --cache-path something shouldn't be impacted, since the wrapper takes this case into account (and does nothing when it's the case).

Sure things could break in other ways:

  • taplo lint changing the caching behavior
  • some Murphy laws 🤷

Of course you have the last word.

language: python
types: [toml]
args: [--default-schema-catalogs]
Expand Down
22 changes: 22 additions & 0 deletions cached_taplo_lint.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
98 changes: 98 additions & 0 deletions test_cached_taplo_lint.py
Original file line number Diff line number Diff line change
@@ -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()