Skip to content

Commit

Permalink
Fix handling of MacOS grep return for CLI completers
Browse files Browse the repository at this point in the history
Fixes #92
  • Loading branch information
WarmCyan committed Oct 5, 2023
1 parent ae17cf2 commit 80da3b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions curifactory/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,15 @@ def experiments_completer(**kwargs) -> list[str]:
.stdout.decode("utf-8")[:-1]
.split("\n")
)
# handle if there's a ./ at the beginning (this happens with macOS's version
# of grep)
for index, file in enumerate(files):
if file.startswith("./"):
files[index] = file[2:]

# remove .py
files = [file[:-3] for file in files if file != ""]

files.sort()
return [filename.replace("/", ".") for filename in files]

Expand All @@ -1072,6 +1080,13 @@ def params_completer(**kwargs) -> list[str]:
.stdout.decode("utf-8")[:-1]
.split("\n")
)
# handle if there's a ./ at the beginning (this happens with macOS's version
# of grep)
for index, file in enumerate(experiment_files):
if file.startswith("./"):
experiment_files[index] = file[2:]

# remove .py
experiment_files = [file[:-3] for file in experiment_files if file != ""]

param_files = (
Expand All @@ -1083,7 +1098,15 @@ def params_completer(**kwargs) -> list[str]:
.stdout.decode("utf-8")[:-1]
.split("\n")
)
# handle if there's a ./ at the beginning (this happens with macOS's version
# of grep)
for index, file in enumerate(param_files):
if file.startswith("./"):
param_files[index] = file[2:]

# remove .py
param_files = [file[:-3] for file in param_files if file != ""]

files = param_files + experiment_files
files.sort()
return [filename.replace("/", ".") for filename in files]
Expand Down
22 changes: 22 additions & 0 deletions test/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,25 @@ def test_experiments_completer():
def test_params_completer():
output = params_completer()
assert output == ["empty", "nonarrayargs", "params1", "params2", "subparams.thing"]


def test_macos_experiment_completer(mocker): # noqa: F811
"""The BSD verison of grep on macOS puts './' at the beginning of returned paths,
we should handle this appropriately"""

mock = mocker.patch("subprocess.run")
mock.return_value.stdout = b"./basic.py\n./subexp/example.py\n"

output = experiments_completer()
assert output == ["basic", "subexp.example"]


def test_macos_params_completer(mocker): # noqa: F811
"""The BSD verison of grep on macOS puts './' at the beginning of returned paths,
we should handle this appropriately"""

mock = mocker.patch("subprocess.run")
mock.return_value.stdout = b"./empty.py\n./subparams/thing.py\n"

output = params_completer()
assert output == ["empty", "empty", "subparams.thing", "subparams.thing"]

0 comments on commit 80da3b7

Please sign in to comment.