Skip to content

Commit db719e0

Browse files
committed
test(ADB): verify adb version format with debug_assertions
1 parent 2e1e364 commit db719e0

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/core/adb.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,42 @@ impl ACommand {
128128
/// ```txt
129129
/// Android Debug Bridge version <num>.<num>.<num>
130130
/// Version <num>.<num>.<num>-<no spaces>
131-
/// Installed as <ANDROID_SDK_HOME>/platform-tools/adb<optional extension>
131+
/// Installed as <ANDROID_SDK_HOME>/platform-tools/adb[.exe]
132132
/// Running on <OS/kernel version> (<CPU arch>)
133133
/// ```
134134
pub fn version(mut self) -> Result<Vec<String>, String> {
135+
#[cfg(debug_assertions)]
136+
static TRIPLE: LazyLock<Regex> = LazyLock::new(|| {
137+
Regex::new(r"^Android Debug Bridge version \d+.\d+.\d+$")
138+
.unwrap_or_else(|_| unreachable!())
139+
});
140+
#[cfg(debug_assertions)]
141+
static DISTRO: LazyLock<Regex> = LazyLock::new(|| {
142+
Regex::new(r"^Version \d+.\d+.\d+-\S+$").unwrap_or_else(|_| unreachable!())
143+
});
144+
135145
self.0.arg("version");
136146
// typically 5 allocs (after `lines`).
137147
// ideally 0, if we didn't use `lines`.
138-
Ok(self.run()?.lines().map(str::to_string).collect())
148+
Ok(self
149+
.run()?
150+
.lines()
151+
.enumerate()
152+
.map(|(i, ln)| {
153+
debug_assert!(match i {
154+
0 => TRIPLE.is_match(ln),
155+
1 => DISTRO.is_match(ln),
156+
2 =>
157+
// missing test for valid path
158+
ln.starts_with("Installed as ")
159+
&& (ln.ends_with("adb") || ln.ends_with("adb.exe")),
160+
// missing test for x86/ARM (both 64b)
161+
3 => ln.starts_with("Running on "),
162+
_ => unreachable!("Expected < 5 lines"),
163+
});
164+
ln.to_string()
165+
})
166+
.collect())
139167
}
140168

141169
/// General executor

0 commit comments

Comments
 (0)