Skip to content

Commit

Permalink
what garble WindowsPath
Browse files Browse the repository at this point in the history
- test: attempt to diagnose WindowsPath getting garbled
  • Loading branch information
msftcangoblowm committed Aug 30, 2024
1 parent 399ac09 commit b00fd8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
33 changes: 22 additions & 11 deletions tests/test_cli_textconv_with_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def func(path_cwd):

key = "diff.inv.textconv"
soi_textconv_path = "sphobjinv-textconv"

# On Windows, resolved executables paths
resolved_soi_textconv_path = shutil.which(soi_textconv_path)
if resolved_soi_textconv_path is None:
resolved_soi_textconv_path = soi_textconv_path
Expand All @@ -93,10 +95,7 @@ def func(path_cwd):
logger_.info(msg_info)

# On Windows, executable's path must be resolved
msg_info = (
""".git/config diff textconv executable's path: """
f"{resolved_soi_textconv_path}"
)
msg_info = f""".git/config diff textconv executable's path: {val}"""
logger_.info(msg_info)

path_git_dir_dst = path_cwd / ".git"
Expand All @@ -112,6 +111,12 @@ def func(path_cwd):
reason = f"Unable to set git config setting {key} to {val}"
assert is_success is True, reason

if is_win:
# .git/config after update
gc_contents = path_git_config_dst.read_text()
msg_info = f""".git/config (after update):{os.linesep}{gc_contents}"""
logger_.info(msg_info)

return func


Expand Down Expand Up @@ -161,14 +166,15 @@ def test_textconv_git_diff(
if is_win or is_linux:
msg_info = f"cwd {wd.cwd!s}"
logger.info(msg_info)
if is_win:
from pathlib import WindowsPath

# On Windows, unresolved executables paths
soi_textconv_path = "sphobjinv-textconv"

# On Windows, resolved executables paths
resolved_soi_textconv_path = shutil.which(soi_textconv_path)
if resolved_soi_textconv_path is None:
resolved_soi_textconv_path = soi_textconv_path
soi_textconv_path = "sphobjinv-textconv"
str_path = shutil.which(soi_textconv_path)
if str_path is not None:
logger.info(str_path)
msg_info = str(WindowsPath(str_path))
logger.info(msg_info)

# git init
wd("git init")
Expand All @@ -177,6 +183,11 @@ def test_textconv_git_diff(

# Into .git/config, set the textconv absolute path
gitconfig(wd.cwd)
if is_win or is_linux:
key = "diff.inv.textconv"
gc_val = wd.git_config_get(key)
msg_info = f".git/config {key} --> {gc_val}"
logging.info(msg_info)

# .gitattributes
# Informs git: .inv are binary files uses textconv "inv" from .git/config
Expand Down
23 changes: 17 additions & 6 deletions tests/wd_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WorkDir
import platform
import shlex
import subprocess as sp # noqa: S404
from pathlib import Path
from pathlib import Path, WindowsPath
from typing import Dict, List


Expand Down Expand Up @@ -257,8 +257,12 @@ def git_config_list(self) -> Dict[str, str]:
str_blob = cp_out.stdout
key_val_pairs = str_blob.split(os.linesep)
for key_val_pair in key_val_pairs:
key, val = key_val_pair.split("=")
d_ret[key] = val
if len(key_val_pair) != 0:
pair = key_val_pair.split("=")
if len(pair) == 2:
key = pair[0]
val = pair[1]
d_ret[key] = val
else:
# no git config settings? Hmmm ...
pass
Expand Down Expand Up @@ -331,16 +335,23 @@ def git_config_set(
"""
is_win = platform.system().lower() == "windows"
cmd = [
"git",
"config",
dotted_key,
]
if is_path and is_win:
# In Bash, single quotes protect (Windows path) backslashes
# Does not deal with escaping spaces
val = f"'{val}'"

# `Path is Windows safe <https://stackoverflow.com/a/68555279>`_
escaped_val = "'" + str(WindowsPath(val)) + "'"
cmd.append(escaped_val)
else:
cmd.append(val)
# Sphinx hates \$
# It's non-obvious if the above solution is viable.
# git config diff.inv.textconv "sh -c 'sphobjinv co plain \"\$0\" -'"
# git config diff.inv.textconv "sh -c 'sphobjinv-textconv \"\$0\"'"
cmd = f"git config {dotted_key} '{val}'"
cp_out = run(cmd, cwd=self.cwd)
if cp_out is None or cp_out.returncode != 0:
ret = False
Expand Down

0 comments on commit b00fd8f

Please sign in to comment.