From b5c3f93fad742a8cc9d3e99ff502055dd5bf97d2 Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Wed, 24 Jul 2024 00:33:27 +0800 Subject: [PATCH 1/3] Skip comments and empty lines in python versions file --- crates/uv-python/src/version_files.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/uv-python/src/version_files.rs b/crates/uv-python/src/version_files.rs index 4067d1b4dd23..22cad953cd83 100644 --- a/crates/uv-python/src/version_files.rs +++ b/crates/uv-python/src/version_files.rs @@ -56,7 +56,17 @@ async fn read_versions_file() -> Result>, 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), @@ -67,7 +77,14 @@ async fn read_version_file() -> Result, 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), From c270156bc918dc48f80bb808062e7b2fddfbf1c9 Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Wed, 24 Jul 2024 01:24:06 +0800 Subject: [PATCH 2/3] Add test --- crates/uv/tests/python_pin.rs | 41 +++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/uv/tests/python_pin.rs b/crates/uv/tests/python_pin.rs index 53ca8fce7c80..f42d49342a46 100644 --- a/crates/uv/tests/python_pin.rs +++ b/crates/uv/tests/python_pin.rs @@ -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; @@ -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()?; + + let version_file = child.child(PYTHON_VERSION_FILENAME); + version_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 + + ----- 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(()) +} From 302db93cc52c47a62b94ce6aa5a96c7ba45edb4f Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Wed, 24 Jul 2024 02:04:04 +0800 Subject: [PATCH 3/3] Simplify test --- crates/uv/tests/python_pin.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/uv/tests/python_pin.rs b/crates/uv/tests/python_pin.rs index f42d49342a46..416b8c388aa0 100644 --- a/crates/uv/tests/python_pin.rs +++ b/crates/uv/tests/python_pin.rs @@ -1,7 +1,7 @@ #![cfg(all(feature = "python", feature = "pypi"))] use anyhow::Result; -use assert_fs::fixture::{FileWriteStr, PathChild, PathCreateDir}; +use assert_fs::fixture::{FileWriteStr, PathChild}; use common::{uv_snapshot, TestContext}; use insta::assert_snapshot; use uv_python::{ @@ -635,12 +635,16 @@ fn python_pin_resolve() { fn python_pin_with_comments() -> Result<()> { let context = TestContext::new_with_versions(&[]); - let child = context.temp_dir.child("foo"); - child.create_dir_all()?; + let content = indoc::indoc! {r" + 3.12 + + # 3.11 + 3.10 + "}; - let version_file = child.child(PYTHON_VERSION_FILENAME); - version_file.write_str("3.12\n\n# 3.11\n3.10\n\n")?; - uv_snapshot!(context.filters(), context.python_pin().current_dir(&child), @r###" + let version_file = context.temp_dir.child(PYTHON_VERSION_FILENAME); + version_file.write_str(content)?; + uv_snapshot!(context.filters(), context.python_pin(), @r###" success: true exit_code: 0 ----- stdout ----- @@ -648,13 +652,11 @@ fn python_pin_with_comments() -> Result<()> { ----- stderr ----- "###); + fs_err::remove_file(version_file)?; - 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###" + let versions_file = context.temp_dir.child(PYTHON_VERSIONS_FILENAME); + versions_file.write_str(content)?; + uv_snapshot!(context.filters(), context.python_pin(), @r###" success: true exit_code: 0 ----- stdout -----