1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import click
import pytest
from click.testing import CliRunner
from pathvalidate.click import (
sanitize_filename_arg,
sanitize_filepath_arg,
validate_filename_arg,
validate_filepath_arg,
)
@click.command()
@click.option("--filename", callback=validate_filename_arg)
@click.option("--filepath", callback=validate_filepath_arg)
def cli_validate(filename, filepath):
if filename:
click.echo(filename)
if filepath:
click.echo(filepath)
@click.command()
@click.option("--filename", callback=sanitize_filename_arg)
@click.option("--filepath", callback=sanitize_filepath_arg)
def cli_sanitize(filename, filepath):
if filename:
click.echo(filename)
if filepath:
click.echo(filepath)
class Test_validate:
@pytest.mark.parametrize(
["value", "expected"],
[
["ab", 0],
["", 0],
["a/b", 2],
["a/?b", 2],
],
)
def test_normal_filename(self, value, expected):
runner = CliRunner()
result = runner.invoke(cli_validate, ["--filename", value])
assert result.exit_code == expected
@pytest.mark.parametrize(
["value", "expected"],
[
["ab", 0],
["", 0],
["a/b", 0],
["a/?b", 2],
],
)
def test_normal_filepath(self, value, expected):
runner = CliRunner()
result = runner.invoke(cli_validate, ["--filepath", value])
assert result.exit_code == expected
class Test_sanitize:
@pytest.mark.parametrize(
["value", "expected"],
[
["ab", "ab"],
["", ""],
["a/b", "ab"],
["a/?b", "ab"],
],
)
def test_normal_filename(self, value, expected):
runner = CliRunner()
result = runner.invoke(cli_sanitize, ["--filename", value])
assert result.output.strip() == expected
@pytest.mark.parametrize(
["value", "expected"],
[
["ab", "ab"],
["", ""],
["a/b", "a/b"],
["a/?b", "a/b"],
],
)
def test_normal_filepath(self, value, expected):
runner = CliRunner()
result = runner.invoke(cli_sanitize, ["--filepath", value])
assert result.output.strip() == expected
|