Skip to content

Commit 968257b

Browse files
committed
fix windows tests
1 parent d1fbed0 commit 968257b

File tree

2 files changed

+69
-27
lines changed

2 files changed

+69
-27
lines changed

tests/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from django.utils.translation import gettext_lazy as _
1515

1616
track_file = str(track_file.relative_to(Path(os.getcwd())))
17-
system_cmd = str(system_cmd.relative_to(Path(os.getcwd())))
17+
system_cmd = ("python", str(system_cmd.relative_to(Path(os.getcwd()))))
1818

1919
USE_TZ = True
2020

@@ -91,13 +91,13 @@
9191
("track", "0"), priority=1, switches=("import",), options={"verbosity": 0}
9292
),
9393
RoutineCommand(("track", "1"), priority=4),
94-
SystemCommand((system_cmd, "sys 2"), priority=8),
94+
SystemCommand((*system_cmd, "sys 2"), priority=8),
9595
)
9696

9797
command("import", "track", "3", priority=3, demo=2)
9898
command("import", "track", "4", priority=3, demo=6, flag=True)
9999
command("import", "track", "5", priority=6, switches=["demo"])
100-
system("import", system_cmd, "sys 1", priority=7)
100+
system("import", *system_cmd, "sys 1", priority=7)
101101

102102

103103
names = set()

tests/test_core.py

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55
from typing import Type, TYPE_CHECKING, TypeVar
66
from tests.django_routines_tests.models import TestModel as _TestModel
7+
from collections import Counter
8+
import math
79

810
from django.core.management import call_command, CommandError
911
from django_typer.management import get_command, TyperCommand
@@ -23,6 +25,40 @@
2325
TestError,
2426
)
2527

28+
WORD = re.compile(r"\w+")
29+
30+
31+
def get_cosine(vec1, vec2):
32+
intersection = set(vec1.keys()) & set(vec2.keys())
33+
numerator = sum([vec1[x] * vec2[x] for x in intersection])
34+
35+
sum1 = sum([vec1[x] ** 2 for x in list(vec1.keys())])
36+
sum2 = sum([vec2[x] ** 2 for x in list(vec2.keys())])
37+
denominator = math.sqrt(sum1) * math.sqrt(sum2)
38+
39+
if not denominator:
40+
return 0.0
41+
else:
42+
return float(numerator) / denominator
43+
44+
45+
def text_to_vector(text):
46+
words = WORD.findall(text)
47+
return Counter(words)
48+
49+
50+
def similarity(text1, text2):
51+
"""
52+
Compute the cosine similarity between two texts.
53+
https://en.wikipedia.org/wiki/Cosine_similarity
54+
55+
We use this to lazily evaluate the output of --help to our
56+
renderings.
57+
#"""
58+
vector1 = text_to_vector(text1)
59+
vector2 = text_to_vector(text2)
60+
return get_cosine(vector1, vector2)
61+
2662

2763
manage_py = Path(__file__).parent.parent / "manage.py"
2864

@@ -347,8 +383,8 @@ def test_list(self, no_color=True):
347383
"[3] track 4 (demo=6, flag=True)",
348384
"[4] track 1",
349385
"[6] track 5 | demo",
350-
f"[7] tests{os.sep}system_cmd.py sys 1",
351-
f"[8] tests{os.sep}system_cmd.py sys 2",
386+
f"[7] python tests{os.sep}system_cmd.py sys 1",
387+
f"[8] python tests{os.sep}system_cmd.py sys 2",
352388
],
353389
)
354390

@@ -361,8 +397,8 @@ def test_list(self, no_color=True):
361397
"[3] track 3 (demo=2)",
362398
"[3] track 4 (demo=6, flag=True)",
363399
"[4] track 1",
364-
f"[7] tests{os.sep}system_cmd.py sys 1",
365-
f"[8] tests{os.sep}system_cmd.py sys 2",
400+
f"[7] python tests{os.sep}system_cmd.py sys 1",
401+
f"[8] python tests{os.sep}system_cmd.py sys 2",
366402
],
367403
)
368404

@@ -377,8 +413,8 @@ def test_list(self, no_color=True):
377413
"[3] track 4 (demo=6, flag=True)",
378414
"[4] track 1",
379415
"[6] track 5 | demo",
380-
f"[7] tests{os.sep}system_cmd.py sys 1",
381-
f"[8] tests{os.sep}system_cmd.py sys 2",
416+
f"[7] python tests{os.sep}system_cmd.py sys 1",
417+
f"[8] python tests{os.sep}system_cmd.py sys 2",
382418
],
383419
)
384420

