From dbcfe9f80b7facd17bbcbcc71bf9bf8da70e4123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 21 May 2025 21:59:39 +0200 Subject: [PATCH] Fix version json tests to work outside git checkout (#13566) ## Summary Fix the two version json tests to account for the possibility that uv was built outside a git checkout (e.g. from an unpacked git archive) and therefore does not have the commit info available. This approach uses separate snapshots for the two cases, as suggested in discussion of pull request #13251. Fixes #13212 ## Test Plan 1. `cargo test` in a git clone. 2. `cargo clean`, moved `.git` away, `cargo test` again. --------- Co-authored-by: Aria Desires --- crates/uv/tests/it/version.rs | 122 ++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 36 deletions(-) diff --git a/crates/uv/tests/it/version.rs b/crates/uv/tests/it/version.rs index e8eb05279..6ecc38aa3 100644 --- a/crates/uv/tests/it/version.rs +++ b/crates/uv/tests/it/version.rs @@ -935,6 +935,22 @@ fn version_get_fallback_unmanaged_short() -> Result<()> { Ok(()) } +/// In tarball builds of uv, git version info is missing (distros do this) +fn git_version_info_expected() -> bool { + // This is setup to aggressively panic to make sure this is working at all + // If you're a packager of uv and this does indeed blow up for you, we will + // gladly change these expects into "just return false" or something. + let manifest_dir = std::env::var(uv_static::EnvVars::CARGO_MANIFEST_DIR) + .expect("CARGO_MANIFEST_DIR not defined"); + let git_dir = std::path::Path::new(&manifest_dir) + .parent() + .expect("parent of manifest dir missing") + .parent() + .expect("grandparent of manifest dir missing") + .join(".git"); + git_dir.exists() +} + // version_get_fallback with `--json` #[test] fn version_get_fallback_unmanaged_json() -> Result<()> { @@ -970,26 +986,43 @@ fn version_get_fallback_unmanaged_json() -> Result<()> { ), ]) .collect::>(); - uv_snapshot!(filters, context.version() - .arg("--output-format").arg("json"), @r#" - success: true - exit_code: 0 - ----- stdout ----- - { - "package_name": "uv", - "version": "[VERSION]", - "commit_info": { - "short_commit_hash": "[LONGHASH]", - "commit_hash": "[LONGHASH]", - "commit_date": "[DATE]", - "last_tag": "[TAG]", - "commits_since_last_tag": [COUNT] + if git_version_info_expected() { + uv_snapshot!(filters, context.version() + .arg("--output-format").arg("json"), @r#" + success: true + exit_code: 0 + ----- stdout ----- + { + "package_name": "uv", + "version": "[VERSION]", + "commit_info": { + "short_commit_hash": "[LONGHASH]", + "commit_hash": "[LONGHASH]", + "commit_date": "[DATE]", + "last_tag": "[TAG]", + "commits_since_last_tag": [COUNT] + } } - } - ----- stderr ----- - warning: Failed to read project metadata (The project is marked as unmanaged: `[TEMP_DIR]/`). Running `uv self version` for compatibility. This fallback will be removed in the future; pass `--preview` to force an error. - "#); + ----- stderr ----- + warning: Failed to read project metadata (The project is marked as unmanaged: `[TEMP_DIR]/`). Running `uv self version` for compatibility. This fallback will be removed in the future; pass `--preview` to force an error. + "#); + } else { + uv_snapshot!(filters, context.version() + .arg("--output-format").arg("json"), @r#" + success: true + exit_code: 0 + ----- stdout ----- + { + "package_name": "uv", + "version": "[VERSION]", + "commit_info": null + } + + ----- stderr ----- + warning: Failed to read project metadata (The project is marked as unmanaged: `[TEMP_DIR]/`). Running `uv self version` for compatibility. This fallback will be removed in the future; pass `--preview` to force an error. + "#); + } let pyproject = fs_err::read_to_string(&pyproject_toml)?; assert_snapshot!( @@ -1201,25 +1234,42 @@ fn self_version_json() -> Result<()> { ), ]) .collect::>(); - uv_snapshot!(filters, context.self_version() - .arg("--output-format").arg("json"), @r#" - success: true - exit_code: 0 - ----- stdout ----- - { - "package_name": "uv", - "version": "[VERSION]", - "commit_info": { - "short_commit_hash": "[LONGHASH]", - "commit_hash": "[LONGHASH]", - "commit_date": "[DATE]", - "last_tag": "[TAG]", - "commits_since_last_tag": [COUNT] - } - } - ----- stderr ----- - "#); + if git_version_info_expected() { + uv_snapshot!(filters, context.self_version() + .arg("--output-format").arg("json"), @r#" + success: true + exit_code: 0 + ----- stdout ----- + { + "package_name": "uv", + "version": "[VERSION]", + "commit_info": { + "short_commit_hash": "[LONGHASH]", + "commit_hash": "[LONGHASH]", + "commit_date": "[DATE]", + "last_tag": "[TAG]", + "commits_since_last_tag": [COUNT] + } + } + + ----- stderr ----- + "#); + } else { + uv_snapshot!(filters, context.self_version() + .arg("--output-format").arg("json"), @r#" + success: true + exit_code: 0 + ----- stdout ----- + { + "package_name": "uv", + "version": "[VERSION]", + "commit_info": null + } + + ----- stderr ----- + "#); + } let pyproject = fs_err::read_to_string(&pyproject_toml)?; assert_snapshot!(