-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Fix python
setting in mdtests, and rewrite a site-packages
test as an mdtest
#17222
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 2 commits
Commits
Show all changes
4 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
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 |
---|---|---|
|
@@ -5,7 +5,9 @@ use colored::Colorize; | |
use config::SystemKind; | ||
use parser as test_parser; | ||
use red_knot_python_semantic::types::check_types; | ||
use red_knot_python_semantic::{Program, ProgramSettings, PythonPath, SearchPathSettings}; | ||
use red_knot_python_semantic::{ | ||
Program, ProgramSettings, PythonPath, SearchPathSettings, SysPrefixPathOrigin, | ||
}; | ||
use ruff_db::diagnostic::{create_parse_diagnostic, Diagnostic, DisplayDiagnosticConfig}; | ||
use ruff_db::files::{system_path_to_file, File}; | ||
use ruff_db::panic::catch_unwind; | ||
|
@@ -158,8 +160,10 @@ fn run_test( | |
|
||
let src_path = project_root.clone(); | ||
let custom_typeshed_path = test.configuration().typeshed(); | ||
let python_path = test.configuration().python(); | ||
let mut typeshed_files = vec![]; | ||
let mut has_custom_versions_file = false; | ||
let mut has_custom_pyvenv_cfg_file = false; | ||
|
||
let test_files: Vec<_> = test | ||
.files() | ||
|
@@ -169,8 +173,8 @@ fn run_test( | |
} | ||
|
||
assert!( | ||
matches!(embedded.lang, "py" | "pyi" | "python" | "text"), | ||
"Supported file types are: py (or python), pyi, text, and ignore" | ||
matches!(embedded.lang, "py" | "pyi" | "python" | "text" | "cfg"), | ||
"Supported file types are: py (or python), pyi, text, cfg and ignore" | ||
); | ||
|
||
let full_path = embedded.full_path(&project_root); | ||
|
@@ -183,11 +187,17 @@ fn run_test( | |
typeshed_files.push(relative_path.to_path_buf()); | ||
} | ||
} | ||
} else if let Some(python_path) = python_path { | ||
if let Ok(relative_path) = full_path.strip_prefix(python_path) { | ||
if relative_path.as_str() == "pyvenv.cfg" { | ||
has_custom_pyvenv_cfg_file = true; | ||
} | ||
} | ||
} | ||
|
||
db.write_file(&full_path, &embedded.code).unwrap(); | ||
|
||
if !full_path.starts_with(&src_path) || embedded.lang == "text" { | ||
if !full_path.starts_with(&src_path) || !matches!(embedded.lang, "py" | "pyi") { | ||
// These files need to be written to the file system (above), but we don't run any checks on them. | ||
return None; | ||
} | ||
|
@@ -221,6 +231,17 @@ fn run_test( | |
} | ||
} | ||
|
||
if let Some(python_path) = python_path { | ||
if !has_custom_pyvenv_cfg_file { | ||
let pyvenv_cfg_file = python_path.join("pyvenv.cfg"); | ||
let python_version = test.configuration().python_version().unwrap_or_default(); | ||
let home_directory = SystemPathBuf::from(format!("/Python{python_version}")); | ||
db.create_directory_all(&home_directory).unwrap(); | ||
db.write_file(&pyvenv_cfg_file, format!("home = {home_directory}")) | ||
.unwrap(); | ||
} | ||
} | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let configuration = test.configuration(); | ||
|
||
let settings = ProgramSettings { | ||
|
@@ -230,13 +251,15 @@ fn run_test( | |
src_roots: vec![src_path], | ||
extra_paths: configuration.extra_paths().unwrap_or_default().to_vec(), | ||
custom_typeshed: custom_typeshed_path.map(SystemPath::to_path_buf), | ||
python_path: PythonPath::KnownSitePackages( | ||
configuration | ||
.python() | ||
.into_iter() | ||
.map(SystemPath::to_path_buf) | ||
.collect(), | ||
), | ||
python_path: configuration | ||
.python() | ||
.map(|sys_prefix| { | ||
PythonPath::SysPrefix( | ||
sys_prefix.to_path_buf(), | ||
SysPrefixPathOrigin::PythonCliFlag, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only noticed this now. The |
||
) | ||
}) | ||
.unwrap_or(PythonPath::KnownSitePackages(vec![])), | ||
}, | ||
}; | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't work on Windows, because on Windows the
site-packages
subdirectory is in a different path relative tosys.prefix
(and red-knot knows this, which is what is causing the test to fail!). For the test to pass on Windows, this would need to be(which is honestly more sane; I wish it were like this on non-Windows platforms too).
I'm wondering about adding a "magic path segment" like this, which mdtest would automatically replace with whatever the path to
site-package
is on the platform that the test is being run on, before it writes the file to the memory file system:But this also feels like a certain amount of spiralling complexity. It might be best to say that tests which need to mock out
site-packages
should stay written in Rust rather than use mdtests?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented the "magic path segment" solution (and documented it) in 3397627. It's not too much added complexity, though I'm still not totally sure it's worth it. The alternative is just to close this PR, though (and maybe revert #17221), so I figured I'd push it to this PR so it can be reviewed. And it is nice to have as many tests as possible be mdtests.