Skip to content

Commit

Permalink
Merge branch 'main' into chore/pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
mixxorz authored Aug 14, 2022
2 parents c908c88 + 3393f2e commit 06a0e60
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
68 changes: 46 additions & 22 deletions dslr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ def cli(url, debug):

@cli.command()
@click.argument("name", shell_complete=complete_snapshot_names)
def snapshot(name: str):
@click.option(
"-y",
"--yes",
"overwrite_confirmed",
is_flag=True,
help="Overwrite existing snapshot without confirmation.",
)
def snapshot(name: str, overwrite_confirmed: bool):
"""
Takes a snapshot of the database
"""
Expand All @@ -84,12 +91,13 @@ def snapshot(name: str):
try:
snapshot = find_snapshot(name)

click.confirm(
click.style(
f"Snapshot {snapshot.name} already exists. Overwrite?", fg="yellow"
),
abort=True,
)
if not overwrite_confirmed:
click.confirm(
click.style(
f"Snapshot {snapshot.name} already exists. Overwrite?", fg="yellow"
),
abort=True,
)

delete_snapshot(snapshot)
new = False
Expand Down Expand Up @@ -184,7 +192,14 @@ def delete(name):
@cli.command()
@click.argument("old_name", shell_complete=complete_snapshot_names)
@click.argument("new_name", shell_complete=complete_snapshot_names)
def rename(old_name, new_name):
@click.option(
"-y",
"--yes",
"overwrite_confirmed",
is_flag=True,
help="Overwrite existing snapshot without confirmation.",
)
def rename(old_name: str, new_name: str, overwrite_confirmed: bool):
"""
Renames a snapshot
"""
Expand All @@ -197,13 +212,14 @@ def rename(old_name, new_name):
try:
existing_snapshot = find_snapshot(new_name)

click.confirm(
click.style(
f"Snapshot {existing_snapshot.name} already exists. Overwrite?",
fg="yellow",
),
abort=True,
)
if not overwrite_confirmed:
click.confirm(
click.style(
f"Snapshot {existing_snapshot.name} already exists. Overwrite?",
fg="yellow",
),
abort=True,
)

delete_snapshot(existing_snapshot)
except SnapshotNotFound:
Expand Down Expand Up @@ -245,7 +261,14 @@ def export(name):
@cli.command("import")
@click.argument("filename", type=click.Path(exists=True))
@click.argument("name", shell_complete=complete_snapshot_names)
def import_(filename, name):
@click.option(
"-y",
"--yes",
"overwrite_confirmed",
is_flag=True,
help="Overwrite existing snapshot without confirmation.",
)
def import_(filename: str, name: str, overwrite_confirmed):
"""
Imports a snapshot from a file
"""
Expand All @@ -254,12 +277,13 @@ def import_(filename, name):
try:
snapshot = find_snapshot(name)

click.confirm(
click.style(
f"Snapshot {snapshot.name} already exists. Overwrite?", fg="yellow"
),
abort=True,
)
if not overwrite_confirmed:
click.confirm(
click.style(
f"Snapshot {snapshot.name} already exists. Overwrite?", fg="yellow"
),
abort=True,
)

delete_snapshot(snapshot)
except SnapshotNotFound:
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def test_snapshot_overwrite(self):
)
self.assertIn("Updated snapshot existing-snapshot-1", result.output)

def test_snapshot_overwrite_with_yes(self):
# stub_exec sets up a fake snapshot called "existing-snapshot-1"
runner = CliRunner()
result = runner.invoke(
cli.cli,
["snapshot", "existing-snapshot-1", "-y"],
)

self.assertEqual(result.exit_code, 0)
self.assertNotIn(
"Snapshot existing-snapshot-1 already exists. Overwrite?", result.output
)
self.assertIn("Updated snapshot existing-snapshot-1", result.output)

def test_restore(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["restore", "existing-snapshot-1"])
Expand Down Expand Up @@ -118,6 +132,20 @@ def test_rename_overwrite(self):
"Renamed snapshot existing-snapshot-1 to existing-snapshot-2", result.output
)

def test_rename_overwrite_with_yes(self):
runner = CliRunner()
result = runner.invoke(
cli.cli, ["rename", "existing-snapshot-1", "existing-snapshot-2", "-y"]
)

self.assertEqual(result.exit_code, 0)
self.assertNotIn(
"Snapshot existing-snapshot-2 already exists. Overwrite?", result.output
)
self.assertIn(
"Renamed snapshot existing-snapshot-1 to existing-snapshot-2", result.output
)

def test_export(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["export", "existing-snapshot-1"])
Expand Down Expand Up @@ -165,6 +193,21 @@ def test_import_overwrite(self):
result.output,
)

def test_import_overwrite_with_yes(self):
runner = CliRunner()
result = runner.invoke(
cli.cli, ["import", "pyproject.toml", "existing-snapshot-1", "-y"]
)

self.assertEqual(result.exit_code, 0)
self.assertNotIn(
"Snapshot existing-snapshot-1 already exists. Overwrite?", result.output
)
self.assertIn(
"Imported snapshot existing-snapshot-1 from pyproject.toml",
result.output,
)


@mock.patch("dslr.cli.get_snapshots")
class ConfigTest(TestCase):
Expand Down

0 comments on commit 06a0e60

Please sign in to comment.