@@ -394,8 +430,8 @@ def test_list(self, no_color=True):
394430
"[3] track 4 (demo=6, flag=True)",
395431
"[4] track 1",
396432
"[6] track 5 | demo",
397-
f"[7] tests{os.sep}system_cmd.py sys 1",
398-
f"[8] tests{os.sep}system_cmd.py sys 2",
433+
f"[7] python tests{os.sep}system_cmd.py sys 1",
434+
f"[8] python tests{os.sep}system_cmd.py sys 2",
399435
],
400436
)
401437

@@ -410,8 +446,8 @@ def test_list(self, no_color=True):
410446
"[3] track 3 (demo=2)",
411447
"[3] track 4 (demo=6, flag=True)",
412448
"[4] track 1",
413-
f"[7] tests{os.sep}system_cmd.py sys 1",
414-
f"[8] tests{os.sep}system_cmd.py sys 2",
449+
f"[7] python tests{os.sep}system_cmd.py sys 1",
450+
f"[8] python tests{os.sep}system_cmd.py sys 2",
415451
],
416452
)
417453

@@ -543,8 +579,8 @@ def test_subprocess(self):
543579
[3] track 4 (demo=6, flag=True)
544580
[4] track 1
545581
[6] track 5 | demo
546-
[7] tests{os.sep}system_cmd.py sys 1
547-
[8] tests{os.sep}system_cmd.py sys 2
582+
[7] python tests{os.sep}system_cmd.py sys 1
583+
[8] python tests{os.sep}system_cmd.py sys 2
548584
549585
╭─ Options ────────────────────────────────────────────────────────────────────╮
550586
│ --subprocess Run commands as subprocesses. │
@@ -584,9 +620,12 @@ def test_helps_rich(self):
584620

585621
routine = get_command("routine", TyperCommand, stdout=stdout, no_color=True)
586622
routine.print_help("./manage.py", "routine", "import")
587-
self.assertEqual(
588-
self.strip_ansi(stdout.getvalue()).strip().replace("\x08", ""),
589-
self.routine_test_help_rich.strip(),
623+
self.assertGreater(
624+
similarity(
625+
self.strip_ansi(stdout.getvalue()).strip().replace("\x08", ""),
626+
self.routine_test_help_rich.strip(),
627+
),
628+
0.99,
590629
)
591630

592631
routine_help_no_rich = """
@@ -635,8 +674,8 @@ def test_helps_rich(self):
635674
[3] track 4 (demo=6, flag=True)
636675
[4] track 1
637676
[6] track 5 | demo
638-
[7] tests{os.sep}system_cmd.py sys 1
639-
[8] tests{os.sep}system_cmd.py sys 2
677+
[7] python tests{os.sep}system_cmd.py sys 1
678+
[8] python tests{os.sep}system_cmd.py sys 2
640679
641680
Options:
642681
--subprocess Run commands as subprocesses.
@@ -665,18 +704,21 @@ def test_helps_no_rich(self):
665704
)
666705
self.assertEqual(result.returncode, 0)
667706
self.assertFalse(result.stderr)
668-
self.assertEqual(
669-
result.stdout.strip().decode(),
670-
self.routine_help_no_rich.format(script=sys.argv[0]).strip(),
707+
self.assertGreater(
708+
similarity(
709+
result.stdout.strip().decode(),
710+
self.routine_help_no_rich.format(script=sys.argv[0]).strip(),
711+
),
712+
0.99,
671713
)
672714

673715
stdout = StringIO()
674716

675717
routine = get_command("routine", TyperCommand, stdout=stdout, no_color=True)
676718
routine.print_help("./manage.py", "routine", "import")
677719
printed = stdout.getvalue().strip().replace("\x08", "")
678-
expected = self.routine_test_help_no_rich.strip()
679-
self.assertEqual(printed, expected)
720+
expected = self.routine_test_help_no_rich
721+
self.assertGreater(similarity(printed, expected), 0.99)
680722

681723
def test_settings_format(self):
682724
routines = getattr(settings, ROUTINE_SETTING)
@@ -819,13 +861,13 @@ def test_settings_format(self):
819861
"switches": ("demo",),
820862
},
821863
{
822-
"command": ("tests/system_cmd.py", "sys 1"),
864+
"command": ("python", f"tests{os.sep}system_cmd.py", "sys 1"),
823865
"kind": "system",
824866
"priority": 7,
825867
"switches": (),
826868
},
827869
{
828-
"command": ("tests/system_cmd.py", "sys 2"),
870+
"command": ("python", f"tests{os.sep}system_cmd.py", "sys 2"),
829871
"kind": "system",
830872
"priority": 8,
831873
"switches": (),

0 commit comments

Comments
 (0)