Skip to content
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
40 changes: 39 additions & 1 deletion fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,46 @@ def _cd_into_file(command: str, error: str, cwd: str) -> Optional[dict]:
"command": f"cd {_quote(folder)}"}


def _mkdir_missing_parents(command: str, error: str) -> Optional[dict]:
words = _words(command)
if not words or words[0] != "mkdir" or "-p" in words:
return None
lowered = error.lower()
if "no such file or directory" not in lowered:
return None
return {"explanation": "mkdir creates only one level at a time. -p makes the missing "
"parent folders too, and does nothing if they already exist.",
"command": f"mkdir -p {' '.join(_quote(w) for w in words[1:])}"}


def _dpkg_interrupted(command: str, error: str) -> Optional[dict]:
lowered = error.lower()
if "dpkg was interrupted" not in lowered or "--configure -a" not in lowered:
return None
return {"explanation": "A previous package install was interrupted, so dpkg refuses new "
"work until it finishes the pending configuration. Run the suggested "
"command, then your original one.",
"command": f"sudo dpkg --configure -a && {command}"}


def _windows_line_endings(command: str, error: str) -> Optional[dict]:
lowered = error.lower()
if "bad interpreter" not in lowered and "^m" not in lowered:
return None
words = _words(command)
if not words or not words[0].endswith(".sh"):
return None
script = words[0]
rest = " ".join(_quote(w) for w in words[1:])
tail = f" {rest}" if rest else ""
return {"explanation": "The script was saved on Windows, and its line endings confuse "
"Linux. Strip them, then run it again.",
"command": f"perl -pi -e 's/\\r$//' {_quote(script)} && {_quote(script)}{tail}"}


RULES = [_typo_in_command, _not_executable, _apt_update, _needs_sudo, _folder_needs_r,
_git_push_rejected, _git_identity, _pip_externally_managed, _port_in_use]
_git_push_rejected, _git_identity, _pip_externally_managed, _port_in_use,
_mkdir_missing_parents, _dpkg_interrupted, _windows_line_endings]


def offline_fix(command: str, error: str, cwd: Optional[str] = None,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def _fix(folder, command, error):
("cd Documents/plan.txt", "bash: cd: Documents/plan.txt: Not a directory", "cd Documents"),
("cd Documents/plan.txt/old", "bash: cd: Documents/plan.txt/old: Not a directory",
"cd Documents"),
("mkdir projects/new/app", "mkdir: cannot create directory 'projects/new/app': "
"No such file or directory", "mkdir -p projects/new/app"),
("sudo apt install x", "E: dpkg was interrupted, you must manually run "
"'sudo dpkg --configure -a' to correct the problem.",
"sudo dpkg --configure -a && sudo apt install x"),
("./script.sh", "bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory",
"perl -pi -e 's/\\r$//' ./script.sh && ./script.sh"),
])
def test_offline_fixes(folder, command, error, fixed):
(folder / "Documents" / "plan.txt").write_text("")
Expand All @@ -79,6 +86,8 @@ def test_cd_into_a_file_here_gets_advice(folder):

def test_new_rules_stay_out_of_the_way(folder):
# Other git and pip failures aren't these rules' business.
assert _fix(folder, "mkdir -p a/b", "mkdir: cannot create directory 'a/b': "
"No such file or directory")["command"] == ""
assert _fix(folder, "git pull", "fatal: not a git repository")["command"] == ""
assert _fix(folder, "pip list", "error: externally-managed-environment")["command"] == ""
assert _fix(folder, "ls notes.txt/x", "ls: cannot access 'notes.txt/x': Not a directory"
Expand Down
Loading