Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/mergify-ci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ url = "2"
uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
mergify-test-support = { path = "../mergify-test-support" }
tempfile = "3.14"
temp-env = { version = "0.3", features = ["async_closure"] }
tokio = { version = "1", default-features = false, features = ["macros", "rt", "time"] }
Expand Down
58 changes: 14 additions & 44 deletions crates/mergify-ci/src/git_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,30 +437,11 @@ fn buildkite_meta_data_set(key: &str, value: &str) -> std::io::Result<()> {

#[cfg(test)]
mod tests {
use mergify_core::OutputMode;
use mergify_core::StdioOutput;
use mergify_test_support::Captured;
use tempfile::TempDir;

use super::*;

type SharedBytes = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;

struct Captured {
output: StdioOutput,
stdout: SharedBytes,
}

fn make_output() -> Captured {
let stdout: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let stderr: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let output = StdioOutput::with_sinks(
OutputMode::Human,
SharedWriter(std::sync::Arc::clone(&stdout)),
SharedWriter(std::sync::Arc::clone(&stderr)),
);
Captured { output, stdout }
}

fn no_notes(_branch: &str, _sha: &str) -> Option<MergeQueueMetadata> {
None
}
Expand All @@ -473,7 +454,7 @@ mod tests {

#[test]
fn falls_back_to_head_pair_when_no_event() {
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars_unset(
["GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH", "BUILDKITE"],
|| detect(&mut cap.output, &no_notes).unwrap(),
Expand All @@ -495,7 +476,7 @@ mod tests {
},
}),
);
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand All @@ -516,7 +497,7 @@ mod tests {
&dir,
&serde_json::json!({"before": "old-sha", "after": "new-sha"}),
);
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("push")),
Expand All @@ -543,7 +524,7 @@ mod tests {
},
}),
);
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand Down Expand Up @@ -581,7 +562,7 @@ mod tests {
None
}
};
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand All @@ -600,7 +581,7 @@ mod tests {
&dir,
&serde_json::json!({"pull_request": {"head": {"sha": "h"}}}),
);
let mut cap = make_output();
let mut cap = Captured::human();
let err = temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand All @@ -614,7 +595,7 @@ mod tests {

#[test]
fn detects_buildkite_pull_request() {
let mut cap = make_output();
let mut cap = Captured::human();
let refs = temp_env::with_vars(
[
("BUILDKITE", Some("true")),
Expand Down Expand Up @@ -647,9 +628,9 @@ mod tests {
head: "h".into(),
source: ReferencesSource::GithubEventPush,
};
let mut cap = make_output();
let mut cap = Captured::human();
emit(&refs, Format::Text, &mut cap.output).unwrap();
let stdout = String::from_utf8(cap.stdout.lock().unwrap().clone()).unwrap();
let stdout = cap.stdout();
assert_eq!(stdout, "Base: b\nHead: h\n");
}

Expand All @@ -660,9 +641,9 @@ mod tests {
head: "has space".into(),
source: ReferencesSource::MergeQueue,
};
let mut cap = make_output();
let mut cap = Captured::human();
emit(&refs, Format::Shell, &mut cap.output).unwrap();
let stdout = String::from_utf8(cap.stdout.lock().unwrap().clone()).unwrap();
let stdout = cap.stdout();
assert!(stdout.contains("MERGIFY_GIT_REFS_BASE=main"));
assert!(stdout.contains("MERGIFY_GIT_REFS_HEAD='has space'"));
assert!(stdout.contains("MERGIFY_GIT_REFS_SOURCE=merge_queue"));
Expand All @@ -675,9 +656,9 @@ mod tests {
head: "HEAD".into(),
source: ReferencesSource::GithubEventOther,
};
let mut cap = make_output();
let mut cap = Captured::human();
emit(&refs, Format::Json, &mut cap.output).unwrap();
let stdout = String::from_utf8(cap.stdout.lock().unwrap().clone()).unwrap();
let stdout = cap.stdout();
assert_eq!(
stdout.trim_end(),
r#"{"base":null,"head":"HEAD","source":"github_event_other"}"#
Expand All @@ -691,15 +672,4 @@ mod tests {
assert!(matches!(Format::parse("json"), Ok(Format::Json)));
assert!(Format::parse("yaml").is_err());
}

struct SharedWriter(SharedBytes);
impl std::io::Write for SharedWriter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
}
40 changes: 5 additions & 35 deletions crates/mergify-ci/src/queue_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,11 @@ fn write_github_output(metadata: &MergeQueueMetadata) -> Result<(), CliError> {
#[cfg(test)]
mod tests {
use mergify_core::ExitCode;
use mergify_core::OutputMode;
use mergify_core::StdioOutput;
use mergify_test_support::Captured;
use tempfile::TempDir;

use super::*;

type SharedBytes = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;

struct Captured {
output: StdioOutput,
stdout: SharedBytes,
}

fn make_output() -> Captured {
let stdout: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let stderr: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let output = StdioOutput::with_sinks(
OutputMode::Human,
SharedWriter(std::sync::Arc::clone(&stdout)),
SharedWriter(std::sync::Arc::clone(&stderr)),
);
Captured { output, stdout }
}

fn write_event_file(dir: &TempDir, body: &str, title: &str) -> PathBuf {
let path = dir.path().join("event.json");
let payload = serde_json::json!({
Expand All @@ -102,7 +83,7 @@ mod tests {

#[test]
fn errors_when_not_in_mq_context() {
let mut cap = make_output();
let mut cap = Captured::human();
let err = temp_env::with_vars_unset(["GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH"], || {
run(&mut cap.output).unwrap_err()
});
Expand All @@ -119,7 +100,7 @@ mod tests {
"merge queue: batch",
);

let mut cap = make_output();
let mut cap = Captured::human();
temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand All @@ -129,7 +110,7 @@ mod tests {
|| run(&mut cap.output).unwrap(),
);

let stdout = String::from_utf8(cap.stdout.lock().unwrap().clone()).unwrap();
let stdout = cap.stdout();
assert!(stdout.contains("\"checking_base_sha\": \"abc123\""));
assert!(stdout.contains("\"number\": 10"));
}
Expand All @@ -144,7 +125,7 @@ mod tests {
);
let gha_output = dir.path().join("gha_output");

let mut cap = make_output();
let mut cap = Captured::human();
temp_env::with_vars(
[
("GITHUB_EVENT_NAME", Some("pull_request")),
Expand All @@ -158,15 +139,4 @@ mod tests {
assert!(written.starts_with("queue_metadata<<ghadelimiter_"));
assert!(written.contains("\"checking_base_sha\":\"deadbeef\""));
}

struct SharedWriter(SharedBytes);
impl std::io::Write for SharedWriter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
}
42 changes: 6 additions & 36 deletions crates/mergify-ci/src/queue_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,10 @@ pub fn detect(output: &mut dyn Output) -> std::io::Result<Option<MergeQueueMetad

#[cfg(test)]
mod tests {
use mergify_core::OutputMode;
use mergify_core::StdioOutput;
use mergify_test_support::Captured;

use super::*;

type SharedBytes = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;

struct Captured {
output: StdioOutput,
stderr: SharedBytes,
}

fn make_output() -> Captured {
let stdout: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let stderr: SharedBytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let output = StdioOutput::with_sinks(
OutputMode::Human,
SharedWriter(std::sync::Arc::clone(&stdout)),
SharedWriter(std::sync::Arc::clone(&stderr)),
);
Captured { output, stderr }
}

#[test]
fn parse_yaml_block_extracts_metadata() {
let body = "prelude\n\n```yaml\nchecking_base_sha: abc\npull_requests:\n - number: 1\n```\ntrailing";
Expand All @@ -152,10 +133,10 @@ mod tests {
}),
..Default::default()
};
let mut cap = make_output();
let mut cap = Captured::human();
let result = extract_from_event(&ev, &mut cap.output).unwrap();
assert!(result.is_none());
assert!(cap.stderr.lock().unwrap().is_empty());
assert!(cap.stderr().is_empty());
}

#[test]
Expand All @@ -168,10 +149,10 @@ mod tests {
}),
..Default::default()
};
let mut cap = make_output();
let mut cap = Captured::human();
let result = extract_from_event(&ev, &mut cap.output).unwrap();
assert!(result.is_none());
let stderr = String::from_utf8(cap.stderr.lock().unwrap().clone()).unwrap();
let stderr = cap.stderr();
assert!(stderr.contains("without body"), "got: {stderr:?}");
}

Expand All @@ -186,19 +167,8 @@ mod tests {
}),
..Default::default()
};
let mut cap = make_output();
let mut cap = Captured::human();
let meta = extract_from_event(&ev, &mut cap.output).unwrap().unwrap();
assert_eq!(meta.checking_base_sha, "deadbeef");
}

struct SharedWriter(SharedBytes);
impl std::io::Write for SharedWriter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
}
Loading
Loading