|
1 | | -import { normalizePathCase, isFileSystemCaseSensitive } from 'pyright-internal/common/pathUtils'; |
| 1 | +import { normalizePathCase } from 'pyright-internal/common/pathUtils'; |
2 | 2 | import { PyrightFileSystem } from 'pyright-internal/pyrightFileSystem'; |
3 | 3 | import { createFromRealFileSystem } from 'pyright-internal/common/realFileSystem'; |
4 | 4 |
|
5 | | -export enum SeenCondition { |
6 | | - AlwaysFalse = 'always-false', |
7 | | - AlwaysTrue = 'always-true', |
8 | | - Mixed = 'mixed', |
9 | | -} |
10 | | - |
11 | | -export class AssertionError extends Error { |
12 | | - constructor(message: string) { |
13 | | - super(message); |
14 | | - this.name = 'AssertionError'; |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -// Private global state - never export directly |
19 | | -let _assertionFlags = { |
20 | | - pathNormalizationChecks: false, |
21 | | - otherChecks: false, |
22 | | -}; |
23 | | -let _context = ''; |
24 | | -const _sometimesResults = new Map<string, Map<string, SeenCondition>>(); |
25 | | - |
26 | | -export function setGlobalAssertionFlags(pathNormalizationChecks: boolean, otherChecks: boolean): void { |
27 | | - _assertionFlags.pathNormalizationChecks = pathNormalizationChecks; |
28 | | - _assertionFlags.otherChecks = otherChecks; |
29 | | -} |
30 | | - |
31 | | -export function setGlobalContext(context: string): void { |
32 | | - _context = context; |
33 | | -} |
34 | | - |
35 | | -// Internal implementation functions |
36 | | -function assertAlwaysImpl(enableFlag: boolean, check: () => boolean, message: () => string): void { |
37 | | - if (!enableFlag) return; |
38 | | - |
39 | | - if (!check()) { |
40 | | - throw new AssertionError(message()); |
41 | | - } |
42 | | -} |
43 | | - |
44 | | -function assertSometimesImpl(enableFlag: boolean, check: () => boolean, key: string): void { |
45 | | - if (!enableFlag) return; |
46 | | - |
47 | | - const ctx = _context; |
48 | | - if (ctx === '') { |
49 | | - throw new AssertionError('Context must be set before calling assertSometimes'); |
50 | | - } |
51 | | - |
52 | | - let ctxMap = _sometimesResults.get(key); |
53 | | - if (!ctxMap) { |
54 | | - ctxMap = new Map(); |
55 | | - _sometimesResults.set(key, ctxMap); |
56 | | - } |
57 | | - |
58 | | - const result = check() ? SeenCondition.AlwaysTrue : SeenCondition.AlwaysFalse; |
59 | | - const prev = ctxMap.get(ctx); |
60 | | - |
61 | | - if (prev === undefined) { |
62 | | - ctxMap.set(ctx, result); |
63 | | - } else if (prev !== result) { |
64 | | - ctxMap.set(ctx, SeenCondition.Mixed); |
65 | | - } |
66 | | -} |
67 | | - |
68 | 5 | const _fs = new PyrightFileSystem(createFromRealFileSystem()); |
| 6 | +const sometimesResults = new Map<string, Set<boolean>>(); |
69 | 7 |
|
70 | | -export function assertAlways(check: () => boolean, message: () => string): void { |
71 | | - assertAlwaysImpl(_assertionFlags.otherChecks, check, message); |
72 | | -} |
73 | | - |
74 | | -export function assertSometimes(check: () => boolean, key: string): void { |
75 | | - assertSometimesImpl(_assertionFlags.otherChecks, check, key); |
76 | | -} |
| 8 | +// Only enable assertions in test mode |
| 9 | +const isTestMode = process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID !== undefined; |
77 | 10 |
|
78 | 11 | export function assertNeverNormalized(path: string): void { |
| 12 | + if (!isTestMode) return; |
| 13 | + |
79 | 14 | const normalized = normalizePathCase(_fs, path); |
80 | | - assertAlwaysImpl( |
81 | | - _assertionFlags.pathNormalizationChecks, |
82 | | - () => normalized !== path, |
83 | | - () => `Path should not be normalized but was: ${path}` |
84 | | - ); |
| 15 | + if (normalized === path) { |
| 16 | + throw new Error(`Path should not be normalized but was: ${path}`); |
| 17 | + } |
85 | 18 | } |
86 | 19 |
|
87 | 20 | export function assertAlwaysNormalized(path: string): void { |
| 21 | + if (!isTestMode) return; |
| 22 | + |
88 | 23 | const normalized = normalizePathCase(_fs, path); |
89 | | - assertAlwaysImpl( |
90 | | - _assertionFlags.pathNormalizationChecks, |
91 | | - () => normalized === path, |
92 | | - () => `Path should be normalized but was not: ${path} -> ${normalized}` |
93 | | - ); |
| 24 | + if (normalized !== path) { |
| 25 | + throw new Error(`Path should be normalized but was not: ${path} -> ${normalized}`); |
| 26 | + } |
94 | 27 | } |
95 | 28 |
|
96 | 29 | export function assertSometimesNormalized(path: string, key: string): void { |
| 30 | + if (!isTestMode) return; |
| 31 | + |
97 | 32 | const normalized = normalizePathCase(_fs, path); |
98 | | - assertSometimesImpl(_assertionFlags.pathNormalizationChecks, () => normalized === path, key); |
99 | | -} |
| 33 | + const isNormalized = normalized === path; |
100 | 34 |
|
101 | | -// Monoidal combination logic |
102 | | -function combine(a: SeenCondition, b: SeenCondition): SeenCondition { |
103 | | - if (a === b) return a; |
104 | | - if (a === SeenCondition.Mixed || b === SeenCondition.Mixed) { |
105 | | - return SeenCondition.Mixed; |
| 35 | + if (!sometimesResults.has(key)) { |
| 36 | + sometimesResults.set(key, new Set()); |
106 | 37 | } |
107 | | - // AlwaysTrue + AlwaysFalse = Mixed |
108 | | - return SeenCondition.Mixed; |
| 38 | + sometimesResults.get(key)!.add(isNormalized); |
109 | 39 | } |
110 | 40 |
|
111 | | -export function checkSometimesAssertions(): Map<string, SeenCondition> { |
112 | | - const summary = new Map<string, SeenCondition>(); |
| 41 | +export function checkSometimesAssertions(): void { |
| 42 | + if (!isTestMode) return; |
113 | 43 |
|
114 | | - for (const [key, ctxMap] of _sometimesResults) { |
115 | | - let agg: SeenCondition | undefined; |
116 | | - for (const state of ctxMap.values()) { |
117 | | - agg = agg === undefined ? state : combine(agg, state); |
118 | | - if (agg === SeenCondition.Mixed) break; |
119 | | - } |
120 | | - if (agg !== undefined) { |
121 | | - summary.set(key, agg); |
| 44 | + for (const [key, values] of sometimesResults) { |
| 45 | + // We should see both true and false for "sometimes" assertions |
| 46 | + if (values.size <= 1) { |
| 47 | + console.warn(`Assertion '${key}' was not mixed across test contexts`); |
122 | 48 | } |
123 | 49 | } |
124 | | - |
125 | | - return summary; |
| 50 | + sometimesResults.clear(); |
126 | 51 | } |
0 commit comments