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

refactor: Re-organize various test utilities #331

Merged
merged 4 commits into from
May 26, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ lz4_flex = "0.11.3"
metrics = "0.22.3"
# File event notifications for the live reloading feature
notify = "6.1.1"
once_cell = "1.19.0"
# Used to open external links like in the user's browser
open = "5.1.2"
# Some alternative atomics that are slightly more ergonoics than `std`'s
Expand Down
14 changes: 14 additions & 0 deletions assets/test_data/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This file attempts to serve as a way of linking back to the source of the
various assets used here

| file | source |
| --- | :--- |
| `bun_logo.png` | <ttps://user-images.githubusercontent.com/709451/182802334-d9c42afe-f35d-4a7b-86ea-9985f73f20c3.png> |
| `corro.svg` | <https://www.rustacean.net/assets/corro.svg> |
| `rgba?8.{gif,jpg,png,qoi}` | Programmatically generated |
| `sample.tmTheme`| Modified from <https://www.sublimetext.com/docs/color_schemes_tmtheme.html> |
| `sample_cargo_badge.svg` | <https://img.shields.io/crates/v/inlyne.svg> |
| `sample_repology_badge.svg` | <https://repology.org/badge/vertical-allrepos/inlyne.svg?exclude_unsupported=1)](https://repology.org/project/inlyne/versions> |
| `atuin_demo.gif` | Modified from <https://github.com/atuinsh/atuin/raw/main/demo.gif> |
| `ariadne_example.png` | <https://raw.githubusercontent.com/zesterer/ariadne/main/misc/example.png> |
| `cargo_public_api.webp` | <https://github.com/Enselic/cargo-public-api/blob/a5c063a170874e57666ba8107fec4285e4bdb775/docs/img/diff-specific-published-version.webp> |
Binary file added assets/test_data/ariadne_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test_data/atuin_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test_data/cargo_public_api.webp
Binary file not shown.
1 change: 1 addition & 0 deletions assets/test_data/corro.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/test_data/sample_cargo_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/test_data/sample_repology_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/file_watcher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ macro_rules! gen_watcher_test {
$(
#[test]
fn $test_name() {
$crate::test_utils::init_test_log();
$crate::test_utils::log::init();

// Give the test a few chances
let mut last_panic = None;
Expand Down
2 changes: 1 addition & 1 deletion src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct ImageData {
}

impl ImageData {
fn load(bytes: &[u8], scale: bool) -> anyhow::Result<Self> {
pub fn load(bytes: &[u8], scale: bool) -> anyhow::Result<Self> {
let (lz4_blob, dimensions) = decode::decode_and_compress(bytes)?;
Ok(Self {
lz4_blob,
Expand Down
4 changes: 2 additions & 2 deletions src/image/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;
use std::{fmt, fs};

use super::{ImageData, Px};
use crate::test_utils::init_test_log;
use crate::test_utils::log;

#[test]
fn px_parsing() {
Expand Down Expand Up @@ -32,7 +32,7 @@ fn check(input_path: &Path) {

#[test]
fn source_image_variety() {
init_test_log();
log::init();

for file in [
"rgb8.gif",
Expand Down
35 changes: 21 additions & 14 deletions src/interpreter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{HtmlInterpreter, ImageCallback, WindowInteractor};
use crate::color::{Theme, ThemeDefaults};
use crate::image::{Image, ImageData};
use crate::opts::ResolvedTheme;
use crate::test_utils::{init_test_log, mock_file_server, File, HttpServer};
use crate::test_utils::{log, server};
use crate::utils::Align;
use crate::{Element, ImageCache};

Expand Down Expand Up @@ -198,7 +198,7 @@ macro_rules! snapshot_interpreted_elements {
$(
#[test]
fn $test_name() {
$crate::test_utils::init_test_log();
$crate::test_utils::log::init();

let text = $md_text;
let opts = $opts;
Expand Down Expand Up @@ -417,12 +417,12 @@ snapshot_interpreted_elements!(

#[test]
fn centered_image_with_size_align_and_link() {
init_test_log();
log::init();

let logo = include_bytes!("../../assets/test_data/bun_logo.png");
let logo_path = "/bun_logo.png";
let files = vec![File::new(logo_path, "image/png", logo)];
let (_server, server_url) = mock_file_server(files);
let files = vec![server::File::new(logo_path, "image/png", logo)];
let (_server, server_url) = server::mock_file_server(files);
let logo_url = server_url + logo_path;

let text = format!(
Expand All @@ -443,11 +443,11 @@ fn centered_image_with_size_align_and_link() {

#[test]
fn image_loading_fails_gracefully() {
init_test_log();
log::init();

let json = r#"{"im": "not an image"}"#;
let json_path = "/snapshot.png";
let (_server, server_url) = mock_file_server(vec![File::new(
let (_server, server_url) = server::mock_file_server(vec![server::File::new(
json_path,
"application/json",
json.as_bytes(),
Expand Down Expand Up @@ -489,7 +489,7 @@ fn picture_dark_light() {
const B64_SINGLE_PIXEL_WEBP_999: &[u8] = b"UklGRhoAAABXRUJQVlA4TA4AAAAvAAAAAM1VICICzYyIBA==";
const B64_SINGLE_PIXEL_WEBP_FFF: &[u8] = b"UklGRhoAAABXRUJQVlA4TA4AAAAvAAAAAM1VICIC/Y+IBA==";

init_test_log();
log::init();

let light_path = "/light.webp";
let dark_path = "/dark.webp";
Expand All @@ -503,10 +503,10 @@ fn picture_dark_light() {
.into_iter()
.map(|(path, b64_bytes)| {
let bytes = BASE64_STANDARD.decode(b64_bytes).unwrap();
File::new(path, webp_mime, &bytes)
server::File::new(path, webp_mime, &bytes)
})
.collect();
let (_server, server_url) = mock_file_server(files);
let (_server, server_url) = server::mock_file_server(files);
let dark_url = format!("{server_url}{dark_path}");
let light_url = format!("{server_url}{light_path}");
let default_url = format!("{server_url}{default_path}");
Expand Down Expand Up @@ -557,17 +557,22 @@ fn picture_dark_light() {

#[test]
fn custom_user_agent() {
init_test_log();
log::init();

let (send_ua, recv_ua) = mpsc::channel();
let send_ua_server = HttpServer::spawn(send_ua, |send_ua, req| {
let state = server::State::new().send(send_ua);
let send_ua_server = server::spawn(state, |state, req, _req_url| {
let ua = req
.headers()
.iter()
.find_map(|Header { field, value }| field.equiv("user-agent").then(|| value.as_str()))
.unwrap_or("<not_set>")
.to_owned();
let _ = send_ua.send(ua);
let _ = state
.send
.as_ref()
.unwrap()
.send(server::FromServer::UserAgent(Some(ua)));
let sample_body = include_bytes!("../../assets/test_data/bun_logo.png");
Response::from_data(sample_body).boxed()
});
Expand All @@ -576,6 +581,8 @@ fn custom_user_agent() {
let text = format!(r"![Show me the UA]({server_url})");
let _ = interpret_md(&text);

let user_agent = recv_ua.recv().unwrap();
let server::FromServer::UserAgent(Some(user_agent)) = recv_ua.recv().unwrap() else {
panic!();
};
insta::assert_snapshot!(user_agent, @"inlyne 0.4.1 https://github.com/Inlyne-Project/inlyne");
}
4 changes: 2 additions & 2 deletions src/keybindings/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::action::{Action, VertDirection};
use super::{KeyCombos, Keybindings, ModifiedKey};
use crate::opts::Config;
use crate::test_utils::init_test_log;
use crate::test_utils::log;

use winit::event::{ModifiersState, VirtualKeyCode as VirtKey};

#[test]
fn sanity() {
init_test_log();
log::init();

let config = r#"
[keybindings]
Expand Down
4 changes: 2 additions & 2 deletions src/opts/tests/error_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! snapshot_config_parse_error {
$(
#[test]
fn $test_name() {
$crate::test_utils::init_test_log();
$crate::test_utils::log::init();

let err = $crate::opts::Config::load_from_str($config_text).unwrap_err();

Expand Down Expand Up @@ -49,7 +49,7 @@ macro_rules! snapshot_keycombo_conflict_err {
$(
#[test]
fn $test_name() {
$crate::test_utils::init_test_log();
$crate::test_utils::log::init();

let err = keycombo_conflict_from_config($config_text).unwrap();

Expand Down
Loading