File size: 1,899 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
from pathlib import Path
import subprocess
import tempfile
import unittest
from tools.workspace.quick import select

class QuickTests(unittest.TestCase):
    def test_selection_and_fallback(self):
        with tempfile.TemporaryDirectory() as folder:
            root = Path(folder)
            def git(*args):
                return subprocess.check_output(['git', '-C', folder, '-c', 'user.name=test', '-c', 'user.email=test@example.invalid', *args], text=True)
            def write(name, text):
                path = root / name; path.parent.mkdir(parents=True, exist_ok=True); path.write_text(text)
            git('init', '-q')
            write('tools/workspace/entrypoints.json', json.dumps({'schema': 1, 'modules': ['np.x'], 'files': []}))
            for name, text in {'np/x.py': 'def value(): return 1\n', 'np/y.py': 'VALUE = 1\n',
                               'tests/test_x.py': 'def test_pair(): pass\n', 'tests/test_literal.py': "PATH = 'np/y.py'\n",
                               'tests/test_static.py': 'from np.x import value\ndef test_value(): assert value() == 2\n'}.items(): write(name, text)
            git('add', '.'); git('commit', '-qm', 'base')
            write('np/x.py', 'def value(): return 2\n'); write('np/y.py', 'VALUE = 2\n')
            git('add', '.'); git('commit', '-qm', 'python')
            self.assertEqual(select(root, 'HEAD^'), ['tests/test_literal.py', 'tests/test_static.py', 'tests/test_x.py'])
            for name, text, error in [('np/uncovered.py', 'X = 1\n', '未找到测试'), ('np/config.json', '{}', '非 Python')]:
                write(name, text); git('add', '.'); git('commit', '-qm', name)
                with self.assertRaisesRegex(ValueError, error): select(root, 'HEAD^')
            write('docs/note.md', 'docs'); git('add', '.'); git('commit', '-qm', 'docs')
            self.assertEqual(select(root, 'HEAD^'), [])