Skip to content
Closed
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
15 changes: 11 additions & 4 deletions sync_pre_commit_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
'black', 'flake8', 'mypy', 'eslint', 'csslint', 'fixmyjs', 'jshint',
'prettier',
})
SYNCED_DEPENDENCIES = {
'@eslint/js': 'eslint',
}

_SEPS = ('==', '@')
_RE_SEP = re.compile(rf'^(.+)({"|".join(_SEPS)})(.+)$')
Expand Down Expand Up @@ -74,16 +77,20 @@ def main(argv: Sequence[str] | None = None) -> int:
for i, dep in enumerate(hook.get('additional_dependencies', ())):
if match := _RE_SEP.match(dep):
name, sep, cur_version = match.groups()
target_version = versions.get(name, cur_version)
target_version = versions.get(
SYNCED_DEPENDENCIES.get(name, name), cur_version,
)
if target_version != cur_version:
updated_dep = f'{name}{sep}{target_version}'
hook['additional_dependencies'][i] = updated_dep
updated.append((hook['id'], name))
updated.append((hook['id'], dep, updated_dep))

if updated:
print(f'Writing updates to {filename}:')
for hid, name in updated:
print(f'\tSetting {hid!r} dependency {name!r} to {versions[name]}')
for hid, old_dep, updated_dep in updated:
print(
f'\tSetting {hid!r} dependency {old_dep!r} to {updated_dep!r}',
)

with open(filename, 'w+') as f:
yaml.dump(loaded, f)
Expand Down
8 changes: 7 additions & 1 deletion tests/sync_pre_commit_deps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_main_noop(tmpdir, s):
assert f.read() == s


def test_main_writes_all(tmpdir):
def test_main_writes_all(tmpdir, capsys):
cfg = tmpdir.join('.pre-commit-config.yaml')
cfg.write(
'repos:\n'
Expand Down Expand Up @@ -130,6 +130,7 @@ def test_main_writes_all(tmpdir):
' - id: eslint\n'
' additional_dependencies:\n'
' - eslint@8.38.0\n'
' - "@eslint/js==8.38.0"\n'
# all repos below should have their additional_dependencies rewritten
'- repo: https://github.com/asottile/yesqa\n'
' rev: v1.5.0\n'
Expand Down Expand Up @@ -185,6 +186,7 @@ def test_main_writes_all(tmpdir):
' - id: eslint\n'
' additional_dependencies:\n'
' - eslint@8.39.0\n'
' - "@eslint/js==8.39.0"\n'
'- repo: https://github.com/asottile/yesqa\n'
' rev: v1.5.0\n'
' hooks:\n'
Expand Down Expand Up @@ -213,6 +215,10 @@ def test_main_writes_all(tmpdir):
' - mypy==1.13.0\n'
)

out = capsys.readouterr().out
assert "'eslint@8.38.0' to 'eslint@8.39.0'" in out
assert "'@eslint/js==8.38.0' to '@eslint/js==8.39.0'" in out


def test_main_no_dep_on_one_and_writes_other(tmpdir):
cfg = tmpdir.join('.pre-commit-config.yaml')
Expand Down
Loading