Skip to content

Commit

Permalink
test: Add integration test for post-release hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Person-93 committed Mar 30, 2022
1 parent 9c64d22 commit fc4f117
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/post-release-hook/crate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
7 changes: 7 additions & 0 deletions tests/post-release-hook/crate/Cargo.lock

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

5 changes: 5 additions & 0 deletions tests/post-release-hook/crate/Cargo.toml
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
2 changes: 2 additions & 0 deletions tests/post-release-hook/crate/release.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
push = false
post-release-hook = ["cargo", "run"]
9 changes: 9 additions & 0 deletions tests/post-release-hook/crate/src/main.rs
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");
}
132 changes: 132 additions & 0 deletions tests/post-release-hook/main.rs
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"));
1 change: 1 addition & 0 deletions tests/post-release-hook/workspace/.gitignore
11 changes: 11 additions & 0 deletions tests/post-release-hook/workspace/Cargo.lock

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

2 changes: 2 additions & 0 deletions tests/post-release-hook/workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["a", "b"]
5 changes: 5 additions & 0 deletions tests/post-release-hook/workspace/a/Cargo.toml
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
1 change: 1 addition & 0 deletions tests/post-release-hook/workspace/a/src
5 changes: 5 additions & 0 deletions tests/post-release-hook/workspace/b/Cargo.toml
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
1 change: 1 addition & 0 deletions tests/post-release-hook/workspace/b/src
1 change: 1 addition & 0 deletions tests/post-release-hook/workspace/release.toml

0 comments on commit fc4f117

Please sign in to comment.