Skip to content

Commit

Permalink
Test our version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Jan 3, 2025
1 parent 42ba3b1 commit c49bc24
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TurnOnApplication {
fn show_about_dialog(&self) {
let dialog = adw::AboutDialog::from_appdata(
"/de/swsnr/turnon/de.swsnr.turnon.metainfo.xml",
Some(&crate::config::release_notes_version()),
Some(&crate::config::release_notes_version().to_string()),
);
dialog.set_version(crate::config::CARGO_PKG_VERSION);

Expand Down
62 changes: 51 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{borrow::Cow, path::PathBuf};
use std::path::PathBuf;

use glib::{gstr, GStr};
use gtk::gio;
Expand All @@ -17,6 +17,11 @@ pub static APP_ID: &GStr = gstr!("de.swsnr.turnon");
/// This provides the full version from `Cargo.toml`.
pub static CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Get [`CARGO_PKG_VERSION`] parsed.
fn cargo_pkg_version() -> semver::Version {
semver::Version::parse(CARGO_PKG_VERSION).unwrap()
}

/// The version to use for release notes.
///
/// For nightly builds (see [`is_development`]) this returns `next`, otherwise
Expand All @@ -25,16 +30,12 @@ pub static CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
///
/// This follows our versioning policy which uses patch releases only for
/// translation updates.
pub fn release_notes_version() -> Cow<'static, str> {
if is_development() {
Cow::Borrowed("next")
} else {
let mut version = semver::Version::parse(CARGO_PKG_VERSION).unwrap();
version.patch = 0;
version.pre = semver::Prerelease::EMPTY;
version.build = semver::BuildMetadata::EMPTY;
version.to_string().into()
}
pub fn release_notes_version() -> semver::Version {
let mut version = cargo_pkg_version();
version.patch = 0;
version.pre = semver::Prerelease::EMPTY;
version.build = semver::BuildMetadata::EMPTY;
version
}

pub const G_LOG_DOMAIN: &str = "TurnOn";
Expand Down Expand Up @@ -81,3 +82,42 @@ pub fn locale_directory() -> PathBuf {
"/usr/share/locale".into()
}
}

#[cfg(test)]
mod tests {

#[test]
fn release_notes_version_only_has_major_and_minor() {
let version = super::release_notes_version();
assert_eq!(version.major, super::cargo_pkg_version().major);
assert_eq!(version.minor, super::cargo_pkg_version().minor);
assert_eq!(version.patch, 0);
assert!(version.pre.is_empty());
assert!(version.build.is_empty());
}
#[test]
fn release_notes_for_release_notes_version() {
let metadata = std::fs::read_to_string(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/de.swsnr.turnon.metainfo.xml.in"
))
.unwrap();
assert!(metadata.contains(&format!(
"<release version=\"{}\"",
super::release_notes_version()
)));
}

#[test]
fn no_release_notes_for_cargo_pkg_version() {
let version = super::cargo_pkg_version();
if version != super::release_notes_version() {
let metadata = std::fs::read_to_string(concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/de.swsnr.turnon.metainfo.xml.in"
))
.unwrap();
assert!(!metadata.contains(&format!("version=\"{version}\"")));
}
}
}

0 comments on commit c49bc24

Please sign in to comment.