Skip to content

Commit dbcfe9f

Browse files
mgornyGankra
andauthored
Fix version json tests to work outside git checkout (astral-sh#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 astral-sh#13251. Fixes astral-sh#13212 ## Test Plan 1. `cargo test` in a git clone. 2. `cargo clean`, moved `.git` away, `cargo test` again. --------- Co-authored-by: Aria Desires <[email protected]>
1 parent 38884da commit dbcfe9f

File tree

1 file changed

+85
-35
lines changed

1 file changed

+85
-35
lines changed

crates/uv/tests/it/version.rs

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,22 @@ fn version_get_fallback_unmanaged_short() -> Result<()> {
935935
Ok(())
936936
}
937937

938+
/// In tarball builds of uv, git version info is missing (distros do this)
939+
fn git_version_info_expected() -> bool {
940+
// This is setup to aggressively panic to make sure this is working at all
941+
// If you're a packager of uv and this does indeed blow up for you, we will
942+
// gladly change these expects into "just return false" or something.
943+
let manifest_dir = std::env::var(uv_static::EnvVars::CARGO_MANIFEST_DIR)
944+
.expect("CARGO_MANIFEST_DIR not defined");
945+
let git_dir = std::path::Path::new(&manifest_dir)
946+
.parent()
947+
.expect("parent of manifest dir missing")
948+
.parent()
949+
.expect("grandparent of manifest dir missing")
950+
.join(".git");
951+
git_dir.exists()
952+
}
953+
938954
// version_get_fallback with `--json`
939955
#[test]
940956
fn version_get_fallback_unmanaged_json() -> Result<()> {
@@ -970,26 +986,43 @@ fn version_get_fallback_unmanaged_json() -> Result<()> {
970986
),
971987
])
972988
.collect::<Vec<_>>();
973-
uv_snapshot!(filters, context.version()
974-
.arg("--output-format").arg("json"), @r#"
975-
success: true
976-
exit_code: 0
977-
----- stdout -----
978-
{
979-
"package_name": "uv",
980-
"version": "[VERSION]",
981-
"commit_info": {
982-
"short_commit_hash": "[LONGHASH]",
983-
"commit_hash": "[LONGHASH]",
984-
"commit_date": "[DATE]",
985-
"last_tag": "[TAG]",
986-
"commits_since_last_tag": [COUNT]
989+
if git_version_info_expected() {
990+
uv_snapshot!(filters, context.version()
991+
.arg("--output-format").arg("json"), @r#"
992+
success: true
993+
exit_code: 0
994+
----- stdout -----
995+
{
996+
"package_name": "uv",
997+
"version": "[VERSION]",
998+
"commit_info": {
999+
"short_commit_hash": "[LONGHASH]",
1000+
"commit_hash": "[LONGHASH]",
1001+
"commit_date": "[DATE]",
1002+
"last_tag": "[TAG]",
1003+
"commits_since_last_tag": [COUNT]
1004+
}
9871005
}
988-
}
9891006
990-
----- stderr -----
991-
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.
992-
"#);
1007+
----- stderr -----
1008+
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.
1009+
"#);
1010+
} else {
1011+
uv_snapshot!(filters, context.version()
1012+
.arg("--output-format").arg("json"), @r#"
1013+
success: true
1014+
exit_code: 0
1015+
----- stdout -----
1016+
{
1017+
"package_name": "uv",
1018+
"version": "[VERSION]",
1019+
"commit_info": null
1020+
}
1021+
1022+
----- stderr -----
1023+
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.
1024+
"#);
1025+
}
9931026

9941027
let pyproject = fs_err::read_to_string(&pyproject_toml)?;
9951028
assert_snapshot!(
@@ -1201,25 +1234,42 @@ fn self_version_json() -> Result<()> {
12011234
),
12021235
])
12031236
.collect::<Vec<_>>();
1204-
uv_snapshot!(filters, context.self_version()
1205-
.arg("--output-format").arg("json"), @r#"
1206-
success: true
1207-
exit_code: 0
1208-
----- stdout -----
1209-
{
1210-
"package_name": "uv",
1211-
"version": "[VERSION]",
1212-
"commit_info": {
1213-
"short_commit_hash": "[LONGHASH]",
1214-
"commit_hash": "[LONGHASH]",
1215-
"commit_date": "[DATE]",
1216-
"last_tag": "[TAG]",
1217-
"commits_since_last_tag": [COUNT]
1237+
1238+
if git_version_info_expected() {
1239+
uv_snapshot!(filters, context.self_version()
1240+
.arg("--output-format").arg("json"), @r#"
1241+
success: true
1242+
exit_code: 0
1243+
----- stdout -----
1244+
{
1245+
"package_name": "uv",
1246+
"version": "[VERSION]",
1247+
"commit_info": {
1248+
"short_commit_hash": "[LONGHASH]",
1249+
"commit_hash": "[LONGHASH]",
1250+
"commit_date": "[DATE]",
1251+
"last_tag": "[TAG]",
1252+
"commits_since_last_tag": [COUNT]
1253+
}
12181254
}
1219-
}
12201255
1221-
----- stderr -----
1222-
"#);
1256+
----- stderr -----
1257+
"#);
1258+
} else {
1259+
uv_snapshot!(filters, context.self_version()
1260+
.arg("--output-format").arg("json"), @r#"
1261+
success: true
1262+
exit_code: 0
1263+
----- stdout -----
1264+
{
1265+
"package_name": "uv",
1266+
"version": "[VERSION]",
1267+
"commit_info": null
1268+
}
1269+
1270+
----- stderr -----
1271+
"#);
1272+
}
12231273

12241274
let pyproject = fs_err::read_to_string(&pyproject_toml)?;
12251275
assert_snapshot!(

0 commit comments

Comments
 (0)