Skip to content

Commit eb67a30

Browse files
committed
!squash
1 parent 22c6bc1 commit eb67a30

File tree

2 files changed

+39
-107
lines changed

2 files changed

+39
-107
lines changed

src/vcspull/cli/sync.py

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,17 @@
11
import argparse
22
import logging
33
import sys
4+
import typing as t
45
from copy import deepcopy
56

6-
import click
7-
import click.shell_completion
8-
from click.shell_completion import CompletionItem
9-
107
from libvcs._internal.shortcuts import create_project
118
from libvcs.url import registry as url_tools
12-
from vcspull.types import ConfigDict
139

1410
from ..config import filter_repos, find_config_files, load_configs
1511

1612
log = logging.getLogger(__name__)
1713

1814

19-
def get_repo_completions(
20-
ctx: click.Context, param: click.Parameter, incomplete: str
21-
) -> list[CompletionItem]:
22-
configs = (
23-
load_configs(find_config_files(include_home=True))
24-
if ctx.params["config"] is None
25-
else load_configs(files=[ctx.params["config"]])
26-
)
27-
found_repos: list[ConfigDict] = []
28-
repo_terms = [incomplete]
29-
30-
for repo_term in repo_terms:
31-
dir, vcs_url, name = None, None, None
32-
if any(repo_term.startswith(n) for n in ["./", "/", "~", "$HOME"]):
33-
dir = dir
34-
elif any(repo_term.startswith(n) for n in ["http", "git", "svn", "hg"]):
35-
vcs_url = repo_term
36-
else:
37-
name = repo_term
38-
39-
# collect the repos from the config files
40-
found_repos.extend(filter_repos(configs, dir=dir, vcs_url=vcs_url, name=name))
41-
if len(found_repos) == 0:
42-
found_repos = configs
43-
44-
return [
45-
CompletionItem(o["name"])
46-
for o in found_repos
47-
if o["name"].startswith(incomplete)
48-
]
49-
50-
51-
def get_config_file_completions(ctx, args, incomplete):
52-
return [
53-
click.shell_completion.CompletionItem(c)
54-
for c in find_config_files(include_home=True)
55-
if str(c).startswith(incomplete)
56-
]
57-
58-
5915
def clamp(n, _min, _max):
6016
return max(_min, min(n, _max))
6117

@@ -64,29 +20,6 @@ def clamp(n, _min, _max):
6420
NO_REPOS_FOR_TERM_MSG = 'No repo found in config(s) for "{name}"'
6521

6622

67-
# @click.command(name="sync")
68-
# @click.pass_context
69-
# @click.argument(
70-
# "repo_terms", type=click.STRING, nargs=-1, shell_complete=get_repo_completions
71-
# )
72-
# @click.option(
73-
# "config",
74-
# "--config",
75-
# "-c",
76-
# type=click.Path(exists=True),
77-
# help="Specify config",
78-
# shell_complete=get_config_file_completions,
79-
# )
80-
# @click.option(
81-
# "exit_on_error",
82-
# "--exit-on-error",
83-
# "-x",
84-
# is_flag=True,
85-
# default=False,
86-
# help="Exit immediately when encountering an error syncing multiple repos",
87-
# )
88-
89-
9023
def create_sync_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
9124
parser.add_argument("--config", "-c", help="Specify config")
9225
parser.add_argument("repo_terms", nargs="+", help="Specify config")
@@ -104,7 +37,9 @@ def sync(
10437
repo_terms,
10538
config,
10639
exit_on_error: bool,
107-
parser: argparse.ArgumentParser | None = None,
40+
parser: t.Optional[
41+
argparse.ArgumentParser
42+
] = None, # optional so sync can be unit tested
10843
) -> None:
10944
if config:
11045
configs = load_configs([config])

tests/test_cli.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pytest
66

77
import yaml
8-
from click.testing import CliRunner
98

109
from libvcs.sync.git import GitSync
1110
from vcspull.__about__ import __version__
@@ -209,44 +208,42 @@ def test_sync(
209208
expected_in_err: "ExpectedOutput",
210209
expected_not_in_err: "ExpectedOutput",
211210
) -> None:
212-
runner = CliRunner()
213-
with runner.isolated_filesystem(temp_dir=tmp_path):
214-
config = {
215-
"~/github_projects/": {
216-
"my_git_repo": {
217-
"url": f"git+file://{git_repo.dir}",
218-
"remotes": {"test_remote": f"git+file://{git_repo.dir}"},
219-
},
220-
"broken_repo": {
221-
"url": f"git+file://{git_repo.dir}",
222-
"remotes": {"test_remote": "git+file://non-existent-remote"},
223-
},
224-
}
211+
config = {
212+
"~/github_projects/": {
213+
"my_git_repo": {
214+
"url": f"git+file://{git_repo.dir}",
215+
"remotes": {"test_remote": f"git+file://{git_repo.dir}"},
216+
},
217+
"broken_repo": {
218+
"url": f"git+file://{git_repo.dir}",
219+
"remotes": {"test_remote": "git+file://non-existent-remote"},
220+
},
225221
}
226-
yaml_config = config_path / ".vcspull.yaml"
227-
yaml_config_data = yaml.dump(config, default_flow_style=False)
228-
yaml_config.write_text(yaml_config_data, encoding="utf-8")
229-
230-
# CLI can sync
231-
try:
232-
cli(sync_args)
233-
except SystemExit:
234-
pass
235-
236-
result = capsys.readouterr()
237-
output = "".join(list(result.out if expected_exit_code == 0 else result.err))
238-
239-
if expected_in_out is not None:
240-
if isinstance(expected_in_out, str):
241-
expected_in_out = [expected_in_out]
242-
for needle in expected_in_out:
243-
assert needle in output
244-
245-
if expected_not_in_out is not None:
246-
if isinstance(expected_not_in_out, str):
247-
expected_not_in_out = [expected_not_in_out]
248-
for needle in expected_not_in_out:
249-
assert needle not in output
222+
}
223+
yaml_config = config_path / ".vcspull.yaml"
224+
yaml_config_data = yaml.dump(config, default_flow_style=False)
225+
yaml_config.write_text(yaml_config_data, encoding="utf-8")
226+
227+
# CLI can sync
228+
try:
229+
cli(sync_args)
230+
except SystemExit:
231+
pass
232+
233+
result = capsys.readouterr()
234+
output = "".join(list(result.out if expected_exit_code == 0 else result.err))
235+
236+
if expected_in_out is not None:
237+
if isinstance(expected_in_out, str):
238+
expected_in_out = [expected_in_out]
239+
for needle in expected_in_out:
240+
assert needle in output
241+
242+
if expected_not_in_out is not None:
243+
if isinstance(expected_not_in_out, str):
244+
expected_not_in_out = [expected_not_in_out]
245+
for needle in expected_not_in_out:
246+
assert needle not in output
250247

251248

252249
class SyncBrokenFixture(t.NamedTuple):

0 commit comments

Comments
 (0)