Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministic unit tests #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# freedesktop-icons
![crates.io-badge](https://img.shields.io/crates/v/freedesktop-icons)
![docrs-badge](https://img.shields.io/docsrs/freedesktop-icons)
# cosmic-freedesktop-icons
![crates.io-badge](https://img.shields.io/crates/v/cosmic-freedesktop-icons)
![docrs-badge](https://img.shields.io/docsrs/cosmic-freedesktop-icons)


This crate provides a [freedesktop icon](https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#implementation_notes) lookup implementation.
Expand All @@ -15,7 +15,7 @@
with the default scale (`1`) and the default size (`24`).

```rust
use freedesktop_icons::lookup;
use cosmic_freedesktop_icons::lookup;

let icon = lookup("firefox").find();
```
Expand All @@ -25,7 +25,7 @@
If you have specific requirements for your lookup you can use the provided builder functions:

```rust
use freedesktop_icons::lookup;
use cosmic_freedesktop_icons::lookup;

let icon = lookup("firefox")
.with_size(48)
Expand All @@ -39,12 +39,12 @@
you can use the internal cache to improve performance.

```rust
use freedesktop_icons::lookup;
use cosmic_freedesktop_icons::lookup;

let icon = lookup("firefox")
.with_size(48)
.with_scale(2)
.with_theme("Arc")
.with_cache()
.find();
```
```
119 changes: 72 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! The following snippet get an icon from the default 'hicolor' theme
//! with the default scale (`1`) and the default size (`24`).
//!
//! ```rust
//! ```rust, no_run
//! # fn main() {
//! use freedesktop_icons::lookup;
//! use cosmic_freedesktop_icons::lookup;
//!
//! let icon = lookup("firefox").find();
//! # }
Expand All @@ -23,9 +23,9 @@
//!
//! If you have specific requirements for your lookup you can use the provided builder functions:
//!
//! ```rust
//! ```rust, no_run
//! # fn main() {
//! use freedesktop_icons::lookup;
//! use cosmic_freedesktop_icons::lookup;
//!
//! let icon = lookup("firefox")
//! .with_size(48)
Expand All @@ -39,9 +39,9 @@
//! If your application is going to repeat the same icon lookups multiple times
//! you can use the internal cache to improve performance.
//!
//! ```rust
//! ```rust, no_run
//! # fn main() {
//! use freedesktop_icons::lookup;
//! use cosmic_freedesktop_icons::lookup;
//!
//! let icon = lookup("firefox")
//! .with_size(48)
Expand All @@ -63,9 +63,9 @@ mod theme;
/// Return the list of installed themes on the system
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::list_themes;
/// use cosmic_freedesktop_icons::list_themes;
///
/// let themes: Vec<&str> = list_themes();
///
Expand Down Expand Up @@ -102,9 +102,9 @@ pub struct LookupBuilder<'a> {
/// Build an icon lookup for the given icon name.
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox").find();
/// # }
Expand All @@ -116,9 +116,9 @@ impl<'a> LookupBuilder<'a> {
/// Restrict the lookup to the given icon size.
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox")
/// .with_size(48)
Expand All @@ -132,9 +132,9 @@ impl<'a> LookupBuilder<'a> {
/// Restrict the lookup to the given scale.
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox")
/// .with_scale(2)
Expand All @@ -147,9 +147,9 @@ impl<'a> LookupBuilder<'a> {

/// Add the given theme to the current lookup :
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox")
/// .with_theme("Papirus")
Expand All @@ -166,9 +166,9 @@ impl<'a> LookupBuilder<'a> {
/// that repeat the same lookups, an application launcher for instance.
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox")
/// .with_scale(2)
Expand All @@ -185,9 +185,9 @@ impl<'a> LookupBuilder<'a> {
/// if you need a modifiable icon, to match a user theme for instance.
///
/// ## Example
/// ```rust
/// ```rust, no_run
/// # fn main() {
/// use freedesktop_icons::lookup;
/// use cosmic_freedesktop_icons::lookup;
///
/// let icon = lookup("firefox")
/// .force_svg()
Expand Down Expand Up @@ -308,58 +308,81 @@ impl<'a> LookupBuilder<'a> {
}
}

// WARNING: these test are highly dependent on your installed icon-themes.
// If you want to run them, make sure you have 'Papirus' and 'Arc' icon-themes installed.
#[cfg(test)]
mod test {
use crate::{lookup, CacheEntry, CACHE};
use speculoos::prelude::*;
use std::path::PathBuf;
use std::{
env,
path::{Path, PathBuf},
sync::LazyLock,
};

pub(super) static TEST_ASSETS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let data_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("test_assets");
assert!(
data_dir.exists(),
"The `test_assets` folder should be in the package root"
);
data_dir
});

/// Override the default search path(s) with a path we control.
///
/// This grants us more control over tests rather than relying on the system having the
/// themes we need.
pub(super) fn set_fake_icons_path() {
env::set_var("XDG_DATA_DIRS", TEST_ASSETS_PATH.to_str().unwrap());
}

#[test]
fn simple_lookup() {
let firefox = lookup("firefox").find();
set_fake_icons_path();
let browser = lookup("browser").find();

let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/scalable/apps/browser.svg");
asserting!("Lookup with no parameters should return an existing icon")
.that(&firefox)
.that(&browser)
.is_some()
.is_equal_to(PathBuf::from(
"/usr/share/icons/hicolor/22x22/apps/firefox.png",
));
.is_equal_to(icon_path);
}

#[test]
fn theme_lookup() {
let firefox = lookup("firefox").with_theme("Papirus").find();
set_fake_icons_path();
let cosmic_fake = lookup("cosmic-fake").with_theme("cosmic-base").find();

let icon_path = TEST_ASSETS_PATH.join("icons/cosmic-base/16x16/apps/cosmic-fake.svg");
asserting!("Lookup with no parameters should return an existing icon")
.that(&firefox)
.that(&cosmic_fake)
.is_some()
.is_equal_to(PathBuf::from(
"/usr/share/icons/Papirus/24x24/apps/firefox.svg",
));
.is_equal_to(icon_path);
}

#[test]
fn should_fallback_to_parent_theme() {
set_fake_icons_path();
let icon = lookup("video-single-display-symbolic")
.with_theme("Arc")
.with_theme("cosmic-base-dark")
.find();

asserting!("Lookup for an icon in the Arc theme should find the icon in its parent")
.that(&icon)
.is_some()
.is_equal_to(PathBuf::from(
"/usr/share/icons/Adwaita/scalable/devices/video-single-display-symbolic.svg",
));
let icon_path = TEST_ASSETS_PATH
.join("icons/cosmic-base/scalable/devices/video-single-display-symbolic.svg");
asserting!(
"Lookup for an icon in the cosmic-dark theme should find the icon in its parent"
)
.that(&icon)
.is_some()
.is_equal_to(icon_path);
}

#[test]
fn should_fallback_to_pixmaps_utlimately() {
fn should_fallback_to_pixmaps_ultimately() {
set_fake_icons_path();
let archlinux_logo = lookup("archlinux-logo")
.with_size(16)
.with_scale(1)
.with_theme("Papirus")
.with_theme("COSMIC")
.find();

asserting!("When lookup fail in theme, icon should be found in '/usr/share/pixmaps'")
Expand All @@ -370,22 +393,24 @@ mod test {

#[test]
fn compare_to_linincon_with_theme() {
let lin_wireshark = linicon::lookup_icon("wireshark")
set_fake_icons_path();
let lin_cosmic_fake = linicon::lookup_icon("cosmic-fake")
.from_theme("cosmic-base")
.next()
.unwrap()
.unwrap()
.path;

let wireshark = lookup("wireshark")
let cosmic_fake = lookup("cosmic-fake")
.with_size(16)
.with_scale(1)
.with_theme("Papirus")
.with_theme("cosmic-base")
.find();

asserting!("Given the same input parameter, lookup should output be the same as linincon")
.that(&wireshark)
.that(&cosmic_fake)
.is_some()
.is_equal_to(lin_wireshark);
.is_equal_to(lin_cosmic_fake);
}

#[test]
Expand Down
31 changes: 18 additions & 13 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ impl Debug for Theme {

#[cfg(test)]
mod test {
use crate::THEMES;
use crate::{
test::{set_fake_icons_path, TEST_ASSETS_PATH},
THEMES,
};
use speculoos::prelude::*;
use std::path::PathBuf;

#[test]
fn get_one_icon() {
let themes = THEMES.get("Adwaita").unwrap();
set_fake_icons_path();
let themes = THEMES.get("cosmic-base-dark").unwrap();
println!(
"{:?}",
themes.iter().find_map(|t| t.try_get_icon_exact_size(
Expand All @@ -233,24 +236,26 @@ mod test {
}

#[test]
fn should_get_png_first() {
fn should_get_svg_first() {
set_fake_icons_path();
let themes = THEMES.get("hicolor").unwrap();
let icon = themes
.iter()
.find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, true));
assert_that!(icon).is_some().is_equal_to(PathBuf::from(
"/usr/share/icons/hicolor/scalable/apps/blueman.svg",
));
.find_map(|t| t.try_get_icon_exact_size("cosmic-fake-applet", 24, 1, true));

let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/scalable/apps/cosmic-fake-applet.svg");
assert_that!(icon).is_some().is_equal_to(icon_path);
}

#[test]
fn should_get_svg_first() {
fn should_get_png_first() {
set_fake_icons_path();
let themes = THEMES.get("hicolor").unwrap();
let icon = themes
.iter()
.find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, false));
assert_that!(icon).is_some().is_equal_to(PathBuf::from(
"/usr/share/icons/hicolor/22x22/apps/blueman.png",
));
.find_map(|t| t.try_get_icon_exact_size("cosmic-cat-tracker", 22, 1, false));

let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/22x22/apps/cosmic-cat-tracker.png");
assert_that!(icon).is_some().is_equal_to(icon_path);
}
}
13 changes: 4 additions & 9 deletions src/theme/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,18 @@ impl Theme {

#[cfg(test)]
mod test {
use crate::THEMES;
use crate::{test::set_fake_icons_path, THEMES};
use speculoos::prelude::*;

#[test]
fn should_get_theme_parents() {
for theme in THEMES.get("Arc").unwrap() {
set_fake_icons_path();
for theme in THEMES.get("cosmic-base-dark").unwrap() {
let parents = theme.inherits();

assert_that!(parents).does_not_contain("hicolor");

assert_that!(parents).is_equal_to(vec![
"Moka",
"Faba",
"elementary",
"Adwaita",
"gnome",
]);
assert_that!(parents).is_equal_to(vec!["cosmic-base", "pop-os-base"]);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading