Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add --no-confirm option to idfx clean #309

Merged
merged 1 commit into from
Aug 4, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- ENH: add `--no-confirm` option to `idfx clean`

## [3.0.1] - 2023-08-03

- ENH: strip all trailing whitespace in `idfx digest`
Expand Down
2 changes: 1 addition & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ CMake cache files and directories
$ idfx clean .
```
This command will print a list of purgable files, and will not remove anything
without confirmation.
without confirmation (unless confirmation is explicitly skipped with `--no-confirm`).

To also remove `Makefile`, `idefix` executables, use the `--all` flag
```shell
Expand Down
18 changes: 14 additions & 4 deletions src/idefix_cli/_commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import subprocess
import sys
from argparse import ArgumentParser
from pathlib import Path
from shutil import rmtree, which

Expand Down Expand Up @@ -39,7 +40,7 @@
GENERATED_DIRS = frozenset(("CMakeFiles",))


def add_arguments(parser) -> None:
def add_arguments(parser: ArgumentParser) -> None:
parser.add_argument(
"directory", nargs="?", default=".", help="the target directory to clean"
)
Expand All @@ -49,16 +50,25 @@ def add_arguments(parser) -> None:
action="store_true",
help="also clean generated Makefiles, idefix excutable files",
)
parser.add_argument(
no_prompt_group = parser.add_mutually_exclusive_group()
no_prompt_group.add_argument(
"--dry-run",
"--dry",
dest="dry",
action="store_true",
help="skip prompt, exit without cleaning",
)
no_prompt_group.add_argument(
"--no-confirm",
dest="confirm",
action="store_false",
help="skip prompt confirmation",
)


def command(directory, clean_all: bool = False, dry: bool = False) -> int:
def command(
directory, clean_all: bool = False, dry: bool = False, confirm: bool = True
) -> int:
origin = os.path.abspath(os.curdir)
with chdir(directory):
patterns = bpatterns | kokkos_files | cmake_files | GENERATED_DIRS
Expand Down Expand Up @@ -93,7 +103,7 @@ def command(directory, clean_all: bool = False, dry: bool = False) -> int:
)
)

if dry or not Confirm.ask("\nPerform cleaning ?"):
if dry or (confirm and not Confirm.ask("\nPerform cleaning ?")):
return 0

for t in targets:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def test_patterns():
assert not kokkos_files.intersection(gpatterns)


@pytest.mark.parametrize("pattern", [p for p in kokkos_files if "*" in p])
def test_clean_no_confirmation(pattern, capsys, tmp_path):
file_to_clean = tmp_path / pattern.replace("*", "legit_file_prefix")
file_to_clean.touch()

ret = main(["clean", str(tmp_path.absolute()), "--no-confirm"])
assert ret == 0
_out, err = capsys.readouterr()
assert err == ""
assert not list(tmp_path.iterdir())


@pytest.mark.parametrize("usr_input", VALID_USER_INPUTS)
@pytest.mark.parametrize("pattern", [p for p in kokkos_files if "*" in p])
def test_clean_wildcards(pattern, usr_input, monkeypatch, tmp_path):
Expand Down