Skip to content

Commit 5d20ef3

Browse files
committed
Fix some unit tests not being run; handle trailing newlines in version parsing
1 parent 796c469 commit 5d20ef3

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

godot-bindings/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ godot4-prebuilt = { optional = true, git = "https://github.com/godot-rust/godot4
2525
bindgen = { optional = true, version = "0.65", default-features = false, features = ["runtime"] }
2626
regex = { optional = true, version = "1.5.5", default-features = false, features = ["std", "unicode-gencat"] }
2727
which = { optional = true, version = "4" }
28+
29+
[dev-dependencies]
30+
# For tests, we need regex unconditionally. Keep this in sync with above dependency.
31+
regex = { version = "1.5.5", default-features = false, features = ["std", "unicode-gencat"] }

godot-bindings/src/godot_version.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn parse_godot_version(version_str: &str) -> Result<GodotVersion, Box<dyn Er
1616
// https://github.com/godot-rust/gdext/issues/118#issuecomment-1465748123
1717
// We assume that it's on a line of its own, but it may be surrounded by other lines.
1818
let regex = Regex::new(
19-
r#"(?xm)
19+
r"(?xm)
2020
# x: ignore whitespace and allow line comments (starting with `#`)
2121
# m: multi-line mode, ^ and $ match start and end of line
2222
^
@@ -32,15 +32,17 @@ pub fn parse_godot_version(version_str: &str) -> Result<GodotVersion, Box<dyn Er
3232
(\.[^.]+)+?
3333
# Git commit SHA1, currently truncated to 9 chars, but accept the full thing
3434
(?:\.(?P<custom_rev>[a-f0-9]{9,40}))?
35+
# Optional newline printed in some systems (e.g. Arch Linux, see #416)
36+
(?:\\n)?
3537
$
36-
"#,
38+
",
3739
)?;
3840

3941
let fail = || format!("Version substring cannot be parsed: `{version_str}`");
4042
let caps = regex.captures(version_str).ok_or_else(fail)?;
4143

4244
Ok(GodotVersion {
43-
full_string: caps.get(0).unwrap().as_str().to_string(),
45+
full_string: caps.get(0).unwrap().as_str().trim().to_string(),
4446
major: cap(&caps, "major")?.unwrap(),
4547
minor: cap(&caps, "minor")?.unwrap(),
4648
patch: cap(&caps, "patch")?.unwrap_or(0),
@@ -86,6 +88,7 @@ fn test_godot_versions() {
8688
("4.0.beta8.mono.custom_build.b28ddd918", 4, 0, 0, "beta8", s("b28ddd918")),
8789
("4.0.rc1.official.8843d9ad3", 4, 0, 0, "rc1", s("8843d9ad3")),
8890
("4.0.stable.arch_linux", 4, 0, 0, "stable", None),
91+
("4.1.1.stable.arch_linux\n", 4, 1, 1, "stable", None),
8992
// Output from 4.0.stable on MacOS in debug mode:
9093
// https://github.com/godotengine/godot/issues/74906
9194
("arguments
@@ -104,7 +107,7 @@ Current path: /Users/runner/work/gdext/gdext/godot-core
104107
for (full, major, minor, patch, status, custom_rev) in good_versions {
105108
let expected = GodotVersion {
106109
// Version line is last in every test at the moment.
107-
full_string: full.lines().last().unwrap().to_owned(),
110+
full_string: full.lines().last().unwrap().trim().to_owned(),
108111
major,
109112
minor,
110113
patch,

godot-bindings/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub struct GodotVersion {
4040
// ----------------------------------------------------------------------------------------------------------------------------------------------
4141
// Regenerate all files
4242

43+
// This file is explicitly included in unit tests. Needs regex dependency.
44+
#[cfg(test)]
45+
mod godot_version;
46+
4347
#[cfg(feature = "custom-godot")]
4448
#[path = ""]
4549
mod custom {

0 commit comments

Comments
 (0)