project-spec 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,128 +0,0 @@
1
- import json
2
- import subprocess
3
- import sys
4
- import tempfile
5
- import unittest
6
- from contextlib import redirect_stderr, redirect_stdout
7
- from io import StringIO
8
- from pathlib import Path
9
-
10
- sys.path.insert(0, str(Path(__file__).resolve().parent))
11
- import coverage as c # noqa: E402
12
-
13
-
14
- class CoverageTests(unittest.TestCase):
15
- def setUp(self):
16
- self.temp = tempfile.TemporaryDirectory()
17
- self.repo = Path(self.temp.name).resolve() / 'repo'
18
- self.vault = self.repo / 'knowledge/demo'
19
- (self.vault / 'notes').mkdir(parents=True)
20
- (self.vault / 'areas').mkdir()
21
- self.page('00-index.md', 'index', 'index', areas=[], body='[[areas/chat]]')
22
- self.page('areas/chat.md', 'chat', 'area', areas=[], body='[[notes/chats]]')
23
- self.page('notes/chats.md', 'chats', sources=['src/chat/send.ts'],
24
- body='A message is refused with "Message can’t be empty". At most 10 photos (`CHAT_MAX_MEDIA`).\n\n'
25
- '| ID | Rule |\n| --- | --- |\n| BR-CHAT-01 | Only group chats can be renamed, by a member \\| staff. |')
26
- self.code('src/chat/send.ts', '''
27
- export const CHAT_MAX_MEDIA = 10;
28
- export const COLD_OUTREACH_PER_HOUR: number = 5; // new people
29
- const RETRY_MS = 60 * 1000
30
- const queryKey = "chats.list";
31
- throw new Error("Message can't be empty");
32
- throw new Error('Only group chats can be renamed');
33
- throw new Error("You cannot remove yourself");
34
- console.error("Failed to load the chat list");
35
- const cls = "flex items-center gap-2 Text here";
36
- const sql = "SELECT id FROM chats WHERE owner = 1";
37
- ''')
38
- self.code('src/chat/window/Header.tsx', '<Button>Back to chat list</Button>\n<p>{count} unread</p>\nconst t = `Hello ${name}, welcome back`;')
39
- self.code('src/chat/window/deep/Picker.tsx', 'const label = "Smileys and people";')
40
- self.code('src/chat/send.test.ts', 'it("says this in a test only", () => {})')
41
- self.code('src/chat/__tests__/more.ts', 'const s = "Only in a test folder";')
42
- self.code('src/orders/pay.py', 'MAX_TOTAL = 5000\nmessage = "The order was already paid"\n')
43
- self.code('database/migrations/001.sql', "INSERT INTO t VALUES ('A sentence in a migration');")
44
- self.code('node_modules/x/index.js', 'const s = "A sentence in a dependency";')
45
- self.code('src/help/article.ts', '\n'.join(f'const p{i} = "This is paragraph number {i} of an article";' for i in range(45)))
46
- (self.repo / 'README.md').write_text('Not code. "A sentence in the readme file"', encoding='utf-8')
47
-
48
- def tearDown(self):
49
- self.temp.cleanup()
50
-
51
- def page(self, path, ident, ptype='note', areas=None, sources=None, body=''):
52
- meta = {'id': ident, 'title': 'Page ' + ident, 'type': ptype, 'status': 'current', 'areas': ['chat'] if areas is None else areas,
53
- 'sources': sources or [], 'updated': '2026-11-18'}
54
- (self.vault / path).write_text('---\n' + ''.join(f'{key}: {json.dumps(value)}\n' for key, value in meta.items()) + '---\n' + body, encoding='utf-8')
55
-
56
- def code(self, path, text):
57
- file = self.repo / path
58
- file.parent.mkdir(parents=True, exist_ok=True)
59
- file.write_text(text, encoding='utf-8')
60
-
61
- def run_cli(self, *args):
62
- out, err = StringIO(), StringIO()
63
- with redirect_stdout(out), redirect_stderr(err):
64
- status = c.main(['--repo', str(self.repo), *args])
65
- return status, out.getvalue(), err.getvalue()
66
-
67
- def measured(self, *prefixes):
68
- import knowledge
69
- notes = knowledge.load_vault(self.repo, self.vault, freshness=False, absent=[])[0]
70
- return c.measure(self.repo, self.vault, notes, prefixes)
71
-
72
- def test_reads_only_the_application_code(self):
73
- self.assertEqual(sorted(self.measured()), ['src/chat/send.ts', 'src/chat/window/Header.tsx', 'src/chat/window/deep/Picker.tsx',
74
- 'src/help/article.ts', 'src/orders/pay.py'])
75
- # with Git, an ignored folder is left out as well, and an untracked file is still read
76
- subprocess.run(['git', '-C', str(self.repo), 'init', '-q'], check=True)
77
- (self.repo / '.gitignore').write_text('src/orders/\n', encoding='utf-8')
78
- self.assertNotIn('src/orders/pay.py', self.measured())
79
- self.assertIn('src/chat/send.ts', self.measured())
80
-
81
- def test_finds_the_sentences_and_numbers_no_page_mentions(self):
82
- files = self.measured()
83
- send = files['src/chat/send.ts']
84
- self.assertEqual((send['cited'], send['sentences'], send['limits']), (True, 3, 3))
85
- self.assertEqual(send['unquoted'], ['You cannot remove yourself'], 'a curly apostrophe and a table row still match; logs, class lists and queries are not sentences')
86
- self.assertEqual(send['unnamed'], [('COLD_OUTREACH_PER_HOUR', '5'), ('RETRY_MS', '60 * 1000')])
87
- self.assertEqual(files['src/chat/window/Header.tsx']['unquoted'], ['Back to chat list'], 'text between tags counts; text with a placeholder does not')
88
- self.assertEqual(files['src/orders/pay.py']['unnamed'], [('MAX_TOTAL', '5000')])
89
- article = files['src/help/article.ts']
90
- self.assertEqual((article['content'], article['sentences'], article['unquoted']), (True, 0, []), 'an article counts as a file only')
91
-
92
- def test_groups_by_folder_and_keeps_an_uncited_folder_as_one_line(self):
93
- groups = c.folders(self.measured())
94
- self.assertEqual(sorted(groups), ['src/chat', 'src/chat/window', 'src/help', 'src/orders'])
95
- self.assertEqual(groups['src/chat/window'], {'files': 2, 'lines': 4, 'cited': 0, 'sentences': 2, 'unquoted': 2, 'limits': 0, 'unnamed': 0})
96
- self.assertEqual((groups['src/chat']['files'], groups['src/chat']['cited']), (1, 1))
97
- # nothing cited anywhere: one line per top folder, and never above the folder that was asked for
98
- self.page('notes/chats.md', 'chats', sources=['README.md'])
99
- self.assertEqual(sorted(c.folders(self.measured())), ['src'])
100
- self.assertEqual(sorted(c.folders(self.measured('src/chat'), ['src/chat'])), ['src/chat'])
101
-
102
- def test_report_lists_what_to_read(self):
103
- status, out, err = self.run_cli()
104
- self.assertEqual((status, err), (0, ''))
105
- self.assertIn('coverage: 5 code files, ', out)
106
- self.assertIn('pages cite 1 of them', out)
107
- self.assertIn('6 sentences a person can see, 4 on no page; 4 named numbers, 3 on no page', out)
108
- self.assertIn('1 files that are mostly text (articles, templates) count as files only', out)
109
- self.assertLess(out.index('src/chat/window'), out.index('src/help'), 'the folder with the most to read comes first')
110
- self.assertNotIn('things under', out)
111
- status, out, _ = self.run_cli('--path', 'src/chat/')
112
- self.assertIn('coverage: 3 code files', out)
113
- self.assertIn('5 things under src/chat that no page mentions:', out)
114
- self.assertIn(' src/chat/send.ts says: You cannot remove yourself', out)
115
- self.assertIn(' src/chat/send.ts sets: RETRY_MS = 60 * 1000', out)
116
- self.assertIn('2 of 3 files there are cited by no page', out)
117
- self.assertEqual(self.run_cli('--path', 'nowhere')[1].strip(), 'coverage: no code files found under nowhere')
118
-
119
- def test_a_vault_with_errors_is_reported_and_still_measured(self):
120
- self.page('notes/bad.md', 'bad', sources=['src/chat/send.ts'], body='[[missing-page]]')
121
- status, out, err = self.run_cli()
122
- self.assertEqual(status, 0)
123
- self.assertIn('the vault has 1 errors', err)
124
- self.assertIn('coverage: 5 code files', out)
125
-
126
-
127
- if __name__ == '__main__':
128
- unittest.main()