1#[macro_export]
39macro_rules! pair {
40 (to: $stream:expr, $key:expr, $value:expr $(, $($args:tt)*)?) => {{
41 #[allow(unused_imports)]
42 use std::{fmt::Write as _, io::Write as _};
43
44 #[allow(clippy::explicit_write)]
45 writeln!($stream, concat!("cargo:", $key, "=", $value) $(, $($args)*)?).unwrap()
46 }};
47 ($key:expr, $value:expr $(, $($args:tt)*)?) => {
48 $crate::pair!(to: std::io::stdout(), $key, $value $(, $($args)*)?);
49 };
50}
51
52#[cfg(test)]
53mod tests {
54 #[test]
55 fn single_literal() {
56 insta::assert_display_snapshot!(
57 crate::capture_output(|output| {
58 crate::pair!(
59 to: output,
60 "KEY", "VALUE"
61 );
62 }),
63 @"cargo:KEY=VALUE\n"
64 );
65 }
66
67 #[test]
68 fn single_with_key_formatted_by_index() {
69 insta::assert_display_snapshot!(
70 crate::capture_output(|output| {
71 crate::pair!(
72 to: output,
73 "{}", "VALUE", "KEY"
74 );
75 }),
76 @"cargo:KEY=VALUE\n"
77 );
78 }
79
80 #[test]
81 fn single_with_key_formatted_by_name() {
82 insta::assert_display_snapshot!(
83 crate::capture_output(|output| {
84 crate::pair!(
85 to: output,
86 "{key}", "VALUE", key = "KEY"
87 );
88 }),
89 @"cargo:KEY=VALUE\n"
90 );
91 }
92
93 #[test]
94 fn single_with_value_formatted_by_index() {
95 insta::assert_display_snapshot!(
96 crate::capture_output(|output| {
97 crate::pair!(
98 to: output,
99 "KEY", "{}", "VALUE"
100 );
101 }),
102 @"cargo:KEY=VALUE\n"
103 );
104 }
105
106 #[test]
107 fn single_with_value_formatted_by_name() {
108 insta::assert_display_snapshot!(
109 crate::capture_output(|output| {
110 crate::pair!(
111 to: output,
112 "KEY", "{value}", value = "VALUE"
113 );
114 }),
115 @"cargo:KEY=VALUE\n"
116 );
117 }
118
119 #[test]
120 fn single_with_key_and_value_formatted_by_index() {
121 insta::assert_display_snapshot!(
122 crate::capture_output(|output| {
123 crate::pair!(
124 to: output,
125 "{}", "{}", "KEY", "VALUE"
126 );
127 }),
128 @"cargo:KEY=VALUE\n"
129 );
130 }
131
132 #[test]
133 fn single_with_key_and_value_formatted_by_name() {
134 insta::assert_display_snapshot!(
135 crate::capture_output(|output| {
136 crate::pair!(
137 to: output,
138 "{key}", "{value}", key = "KEY", value = "VALUE"
139 );
140 }),
141 @"cargo:KEY=VALUE\n"
142 );
143 }
144}