Skip to content
Merged
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
Expand Up @@ -6,6 +6,7 @@
/.idea
/.project
/.pydevproject
/.pytest_cache
/.tox
/.venv.touch
/venv*
Expand Down
1 change: 1 addition & 0 deletions pre_commit_mirror_maker/all/.pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
language: {language}
'{match_key}': {match_val}
args: {args}
additional_dependencies: {additional_dependencies}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How important is the minimum_pre_commit_version key? Is it worth doing some hackery to get it in for projects which have additional_dependencies?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

probably not all that important, it's not exactly a new feature (and will fail for other reasons) 😆

20 changes: 20 additions & 0 deletions pre_commit_mirror_maker/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,28 @@ def python_get_package_versions(package_name):
return list(reversed(versions))


def rust_get_package_versions(package_name):
url = f'https://crates.io/api/v1/crates/{package_name}'
resp = json.load(urllib.request.urlopen(url))
return list(reversed([version['num'] for version in resp['versions']]))


def node_get_additional_dependencies(package_name, package_version):
return [f'{package_name}@{package_version}']


def rust_get_additional_dependencies(package_name, package_version):
return [f'cli:{package_name}:{package_version}']


LIST_VERSIONS = {
'node': node_get_package_versions,
'python': python_get_package_versions,
'ruby': ruby_get_package_versions,
'rust': rust_get_package_versions,
}

ADDITIONAL_DEPENDENCIES = {
'node': node_get_additional_dependencies,
'rust': rust_get_additional_dependencies,
}
30 changes: 27 additions & 3 deletions pre_commit_mirror_maker/make_repo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import os.path
import subprocess

import pkg_resources

from pre_commit_mirror_maker.languages import ADDITIONAL_DEPENDENCIES
from pre_commit_mirror_maker.languages import LIST_VERSIONS


Expand Down Expand Up @@ -41,11 +43,20 @@ def format_files(src, dest, **fmt_vars):
file_obj.write(output_contents)


def _commit_version(repo, *, language, version, **fmt_vars):
def _commit_version(
repo, *, language, version, additional_dependencies, **fmt_vars,
):
# 'all' writes the .version and .pre-commit-hooks.yaml files
for lang in ('all', language):
src = pkg_resources.resource_filename('pre_commit_mirror_maker', lang)
format_files(src, repo, language=language, version=version, **fmt_vars)
format_files(
src,
repo,
language=language,
version=version,
additional_dependencies=additional_dependencies,
**fmt_vars,
)

hooks_yaml = os.path.join(repo, 'hooks.yaml')
if os.path.exists(hooks_yaml):
Expand Down Expand Up @@ -73,6 +84,19 @@ def make_repo(repo, *, language, name, **fmt_vars):
versions_to_apply = package_versions

for version in versions_to_apply:
if language in ADDITIONAL_DEPENDENCIES:
additional_dependencies = ADDITIONAL_DEPENDENCIES[language](
name,
version,
)
else:
additional_dependencies = []

_commit_version(
repo, name=name, language=language, version=version, **fmt_vars,
repo,
name=name,
language=language,
version=version,
additional_dependencies=json.dumps(additional_dependencies),
**fmt_vars,
)
9 changes: 0 additions & 9 deletions pre_commit_mirror_maker/node/.pre-commit-hooks.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions pre_commit_mirror_maker/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "__fake_crate"
version = "0.0.0"

[[bin]]
name = "__fake_cmd"
path = "main.rs"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It turns out you can't really make a noop Cargo.toml file, and since we're installing with --bins, you can't really even make a library-only crate (so there would be no binary); cargo is "smart" enough to complain about that.

So this builds a bogus executable which does nothing. Maybe okay?

An alternative could be to make pre-commit's Rust support allow repos with no Cargo.toml (and only use additional_dependencies in that case), but I'm worried about it masking bugs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is probably fine, we have to do the same dance for go as well

1 change: 1 addition & 0 deletions pre_commit_mirror_maker/rust/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {{}}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
'all/.pre-commit-hooks.yaml',
'all/.version',
'node/.npmignore',
'node/.pre-commit-hooks.yaml',
'node/*',
'python/*',
'ruby/*',
'rust/*',
],
},
)
8 changes: 8 additions & 0 deletions tests/languages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pre_commit_mirror_maker.languages import node_get_package_versions
from pre_commit_mirror_maker.languages import python_get_package_versions
from pre_commit_mirror_maker.languages import ruby_get_package_versions
from pre_commit_mirror_maker.languages import rust_get_package_versions


def assert_all_text(versions):
Expand All @@ -29,3 +30,10 @@ def test_ruby_get_package_version_output():
ret = ruby_get_package_versions('scss-lint')
assert ret
assert_all_text(ret)


@pytest.mark.integration
def test_rust_get_package_version_output():
ret = rust_get_package_versions('clap')
assert ret
assert_all_text(ret)
24 changes: 24 additions & 0 deletions tests/make_repo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_commit_version(in_git_dir):
'.',
version='0.24.1', language='ruby', name='scss-lint', entry='scss-lint',
match_key='files', match_val=r'\.scss$', args='[]',
additional_dependencies=[],
)

# Assert that our things got copied over
Expand All @@ -84,6 +85,7 @@ def test_arguments(in_git_dir):
'.',
version='0.6.2', language='python', name='yapf', entry='yapf',
match_key='files', match_val=r'\.py$', args='["-i"]',
additional_dependencies=['scikit-learn'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lol you troll

)
contents = in_git_dir.join('.pre-commit-hooks.yaml').read()
assert yaml.safe_load(contents) == [{
Expand All @@ -93,6 +95,7 @@ def test_arguments(in_git_dir):
'language': 'python',
'files': r'\.py$',
'args': ['-i'],
'additional_dependencies': ['scikit-learn'],
}]


Expand Down Expand Up @@ -213,3 +216,24 @@ def test_python_integration(in_git_dir):
subprocess.check_call((sys.executable, 'setup.py', 'egg_info'))

# TODO: test that the package is installable


@pytest.mark.integration
def test_rust_integration(in_git_dir):
make_repo(
'.',
language='rust', name='shellharden', entry='shellharden',
match_key='types', match_val='shell', args='["--replace"]',
)
# Our files should exist
assert in_git_dir.join('.version').exists()
assert in_git_dir.join('.pre-commit-hooks.yaml').exists()
assert in_git_dir.join('Cargo.toml').exists()
assert in_git_dir.join('main.rs').exists()

# Should have made _some_ tags
assert _cmd('git', 'tag', '-l')
# Should have made _some_ commits
assert _cmd('git', 'log', '--oneline')

# TODO: test that the package is installable