Skip to content

Commit

Permalink
Try creds
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 23, 2024
1 parent 29193ac commit c2d2d7e
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ doctest = false
workspace = true

[dependencies]
uv-cache-key = { workspace = true }
uv-distribution-types = { workspace = true }
uv-fs = { workspace = true, features = ["tokio", "schemars"] }
uv-git = { workspace = true }
Expand Down
143 changes: 104 additions & 39 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use itertools::Itertools;
use std::path::Path;
use std::str::FromStr;
use std::{fmt, mem};

use itertools::Itertools;
use thiserror::Error;
use toml_edit::{Array, ArrayOfTables, DocumentMut, Item, RawString, Table, TomlError, Value};
use url::Url;

use uv_cache_key::CanonicalUrl;
use uv_distribution_types::Index;
use uv_fs::PortablePath;
use uv_pep440::{Version, VersionSpecifier, VersionSpecifiers};
Expand Down Expand Up @@ -220,70 +224,131 @@ impl PyProjectTomlMut {
.ok_or(Error::MalformedSources)?
.entry("index")
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()))
.as_array_of_tables()
.as_array_of_tables_mut()
.ok_or(Error::MalformedSources)?;

let mut table = Table::new();
if let Some(name) = index.name.as_ref() {
table.insert("name", toml_edit::value(name.to_string()));
} else if let Some(name) = existing
// If there's already an index with the same name or URL, update it (and move it to the top).
let mut table = existing
.iter()
.find(|table| {
table
// If the index has the same name, reuse it.
if let Some(index) = index.name.as_deref() {
if table
.get("name")
.and_then(|name| name.as_str())
.is_some_and(|name| name == index)
{
return true;
}
}

// If the index is the default, and there's another default index, reuse it.
if index.default
&& table
.get("default")
.is_some_and(|default| default.as_bool() == Some(true))
{
return true;
}

// If there's another index with the same URL, reuse it.
if table
.get("url")
.is_some_and(|url| url.as_str() == Some(index.url.url().as_str()))
.and_then(|item| item.as_str())
.and_then(|url| Url::parse(url).ok())
.is_some_and(|url| {
CanonicalUrl::new(&url) == CanonicalUrl::new(index.url.url())
})
{
return true;
}

false
})
.and_then(|existing| existing.get("name"))
.cloned()
.unwrap_or_default();

// If necessary, update the name.
if let Some(index) = index.name.as_deref() {
if !table
.get("name")
.and_then(|name| name.as_str())
.is_some_and(|name| name == index)
{
table.insert("name", toml_edit::value(index.to_string()));
}
}

// If necessary, update the URL.
if !table
.get("url")
.and_then(|item| item.as_str())
.and_then(|url| Url::parse(url).ok())
.is_some_and(|url| CanonicalUrl::new(&url) == CanonicalUrl::new(index.url.url()))
{
// If there's an existing index with the same URL, and a name, preserve the name.
table.insert("name", name.clone());
table.insert("url", toml_edit::value(index.url.to_string()));
}
table.insert("url", toml_edit::value(index.url.to_string()));

// If necessary, update the default.
if index.default {
table.insert("default", toml_edit::value(true));
if !table
.get("default")
.and_then(|default| default.as_bool())
.is_some_and(|default| default == true)
{
table.insert("default", toml_edit::value(true));
}
}

