Skip to content
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

Allow comments in .python-version[s] #5350

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions crates/uv-python/src/version_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ async fn read_versions_file() -> Result<Option<Vec<String>>, std::io::Error> {
match fs::tokio::read_to_string(PYTHON_VERSIONS_FILENAME).await {
Ok(content) => {
debug!("Reading requests from `{PYTHON_VERSIONS_FILENAME}`");
Ok(Some(content.lines().map(ToString::to_string).collect()))
Ok(Some(
content
.lines()
.filter(|line| {
// Skip comments and empty lines.
let trimmed = line.trim();
!(trimmed.is_empty() || trimmed.starts_with('#'))
})
.map(ToString::to_string)
.collect(),
))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err),
Expand All @@ -67,7 +77,14 @@ async fn read_version_file() -> Result<Option<String>, std::io::Error> {
match fs::tokio::read_to_string(PYTHON_VERSION_FILENAME).await {
Ok(content) => {
debug!("Reading requests from `{PYTHON_VERSION_FILENAME}`");
Ok(content.lines().next().map(ToString::to_string))
Ok(content
.lines()
.find(|line| {
// Skip comments and empty lines.
let trimmed = line.trim();
!(trimmed.is_empty() || trimmed.starts_with('#'))
})
.map(ToString::to_string))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err),
Expand Down
41 changes: 39 additions & 2 deletions crates/uv/tests/python_pin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![cfg(all(feature = "python", feature = "pypi"))]

use assert_fs::fixture::{FileWriteStr as _, PathChild as _};
use anyhow::Result;
use assert_fs::fixture::{FileWriteStr, PathChild, PathCreateDir};
use common::{uv_snapshot, TestContext};
use insta::assert_snapshot;
use uv_python::{
platform::{Arch, Os},
PYTHON_VERSION_FILENAME,
PYTHON_VERSIONS_FILENAME, PYTHON_VERSION_FILENAME,
};

mod common;
Expand Down Expand Up @@ -629,3 +630,39 @@ fn python_pin_resolve() {
"###);
});
}

#[test]
fn python_pin_with_comments() -> Result<()> {
let context = TestContext::new_with_versions(&[]);

let child = context.temp_dir.child("foo");
child.create_dir_all()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we need a child directory? The context.temp_dir should be fine, right? Then you don't need to set the working directory.

Copy link
Contributor Author

@j178 j178 Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing both .python-version and .python-versions in the same test, so I want to separate them by using different subdirectory, otherwise only one of them will be utilized.

Copy link
Member

@zanieb zanieb Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just test the one that has higher precedence first. edit: second* :)

I'd recommend separate tests though over creating subdirectories for mixed state in a single test.


let version_file = child.child(PYTHON_VERSION_FILENAME);
version_file.write_str("3.12\n\n# 3.11\n3.10\n\n")?;
j178 marked this conversation as resolved.
Show resolved Hide resolved
uv_snapshot!(context.filters(), context.python_pin().current_dir(&child), @r###"
success: true
exit_code: 0
----- stdout -----
3.12

----- stderr -----
"###);

let child = context.temp_dir.child("bar");
child.create_dir_all()?;

let versions_file = child.child(PYTHON_VERSIONS_FILENAME);
versions_file.write_str("3.12\n\n# 3.11\n3.10\n\n")?;
uv_snapshot!(context.filters(), context.python_pin().current_dir(&child), @r###"
success: true
exit_code: 0
----- stdout -----
3.12
3.10

----- stderr -----
"###);

Ok(())
}
Loading