-
Notifications
You must be signed in to change notification settings - Fork 31
fix: Make tests more robust & fix path-handling bugs on macOS #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { normalizePathCase, isFileSystemCaseSensitive } from 'pyright-internal/common/pathUtils'; | ||
import { PyrightFileSystem } from 'pyright-internal/pyrightFileSystem'; | ||
import { createFromRealFileSystem } from 'pyright-internal/common/realFileSystem'; | ||
|
||
export enum SeenCondition { | ||
AlwaysFalse = 'always-false', | ||
AlwaysTrue = 'always-true', | ||
Mixed = 'mixed' | ||
} | ||
|
||
export class AssertionError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'AssertionError'; | ||
} | ||
} | ||
|
||
// Private global state - never export directly | ||
let _assertionFlags = { | ||
pathNormalizationChecks: false, | ||
otherChecks: false | ||
}; | ||
let _context = ''; | ||
const _sometimesResults = new Map<string, Map<string, SeenCondition>>(); | ||
|
||
export function setGlobalAssertionFlags(pathNormalizationChecks: boolean, otherChecks: boolean): void { | ||
_assertionFlags.pathNormalizationChecks = pathNormalizationChecks; | ||
_assertionFlags.otherChecks = otherChecks; | ||
} | ||
|
||
export function setGlobalContext(context: string): void { | ||
_context = context; | ||
} | ||
|
||
// Internal implementation functions | ||
function assertAlwaysImpl(enableFlag: boolean, check: () => boolean, message: () => string): void { | ||
if (!enableFlag) return; | ||
|
||
if (!check()) { | ||
throw new AssertionError(message()); | ||
} | ||
} | ||
|
||
function assertSometimesImpl(enableFlag: boolean, check: () => boolean, key: string): void { | ||
if (!enableFlag) return; | ||
|
||
const ctx = _context; | ||
if (ctx === '') { | ||
throw new AssertionError('Context must be set before calling assertSometimes'); | ||
} | ||
|
||
let ctxMap = _sometimesResults.get(key); | ||
if (!ctxMap) { | ||
ctxMap = new Map(); | ||
_sometimesResults.set(key, ctxMap); | ||
} | ||
|
||
const result = check() ? SeenCondition.AlwaysTrue : SeenCondition.AlwaysFalse; | ||
const prev = ctxMap.get(ctx); | ||
|
||
if (prev === undefined) { | ||
ctxMap.set(ctx, result); | ||
} else if (prev !== result) { | ||
ctxMap.set(ctx, SeenCondition.Mixed); | ||
} | ||
} | ||
|
||
const _fs = new PyrightFileSystem(createFromRealFileSystem()); | ||
|
||
export function assertAlways(check: () => boolean, message: () => string): void { | ||
assertAlwaysImpl(_assertionFlags.otherChecks, check, message); | ||
} | ||
|
||
export function assertSometimes(check: () => boolean, key: string): void { | ||
assertSometimesImpl(_assertionFlags.otherChecks, check, key); | ||
} | ||
|
||
export function assertNeverNormalized(path: string): void { | ||
const normalized = normalizePathCase(_fs, path); | ||
assertAlwaysImpl( | ||
_assertionFlags.pathNormalizationChecks, | ||
() => normalized !== path, | ||
() => `Path should not be normalized but was: ${path}` | ||
); | ||
} | ||
|
||
export function assertAlwaysNormalized(path: string): void { | ||
const normalized = normalizePathCase(_fs, path); | ||
assertAlwaysImpl( | ||
_assertionFlags.pathNormalizationChecks, | ||
() => normalized === path, | ||
() => `Path should be normalized but was not: ${path} -> ${normalized}` | ||
); | ||
} | ||
|
||
export function assertSometimesNormalized(path: string, key: string): void { | ||
const normalized = normalizePathCase(_fs, path); | ||
assertSometimesImpl( | ||
_assertionFlags.pathNormalizationChecks, | ||
() => normalized === path, | ||
key | ||
); | ||
} | ||
|
||
// Monoidal combination logic | ||
function combine(a: SeenCondition, b: SeenCondition): SeenCondition { | ||
if (a === b) return a; | ||
if (a === SeenCondition.Mixed || b === SeenCondition.Mixed) { | ||
return SeenCondition.Mixed; | ||
} | ||
// AlwaysTrue + AlwaysFalse = Mixed | ||
return SeenCondition.Mixed; | ||
} | ||
|
||
export function checkSometimesAssertions(): Map<string, SeenCondition> { | ||
const summary = new Map<string, SeenCondition>(); | ||
|
||
for (const [key, ctxMap] of _sometimesResults) { | ||
let agg: SeenCondition | undefined; | ||
for (const state of ctxMap.values()) { | ||
agg = agg === undefined ? state : combine(agg, state); | ||
if (agg === SeenCondition.Mixed) break; | ||
} | ||
if (agg !== undefined) { | ||
summary.set(key, agg); | ||
} | ||
} | ||
|
||
return summary; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.