Skip to content

Commit 92dccd0

Browse files
eugenetrigubaEugene Triguba
andauthored
Only use and install mslex on Windows (#36)
* Fix lint issues where encoding was not specified * Only use and install mslex on Windows * Escape backslash in test_windows * Alter Windows CI name Since we actually have some tests other than the path sanity test, it probably makes sense to just call it a Windows test now. * Disable pylint issue with mslex import It's raising an unable to import issue with that line, which makes sense since mslex wouldn't be installed on any other platform other than Windows, but it also wouldn't be run on any other platform so it seems to be a non issue. Co-authored-by: Eugene Triguba <[email protected]>
1 parent a576185 commit 92dccd0

File tree

8 files changed

+69
-37
lines changed

8 files changed

+69
-37
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# This workflow checks that windows paths (a\b\c) work correctly when passed as args to taskipy
1+
# This workflow checks that windows paths (a\b\c) work correctly when
2+
# passed as args to taskipy and that mslex is installed.
23

3-
name: Taskipy Windows Sanity Test
4+
name: Taskipy Windows Tests
45

56
on:
67
push:
@@ -13,15 +14,15 @@ jobs:
1314
runs-on: windows-latest
1415
steps:
1516
- uses: actions/checkout@v2
16-
- name: Set up Python 3.8
17+
- name: Set up Python 3.9
1718
uses: actions/setup-python@v2
1819
with:
19-
python-version: 3.8
20+
python-version: 3.9
2021
- name: Install Poetry
2122
run: |
2223
python -m pip install --upgrade pip
2324
pip install poetry
2425
- name: Install dependencies
2526
run: poetry install
26-
- name: Test Windows-Style Paths
27-
run: poetry run task run_unittest_as_windows_sanity tests\test_windows_sanity.py
27+
- name: Test Windows
28+
run: poetry run task test_windows

poetry.lock

Lines changed: 26 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ task = 'taskipy.cli:main'
2323
python = "^3.6"
2424
toml = "^0.10.0"
2525
psutil = "^5.7.2"
26-
mslex = "^0.3.0"
26+
mslex = { version = "^0.3.0", markers = "sys_platform == 'win32'" }
2727
colorama = "^0.4.4"
2828

2929
[tool.poetry.dev-dependencies]
@@ -36,7 +36,7 @@ psutil = "^5.6.7"
3636
test = { cmd = "python -m unittest -v tests/test_*.py", help = "runs all tests" }
3737
post_test = "./task lint"
3838

39-
run_unittest_as_windows_sanity = { cmd = "python -m unittest -v", help = "this is a command that is run by our Windows CI on Github" }
39+
test_windows = { cmd = "python -m unittest -v tests\\test_windows.py", help = "this is a command that is run by our Windows CI on Github" }
4040

4141
lint = { cmd = "./task lint_pylint && ./task lint_mypy", help = "lint project with pylint for style and mypy for typings" }
4242
lint_pylint = "pylint tests taskipy"

taskipy/task_runner.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import psutil # type: ignore
44
import signal
55
import subprocess
6-
import shlex
7-
import mslex # type: ignore
86
from pathlib import Path
97
from typing import List, Union, Optional
108

@@ -13,6 +11,11 @@
1311
from taskipy.task import Task
1412
from taskipy.help import HelpFormatter
1513

14+
if platform.system() == 'Windows':
15+
import mslex as shlex # type: ignore # pylint: disable=E0401
16+
else:
17+
import shlex # type: ignore[no-redef]
18+
1619

1720
class TaskRunner:
1821
def __init__(self, cwd: Union[str, Path]):
@@ -56,11 +59,6 @@ def __run_command_and_return_exit_code(self, command: str, args: List[str] = Non
5659
if self.__project.runner is not None:
5760
command = f'{self.__project.runner} {command}'
5861

59-
def quote_arg(arg: str) -> str:
60-
if platform.system() == 'Windows':
61-
return mslex.quote(arg)
62-
return shlex.quote(arg)
63-
6462
def send_signal_to_task_process(signum: int, _frame) -> None:
6563
# pylint: disable=W0640
6664
psutil_process_wrapper = psutil.Process(process.pid)
@@ -75,7 +73,7 @@ def send_signal_to_task_process(signum: int, _frame) -> None:
7573
else:
7674
process.send_signal(signum)
7775

78-
command_with_args = ' '.join([command] + [quote_arg(arg) for arg in args])
76+
command_with_args = ' '.join([command] + [shlex.quote(arg) for arg in args])
7977
process = subprocess.Popen(command_with_args,
8078
shell=True,
8179
cwd=self.__working_dir)

tests/test_taskipy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_running_task(self):
7676
cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks')
7777
self.run_task('create_hello_txt', cwd=cwd)
7878

79-
with open(path.join(cwd, 'hello.txt'), 'r') as f:
79+
with open(path.join(cwd, 'hello.txt'), 'r', encoding='utf-8') as f:
8080
hello_file_contents = f.readline().strip()
8181

8282
self.assertEqual(hello_file_contents, 'hello, world')

tests/test_windows.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import platform
2+
3+
import unittest
4+
5+
6+
@unittest.skipIf(platform.system() != 'Windows', 'Windows only tests')
7+
class WindowsTestCase(unittest.TestCase):
8+
def test_windows_path_sanity(self):
9+
'''
10+
This test case is a no-op, and exists only to ensure that windows paths work
11+
as part of the windows sanity ci test.
12+
'''
13+
print('sanity passed - test was run')
14+
15+
def test_mslex_install(self):
16+
'''Ensure that mslex is installed on Windows'''
17+
try:
18+
import mslex # type: ignore # pylint: disable=W0611,C0415
19+
except ImportError:
20+
self.fail('Unable to import mslex')
21+
22+
def test_mslex_import(self):
23+
'''Ensure that mslex is used as shlex'''
24+
from taskipy.task_runner import shlex # pylint: disable=C0415
25+
26+
self.assertEqual(shlex.__name__, 'mslex')

tests/test_windows_sanity.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/utils/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, py_project_toml: str):
2424

2525
def generate_project(self, temp_dir: str):
2626
os.makedirs(temp_dir)
27-
with open(path.join(temp_dir, 'pyproject.toml'), 'w') as f:
27+
with open(path.join(temp_dir, 'pyproject.toml'), 'w', encoding='utf-8') as f:
2828
f.write(self._py_project_toml)
2929

3030

0 commit comments

Comments
 (0)