Skip to content

Commit

Permalink
chore: make tests running on windows.
Browse files Browse the repository at this point in the history
- update decode of `executor`.
- skip some test case that not support on windows.
- adjust test case compatible with windows.
  • Loading branch information
zlj-zz committed Dec 8, 2023
1 parent e9c236a commit 10ec180
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
20 changes: 19 additions & 1 deletion pigit/ext/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
SILENT: Final = 1 << 5 # silent mode. output will be discarded.


def _detect_encoding(data:ByteString)->str:
encodings = ['utf-8', 'gbk','latin-1','iso-8859-1']

for encoding in encodings:
try:
# TODO: It may be possible to decode, but the result is not correct.
data.decode(encoding)
return encoding
except UnicodeDecodeError:
continue

return ''


@dataclasses.dataclass
class ExecState:
"""State ctx of ~Executor."""
Expand Down Expand Up @@ -115,7 +129,11 @@ def _try_decode(self, content: ExecResType, state: "ExecState") -> ExecResType:
The decoded output if decoding is enabled, otherwise the original output.
"""
if state.decoding and content is not None and not isinstance(content, str):
return content.decode()
try:
return content.decode()
except UnicodeDecodeError:
# Default encoding may not is 'utf-8' on windows.
return content.decode(_detect_encoding(content))
else:
return content

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pylint
pytest
pytest-mock
pytest-cov
pytest-asyncio
pyinstrument
requests
setuptools
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@

TEST_PATH = os.path.dirname(__file__)

# Not support.
if sys.platform == "win32":
collect_ignore_glob = ["test_tui_input.py", "test_tui_eventloop.py"]

PYTHON_VERSION = sys.version_info[:3]
if PYTHON_VERSION < (3, 8, 5):
raise Exception(
"The current version of pigit does not support less than (Python3.8)."
)


# /opt/homebrew/opt/python@3.8/bin/python3 -m pytest
16 changes: 12 additions & 4 deletions tests/test_ext_executor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import time
import textwrap
import pytest
Expand All @@ -13,6 +14,8 @@
Executor,
)

win_skip_mark = pytest.mark.skipif(sys.platform == "win32", reason="windows skip.")


class TestExecutor:
@classmethod
Expand Down Expand Up @@ -58,6 +61,7 @@ def test_exec(self, cmd, flags, kws, expected):
assert result == expected
# mock_popen.assert_called_once_with(args=cmd, **kws)

@win_skip_mark
def test_exec_with_more(self):
print()

Expand Down Expand Up @@ -111,13 +115,17 @@ def test_exec_parallel(self):
if __name__ == '__main__':
import time
print({0})
print({0}, end='')
time.sleep(int({0}))
print({0})
print({0}, end='')
"""
)

cmds = [["python3", "-c", code.format(i)] for i in range(3, 0, -1)]
cmd = 'python3'
if self.executor.exec('python -V', flags=REPLY)[0] == 0 :
cmd ='python'

cmds = [[cmd, "-c", code.format(i)] for i in range(3, 0, -1)]
# pprint(cmds)

start_t = time.time()
Expand All @@ -129,4 +137,4 @@ def test_exec_parallel(self):
assert end_t - start_t < 4

results2 = self.executor.exec_parallel(*cmds, flags=REPLY | DECODE)
assert results2 == [(0, "", "{0}\n{0}\n".format(i)) for i in range(3, 0, -1)]
assert results2 == [(0, "", "{0}{0}".format(i)) for i in range(3, 0, -1)]
8 changes: 6 additions & 2 deletions tests/test_ext_lcstat.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from typing import Optional
import os
import time
from pathlib import Path

import pytest

from pigit.const import USER_HOME
from pigit.ext.lcstat import Counter


@pytest.mark.parametrize(
"path",
[
# os.getcwd(),
os.environ["HOME"] + "/.config",
os.getcwd(),
Path(USER_HOME, ".config")
# USER_HOME + "/.config",
# *os.environ.values(),
],
)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import stat
import shutil
import pytest
from unittest.mock import patch
Expand All @@ -18,7 +19,15 @@ def create_repo(test_repo: str):

# re-create test repo
if os.path.isdir(test_repo):
shutil.rmtree(test_repo)
shutil.rmtree(
test_repo,
onerror=lambda func, path, _: (
# Fix `PermissionError` on windows.
# Will deprecated `onerror` on Py312.
os.chmod(path, stat.S_IWRITE),
func(path),
),
)
os.makedirs(test_repo, exist_ok=True)
print(executor.exec("git init", flags=WAITING, cwd=test_repo))
print(executor.exec("git config user.name Zachary", flags=WAITING, cwd=test_repo))
Expand Down

0 comments on commit 10ec180

Please sign in to comment.