fix M
This commit is contained in:
@@ -301,6 +301,35 @@ def resolveUnmuxOutputDirectory(context, outputDirectory, subtitlesOnly, label):
|
||||
return os.path.join(configuredSubtitlesBaseDirectory, resolvedLabel), True
|
||||
|
||||
|
||||
def confirmUnmuxOutputDirectoryCreation(outputDirectory):
|
||||
message = (
|
||||
"Create unmux output directory and missing parents: "
|
||||
+ str(outputDirectory)
|
||||
)
|
||||
|
||||
if not sys.stdin.isatty():
|
||||
return click.confirm(message, default=True)
|
||||
|
||||
click.echo(f"{message} [Y/n]: ", nl=False)
|
||||
while True:
|
||||
char = click.getchar()
|
||||
if char in ('\r', '\n'):
|
||||
click.echo()
|
||||
return True
|
||||
|
||||
normalizedChar = char.lower()
|
||||
if normalizedChar == 'y':
|
||||
click.echo(char)
|
||||
return True
|
||||
if normalizedChar == 'n':
|
||||
click.echo(char)
|
||||
return False
|
||||
if char in ('\x03', '\x04'):
|
||||
raise click.Abort()
|
||||
|
||||
click.echo("\nPlease respond with 'y' or 'n': ", nl=False)
|
||||
|
||||
|
||||
def ensureUnmuxOutputDirectory(context, outputDirectory):
|
||||
resolvedOutputDirectory = os.path.expanduser(str(outputDirectory).strip())
|
||||
if not resolvedOutputDirectory:
|
||||
@@ -318,11 +347,7 @@ def ensureUnmuxOutputDirectory(context, outputDirectory):
|
||||
if context.get('dry_run', False):
|
||||
return False
|
||||
|
||||
if not click.confirm(
|
||||
"Create unmux output directory and missing parents: "
|
||||
+ resolvedOutputDirectory,
|
||||
default=True,
|
||||
):
|
||||
if not confirmUnmuxOutputDirectoryCreation(resolvedOutputDirectory):
|
||||
raise click.ClickException("Unmux output directory creation aborted by user.")
|
||||
|
||||
os.makedirs(resolvedOutputDirectory, exist_ok=True)
|
||||
|
||||
@@ -104,6 +104,24 @@ class UnmuxOutputDirectoryTests(unittest.TestCase):
|
||||
self.assertTrue(output_directory.is_dir())
|
||||
mocked_confirm.assert_called_once()
|
||||
|
||||
def test_tty_carriage_return_accepts_default_directory_creation(self):
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
output_directory = Path(tempdir) / "missing" / "manual"
|
||||
|
||||
with patch("ffx.cli.sys.stdin.isatty", return_value=True), patch(
|
||||
"ffx.cli.click.getchar",
|
||||
return_value="\r",
|
||||
) as mocked_getchar, patch("ffx.cli.click.confirm") as mocked_confirm:
|
||||
created = cli.ensureUnmuxOutputDirectory(
|
||||
{"dry_run": False},
|
||||
str(output_directory),
|
||||
)
|
||||
|
||||
self.assertTrue(created)
|
||||
self.assertTrue(output_directory.is_dir())
|
||||
mocked_getchar.assert_called_once()
|
||||
mocked_confirm.assert_not_called()
|
||||
|
||||
def test_missing_output_directory_can_be_rejected(self):
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
output_directory = Path(tempdir) / "missing" / "manual"
|
||||
|
||||
Reference in New Issue
Block a user