From bd2c97542fb0993b8e635397820d9c56e5c2a49b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 13 Sep 2024 22:50:11 +0200 Subject: [PATCH] Add test case to ensure all errors are in use --- crates/utils/src/error.rs | 11 +--------- crates/utils/tests/test_errors_used.rs | 28 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 crates/utils/tests/test_errors_used.rs diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 4e634bde3..d805ecf0f 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -1,6 +1,6 @@ use cfg_if::cfg_if; use serde::{Deserialize, Serialize}; -use std::{backtrace::Backtrace, fmt::Debug}; +use std::fmt::Debug; use strum::{Display, EnumIter}; #[derive(Display, Debug, Serialize, Deserialize, Clone, PartialEq, Eq, EnumIter, Hash)] @@ -23,19 +23,14 @@ pub enum LemmyErrorType { CouldntUpdateComment, CouldntUpdatePrivateMessage, CannotLeaveAdmin, - NoLinesInHtml, - SiteMetadataPageIsNotDoctypeHtml, PictrsResponseError(String), PictrsPurgeResponseError(String), - PictrsCachingDisabled, ImageUrlMissingPathSegments, ImageUrlMissingLastPathSegment, PictrsApiKeyNotProvided, NoContentTypeHeader, NotAnImageType, NotAModOrAdmin, - NoAdmins, - NotTopAdmin, NotTopMod, NotLoggedIn, NotHigherMod, @@ -84,18 +79,15 @@ pub enum LemmyErrorType { RegistrationClosed, RegistrationApplicationAnswerRequired, EmailAlreadyExists, - FederationForbiddenByStrictAllowList, PersonIsBannedFromCommunity, ObjectIsNotPublic, InvalidCommunity, CannotCreatePostOrCommentInDeletedOrRemovedCommunity, CannotReceivePage, - NewPostCannotBeLocked, OnlyLocalAdminCanRemoveCommunity, OnlyLocalAdminCanRestoreCommunity, NoIdGiven, IncorrectLogin, - InvalidQuery, ObjectNotLocal, PostIsLocked, PersonIsBannedFromSite(String), @@ -138,7 +130,6 @@ pub enum LemmyErrorType { CouldntUpdateCommunity, CouldntUpdateReplies, CouldntUpdatePersonMentions, - PostTitleTooLong, CouldntCreatePost, CouldntCreatePrivateMessage, CouldntUpdatePrivate, diff --git a/crates/utils/tests/test_errors_used.rs b/crates/utils/tests/test_errors_used.rs new file mode 100644 index 000000000..5324ee45a --- /dev/null +++ b/crates/utils/tests/test_errors_used.rs @@ -0,0 +1,28 @@ +use lemmy_utils::LemmyErrorType; +use std::{env::current_dir, process::Command}; +use strum::IntoEnumIterator; + +#[test] +fn test_errors_used() { + let mut unused_error_found = false; + let mut current_dir = current_dir().unwrap(); + current_dir.pop(); + current_dir.pop(); + for error in LemmyErrorType::iter() { + let mut command = Command::new("grep"); + let command = command + .current_dir(current_dir.clone()) + .arg("-R") + .arg("--exclude=error.rs") + .arg(error.to_string()) + .arg("crates/") + .arg("src/"); + let output = command.output().unwrap(); + let stdout = std::str::from_utf8(&output.stdout).unwrap(); + if stdout.len() == 0 { + println!("LemmyErrorType::{} is unused", error); + unused_error_found = true; + } + } + assert!(unused_error_found == false); +}