// Push the item to the table.
let mut updated = ArrayOfTables::new();
updated.push(table);
for table in existing {
// If there's another index with the same name, replace it.
if table
.get("name")
.is_some_and(|name| name.as_str() == index.name.as_deref())
{
continue;
// Remove any replaced tables.
existing.retain(|table| {
// If the index has the same name, skip it.
if let Some(index) = index.name.as_deref() {
if table
.get("name")
.and_then(|name| name.as_str())
.is_some_and(|name| name == index)
{
return false;
}
}

// If there's another default index, remove it.
// If there's another default index, skip it.
if index.default
&& table
.get("default")
.is_some_and(|default| default.as_bool() == Some(true))
{
continue;
return false;
}

// If there's another index with the same URL, replace it.
// If there's another index with the same URL, skip it.
if table
.get("url")
.is_some_and(|url| url.as_str() == Some(index.url.url().as_str()))
.and_then(|item| item.as_str())
.and_then(|url| Url::parse(url).ok())
.is_some_and(|url| CanonicalUrl::new(&url) == CanonicalUrl::new(index.url.url()))
{
continue;
return false;
}

updated.push(table.clone());
true
});

// Set the position to the minimum.
if let Some(position) = existing.iter().filter_map(|table| table.position()).min() {
table.set_position(position);

// Increment the position of all elements.
for table in existing.iter_mut() {
if let Some(position) = table.position() {
table.set_position(position + 1);
}
}
}
self.doc
.entry("tool")
.or_insert(implicit())
.as_table_mut()
.ok_or(Error::MalformedSources)?
.entry("uv")
.or_insert(implicit())
.as_table_mut()
.ok_or(Error::MalformedSources)?
.insert("index", Item::ArrayOfTables(updated));

// Push the item to the table.
existing.push(table);

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ pub(crate) async fn add(

let content = toml.to_string();

debug!("Updated `pyproject.toml`:\n{}", content);

// Save the modified `pyproject.toml` or script.
let modified = match &target {
Target::Script(script, _) => {
Expand Down
121 changes: 108 additions & 13 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5507,7 +5507,7 @@ fn add_index() -> Result<()> {
);
});

// Adding a subsequent index should put it _below_ the existing index.
// Adding a subsequent index should put it _above_ the existing index.
uv_snapshot!(context.filters(), context.add().arg("jinja2").arg("--index").arg("pytorch=https://download.pytorch.org/whl/cu121").env_remove(EnvVars::UV_EXCLUDE_NEWER), @r###"
success: true
exit_code: 0
Expand Down Expand Up @@ -5541,11 +5541,11 @@ fn add_index() -> Result<()> {
name = "pytorch"
url = "https://download.pytorch.org/whl/cu121"
[[tool.uv.index]]
url = "https://pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
[[tool.uv.index]]
url = "https://pypi.org/simple"
"###
);
});
Expand Down Expand Up @@ -5638,15 +5638,15 @@ fn add_index() -> Result<()> {
"jinja2>=3.1.3",
]
[tool.uv.sources]
jinja2 = { index = "pytorch" }
[[tool.uv.index]]
name = "pytorch"
url = "https://test.pypi.org/simple"
[[tool.uv.index]]
url = "https://pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
"###
);
});
Expand Down Expand Up @@ -5748,15 +5748,15 @@ fn add_index() -> Result<()> {
"typing-extensions>=4.12.2",
]
[tool.uv.sources]
jinja2 = { index = "pytorch" }
[[tool.uv.index]]
url = "https://pypi.org/simple"
[[tool.uv.index]]
name = "pytorch"
url = "https://test.pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
"###
);
});
Expand Down Expand Up @@ -5867,15 +5867,15 @@ fn add_index() -> Result<()> {
"typing-extensions>=4.12.2",
]
[tool.uv.sources]
jinja2 = { index = "pytorch" }
[[tool.uv.index]]
name = "pytorch"
url = "https://test.pypi.org/simple"
[[tool.uv.index]]
url = "https://pypi.org/simple"
[tool.uv.sources]
jinja2 = { index = "pytorch" }
"###
);
});
Expand Down Expand Up @@ -6133,6 +6133,101 @@ fn add_default_index_url() -> Result<()> {
Ok(())
}

#[test]
fn add_index_credentials() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
# Set an internal index as the default, without credentials.
[[tool.uv.index]]
name = "internal"
url = "https://pypi-proxy.fly.dev/basic-auth/simple"
default = true
"#})?;

// Provide credentials for the index via the environment variable.
uv_snapshot!(context.filters(), context.add().arg("iniconfig==2.0.0").env("UV_DEFAULT_INDEX", "https://public:[email protected]/basic-auth/simple"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 2 packages in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ iniconfig==2.0.0
"###);

let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"iniconfig==2.0.0",
]
# Set an internal index as the default, without credentials.
[[tool.uv.index]]
name = "internal"
url = "https://pypi-proxy.fly.dev/basic-auth/simple"
default = true
"###
);
});

let lock = fs_err::read_to_string(context.temp_dir.join("uv.lock"))?;

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"
[options]
exclude-newer = "2024-03-25T00:00:00Z"
[[package]]
name = "iniconfig"
version = "2.0.0"
source = { registry = "https://pypi-proxy.fly.dev/basic-auth/simple" }
sdist = { url = "https://pypi-proxy.fly.dev/basic-auth/files/packages/d7/4b/cbd8e699e64a6f16ca3a8220661b5f83792b3017d0f79807cb8708d33913/iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3", size = 4646 }
wheels = [
{ url = "https://pypi-proxy.fly.dev/basic-auth/files/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 },
]
[[package]]
name = "project"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "iniconfig" },
]
[package.metadata]
requires-dist = [{ name = "iniconfig", specifier = "==2.0.0" }]
"###
);
});

Ok(())
}

/// Accidentally add a dependency on the project itself.
#[test]
fn add_self() -> Result<()> {
Expand Down

0 comments on commit c2d2d7e

Please sign in to comment.