Skip to content

Commit 9184d67

Browse files
committed
Fix make_twill_filename
1 parent 80a61c9 commit 9184d67

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/twill/utils.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ def get_equiv_refresh_interval() -> Optional[int]:
461461

462462
def is_hidden_filename(filename: str) -> bool:
463463
"""Check if this is a hidden file (starting with a dot)."""
464-
return filename not in (".", "..") and Path(filename).name.startswith(".")
464+
return filename not in (os.curdir, os.pardir) and Path(
465+
filename
466+
).name.startswith(".")
465467

466468

467469
def is_twill_filename(filename: str) -> bool:
@@ -471,14 +473,13 @@ def is_twill_filename(filename: str) -> bool:
471473

472474
def make_twill_filename(name: str) -> str:
473475
"""Add the twill extension to the name of a script if necessary."""
474-
if name not in (".", ".."):
476+
if name not in (os.curdir, os.pardir):
475477
path = Path(name)
476-
twill_name = path.stem
477-
ext = path.suffix
478-
if not ext:
479-
twill_name += twill_ext
480-
if Path(twill_name).exists():
481-
name = twill_name
478+
if not (path.suffix or (path.exists() and not path.is_dir())):
479+
twill_path = path.with_suffix(twill_ext)
480+
if twill_path.exists() and not twill_path.is_dir():
481+
path = twill_path
482+
name = str(path)
482483
return name
483484

484485

tests/test_gather.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_gather_dir():
1010

1111
os.chdir(test_dir)
1212
try:
13-
files = gather_filenames((".",))
13+
files = gather_filenames((os.curdir,))
1414
if os.sep != "/":
1515
files = [f.replace(os.sep, "/") for f in files]
1616
assert sorted(files) == sorted(

tests/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
from pathlib import Path
2+
13
import pytest
24
from twill import utils
35
from twill.errors import TwillException
46

57

8+
def test_is_hidden_filename():
9+
is_hidden_filename = utils.is_hidden_filename
10+
assert not is_hidden_filename("foo")
11+
assert is_hidden_filename(".foo")
12+
assert not is_hidden_filename(".foo/bar")
13+
assert is_hidden_filename("foo/.bar")
14+
15+
16+
def test_is_twill_filename():
17+
is_twill_filename = utils.is_twill_filename
18+
assert not is_twill_filename("foo")
19+
assert is_twill_filename("foo.twill")
20+
assert not is_twill_filename(".foo.twill")
21+
22+
623
def test_make_boolean():
724
make_boolean = utils.make_boolean
825
assert make_boolean(True) # noqa: FBT003
@@ -19,6 +36,18 @@ def test_make_boolean():
1936
make_boolean("no")
2037

2138

39+
def test_make_twill_filename():
40+
make_twill_filename = utils.make_twill_filename
41+
assert make_twill_filename("test_foo") == "test_foo"
42+
assert make_twill_filename("../tests/test_foo") == str(
43+
Path("../tests/test_foo")
44+
)
45+
assert make_twill_filename("test_basic") == "test_basic.twill"
46+
assert make_twill_filename("../tests/test_basic") == str(
47+
Path("../tests/test_basic.twill")
48+
)
49+
50+
2251
def test_trunc():
2352
trunc = utils.trunc
2453
assert trunc("hello, world!", 12) == "hello, w ..."

0 commit comments

Comments
 (0)