-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add integration test for post-release hook
- Loading branch information
Showing
14 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "post-release-hook-crate" | ||
version = "1.2.3" | ||
edition = "2021" | ||
publish = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
push = false | ||
post-release-hook = ["cargo", "run"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use std::env; | ||
|
||
fn main() { | ||
eprintln!("START_ENV_VARS"); | ||
for (key, value) in env::vars_os() { | ||
eprintln!("{}={}", key.to_string_lossy(), value.to_string_lossy()); | ||
} | ||
eprintln!("END_ENV_VARS"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use assert_fs::prelude::*; | ||
use assert_fs::TempDir; | ||
use maplit::hashset; | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::fmt::Debug; | ||
use std::path::Path; | ||
use std::process::{Command, Stdio}; | ||
use std::str::Lines; | ||
|
||
#[test] | ||
fn krate() { | ||
let root = Path::new("tests/post-release-hook/crate") | ||
.canonicalize() | ||
.unwrap(); | ||
let expected = hashset! { | ||
EnvVar::new("RELEASE_TAG", "v1.2.3"), | ||
EnvVar::new("DRY_RUN", "true"), | ||
}; | ||
let output = run_cargo_release(root.as_path()); | ||
let mut output = output.lines(); | ||
let actual = read_env_var_dump(&mut output).expect("missing env var dump"); | ||
assert!( | ||
actual.is_superset(&expected), | ||
"not all expected env vars are present and matching\n{expected:#?}" | ||
); | ||
} | ||
|
||
#[test] | ||
fn workspace() { | ||
let ws_root = Path::new("tests/post-release-hook/workspace") | ||
.canonicalize() | ||
.unwrap(); | ||
let expected = vec![ | ||
hashset! { | ||
EnvVar::new("RELEASE_TAG", "post-release-hook-ws-a-v42.42.42"), | ||
EnvVar::new("DRY_RUN", "true"), | ||
}, | ||
hashset! { | ||
EnvVar::new("RELEASE_TAG", "post-release-hook-ws-b-v420.420.420"), | ||
EnvVar::new("DRY_RUN", "true"), | ||
}, | ||
]; | ||
let output = run_cargo_release(ws_root.as_path()); | ||
let mut output = output.lines(); | ||
let mut actual = Vec::new(); | ||
while let Some(vars) = read_env_var_dump(&mut output) { | ||
actual.push(vars); | ||
} | ||
for expected in expected { | ||
assert!(actual.iter().any(|actual| actual.is_superset(&expected))); | ||
} | ||
} | ||
|
||
fn run_cargo_release(dir: &Path) -> String { | ||
let temp = TempDir::new().unwrap(); | ||
temp.copy_from(dir, &["**"]).unwrap(); | ||
|
||
git(temp.path(), &["init"]); | ||
git(temp.path(), &["add", "."]); | ||
git( | ||
temp.path(), | ||
&["commit", "--message", "this is a commit message"], | ||
); | ||
|
||
let mut cargo = env::var_os("CARGO").map_or_else(|| Command::new("cargo"), Command::new); | ||
let output = cargo | ||
.stderr(Stdio::piped()) | ||
.args(["run", "--manifest-path"]) | ||
.arg(Path::new(PROJECT_ROOT).join("Cargo.toml")) | ||
.args(["--", "release", "-vv"]) | ||
.current_dir(&temp) | ||
.spawn() | ||
.unwrap() | ||
.wait_with_output() | ||
.unwrap(); | ||
|
||
temp.close().unwrap(); | ||
|
||
if !output.status.success() { | ||
panic!("cargo release exited with {}", output.status); | ||
} | ||
let output = String::from_utf8(output.stderr).unwrap(); | ||
eprintln!("{output}"); | ||
output | ||
} | ||
|
||
fn git(dir: &Path, args: &[&str]) { | ||
assert!(Command::new("git") | ||
.args(args) | ||
.current_dir(dir) | ||
.spawn() | ||
.unwrap() | ||
.wait() | ||
.unwrap() | ||
.success()); | ||
} | ||
|
||
fn read_env_var_dump(lines: &mut Lines) -> Option<HashSet<EnvVar>> { | ||
let mut variables = HashSet::new(); | ||
loop { | ||
if lines.next()?.trim() == "START_ENV_VARS" { | ||
break; | ||
} | ||
} | ||
loop { | ||
let line = lines.next().expect("missing end of env var dump").trim(); | ||
if line == "END_ENV_VARS" { | ||
return Some(variables); | ||
} | ||
|
||
let (key, value) = line.split_once('=').unwrap(); | ||
variables.insert(EnvVar::new(key, value)); | ||
} | ||
} | ||
|
||
#[derive(Debug, Eq, PartialEq, Hash)] | ||
struct EnvVar { | ||
key: String, | ||
value: String, | ||
} | ||
|
||
impl EnvVar { | ||
fn new(key: &str, value: &str) -> Self { | ||
Self { | ||
key: key.to_owned(), | ||
value: value.to_owned(), | ||
} | ||
} | ||
} | ||
|
||
const PROJECT_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../crate/.gitignore |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[workspace] | ||
members = ["a", "b"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "post-release-hook-ws-a" | ||
version = "42.42.42" | ||
edition = "2021" | ||
publish = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../crate/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
name = "post-release-hook-ws-b" | ||
version = "420.420.420" | ||
edition = "2021" | ||
publish = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../crate/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../crate/release.toml |