Skip to content

Commit 7056a5a

Browse files
Merge pull request #175 from weilycoder/fix1
2 parents 69c7b94 + 6e7637b commit 7056a5a

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

cyaron/tests/compare_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
import tempfile
66
import subprocess
7-
from cyaron import IO, Compare, log
7+
from cyaron import IO, Compare, log, escape_path
88
from cyaron.output_capture import captured_output
99
from cyaron.graders.mismatch import *
1010
from cyaron.compare import CompareMismatch
@@ -83,13 +83,13 @@ def test_fulltext_program(self):
8383

8484
try:
8585
with captured_output() as (out, err):
86-
Compare.program(f"{sys.executable} correct.py",
87-
f"{sys.executable} incorrect.py",
86+
Compare.program(f"{escape_path(sys.executable)} correct.py",
87+
f"{escape_path(sys.executable)} incorrect.py",
8888
std=io,
8989
input=io,
9090
grader="FullText")
9191
except CompareMismatch as e:
92-
self.assertEqual(e.name, f'{sys.executable} incorrect.py')
92+
self.assertEqual(e.name, f'{escape_path(sys.executable)} incorrect.py')
9393
e = e.mismatch
9494
self.assertEqual(e.content, '2\n')
9595
self.assertEqual(e.std, '1\n')
@@ -105,7 +105,7 @@ def test_fulltext_program(self):
105105
self.assertTrue(False)
106106

107107
result = out.getvalue().strip()
108-
correct_out = f'{sys.executable} correct.py: Correct \n{sys.executable} incorrect.py: !!!INCORRECT!!! Hash mismatch: read 53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3, expected 4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865'
108+
correct_out = f'{escape_path(sys.executable)} correct.py: Correct \n{escape_path(sys.executable)} incorrect.py: !!!INCORRECT!!! Hash mismatch: read 53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3, expected 4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865'
109109
self.assertEqual(result, correct_out)
110110

111111
def test_file_input(self):
@@ -122,13 +122,13 @@ def test_file_input(self):
122122
io.input_writeln("233")
123123

124124
with captured_output() as (out, err):
125-
Compare.program(f"{sys.executable} correct.py",
126-
std_program=f"{sys.executable} std.py",
125+
Compare.program(f"{escape_path(sys.executable)} correct.py",
126+
std_program=f"{escape_path(sys.executable)} std.py",
127127
input=io,
128128
grader="NOIPStyle")
129129

130130
result = out.getvalue().strip()
131-
correct_out = f'{sys.executable} correct.py: Correct'
131+
correct_out = f'{escape_path(sys.executable)} correct.py: Correct'
132132
self.assertEqual(result, correct_out)
133133

134134
def test_concurrent(self):

cyaron/tests/io_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import shutil
66
import tempfile
77
import subprocess
8-
from cyaron import IO
8+
from cyaron import IO, escape_path
99
from cyaron.output_capture import captured_output
1010

1111

@@ -92,7 +92,7 @@ def test_output_gen_time_limit_exceeded(self):
9292
abs_input_filename: str = os.path.abspath(input_filename)
9393
with self.assertRaises(subprocess.TimeoutExpired):
9494
test.input_writeln(abs_input_filename)
95-
test.output_gen(f'"{sys.executable}" long_time.py',
95+
test.output_gen(f'{escape_path(sys.executable)} long_time.py',
9696
time_limit=TIMEOUT)
9797
time.sleep(WAIT_TIME)
9898
try:
@@ -108,7 +108,7 @@ def test_output_gen_time_limit_not_exceeded(self):
108108
"print(1)")
109109

110110
with IO("test_gen.in", "test_gen.out") as test:
111-
test.output_gen(f'"{sys.executable}" short_time.py',
111+
test.output_gen(f'{escape_path(sys.executable)} short_time.py',
112112
time_limit=0.5)
113113
with open("test_gen.out", encoding="utf-8") as f:
114114
output = f.read()

cyaron/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Some utility functions."""
2+
import os
3+
import shlex
24
import sys
35
import random
46
from typing import cast, Any, Dict, Iterable, Tuple, Union
57

68
__all__ = [
79
"ati", "list_like", "int_like", "strtolines", "make_unicode",
8-
"unpack_kwargs", "process_args"
10+
"unpack_kwargs", "process_args", "escape_path"
911
]
1012

1113

@@ -79,3 +81,10 @@ def process_args():
7981
for s in sys.argv:
8082
if s.startswith("--randseed="):
8183
random.seed(s.split("=")[1])
84+
85+
def escape_path(path: str) -> str:
86+
"""Escape the path."""
87+
if os.name == 'nt':
88+
return '"' + path.replace('\\', '/') + '"'
89+
else:
90+
return shlex.quote(path)

0 commit comments

Comments
 (0)