Skip to content

Commit 0b8204e

Browse files
authored
Treat empty PANTS_VERSION and PANTS_SHA as if they were unset (#190)
Overriding the pants version via the environment variables is convenient, and this PR makes it slightly more convenient by having `PANTS_VERSION= pants ...` treated the same as not setting it. This means a script can do something like `for PANTS_VERSION in "" "2.15.0" ...;` to test against multiple versions, including whatever is natively configured in `pants.toml`, without having to conditionalise `PANTS_VERSION`'s existence entirely. For a concrete example, pantsbuild/pants#19161 wants to be able to run a foreign repo's "native" pants version, but it's not trivial to do, due to this issue (the `""` part of the pants version matrix is commented out; obviously I could also have it set the env var conditionally, but that's less convenient). Before this change, running `PANTS_VERSION= pants version` next to a `pants.toml` would prompt for initialising pants: ``` PANTS_VERSION= pants version No Pants configuration was found at or above /Users/huon/projects/stellargraph/exoflare. Would you like to configure /Users/huon/projects/stellargraph/exoflare as a Pants project? (Y/n): ``` After this change, it's the same as not setting `PANTS_VERSION`: use `pants.toml`'s setting if one exists, or prompt for initialisation, if there's no `pants.toml`.
1 parent 686b544 commit 0b8204e

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes
22

3+
## 0.8.1
4+
5+
This release adjusts the `PANTS_SHA` and `PANTS_VERSION` environment variables to be ignored, if
6+
they're set to an empty string. For instance, `PANTS_SHA= PANTS_VERSION= pants ...` will now behave
7+
the same as `pants ...`.
8+
39
## 0.8.0
410

511
This release brings no new features or bug fixes, but it does convert the building of the

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66
[package]
77
name = "scie-pants"
88
description = "Protects your Pants from the elements."
9-
version = "0.8.0"
9+
version = "0.8.1"
1010
edition = "2021"
1111
authors = [
1212
"John Sirois <[email protected]>",

package/src/test.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub(crate) fn run_integration_tests(
100100
test_python_repos_repos(scie_pants_scie);
101101
test_initialize_new_pants_project(scie_pants_scie);
102102
test_set_pants_version(scie_pants_scie);
103+
test_ignore_empty_pants_version_pants_sha(scie_pants_scie);
103104

104105
let clone_root = create_tempdir()?;
105106
test_use_in_repo_with_pants_script(scie_pants_scie, &clone_root);
@@ -357,6 +358,35 @@ fn test_set_pants_version(scie_pants_scie: &Path) {
357358
.unwrap();
358359
}
359360

361+
fn test_ignore_empty_pants_version_pants_sha(scie_pants_scie: &Path) {
362+
integration_test!("Verifying ignoring PANTS_SHA and PANTS_VERSION when set to empty string");
363+
364+
let tmpdir = create_tempdir().unwrap();
365+
366+
let pants_release = "2.15.0";
367+
let pants_toml_content = format!(
368+
r#"
369+
[GLOBAL]
370+
pants_version = "{pants_release}"
371+
"#
372+
);
373+
let pants_toml = tmpdir.path().join("pants.toml");
374+
write_file(&pants_toml, false, pants_toml_content).unwrap();
375+
376+
let output = execute(
377+
Command::new(scie_pants_scie)
378+
.arg("-V")
379+
.env("PANTS_SHA", "")
380+
.env("PANTS_VERSION", "")
381+
.current_dir(&tmpdir)
382+
.stdout(Stdio::piped()),
383+
);
384+
assert_eq!(
385+
pants_release,
386+
decode_output(output.unwrap().stdout).unwrap().trim()
387+
);
388+
}
389+
360390
fn test_use_in_repo_with_pants_script(scie_pants_scie: &Path, clone_root: &TempDir) {
361391
integration_test!("Verify scie-pants can be used as `pants` in a repo with the `pants` script");
362392
// This verifies a fix for https://github.com/pantsbuild/scie-pants/issues/28.

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ impl Process {
7474
}
7575

7676
fn env_version(env_var_name: &str) -> Result<Option<String>> {
77-
if let Some(raw_version) = env::var_os(env_var_name) {
77+
let raw_version = env::var_os(env_var_name).unwrap_or(OsString::new());
78+
if raw_version.len() == 0 {
79+
// setting PANTS_VERSION= or PANTS_SHA= behaves the same as not setting them
80+
Ok(None)
81+
} else {
7882
Ok(Some(raw_version.into_string().map_err(|raw| {
7983
anyhow!("Failed to interpret {env_var_name} {raw:?} as UTF-8 string.")
8084
})?))
81-
} else {
82-
Ok(None)
8385
}
8486
}
8587

0 commit comments

Comments
 (0)