diff --git a/Cargo.toml b/Cargo.toml index 0f64a18..a02243d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,25 +30,35 @@ fancy-regex = "0.10.0" thiserror = "1.0.36" url = "2.2.2" log = "0.4.17" -reqwest = {version = "0.11.11", default-features = false, features = ["json", "gzip", "brotli", "stream"]} -tokio = {version = "1.20.0", features = ["macros", "time", "fs", "process"]} +reqwest = { version = "0.11.11", default-features = false, features = [ + "json", + "gzip", + "brotli", + "stream", +] } +tokio = { version = "1.20.0", features = ["macros", "time", "fs", "process"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.82" -serde_with = {version = "2.0.0", features = ["json"] } +serde_with = { version = "2.0.0", features = ["json"] } rand = "0.8.5" -time = {version = "0.3.15", features = ["macros", "serde", "serde-well-known"]} +time = { version = "0.3.15", features = [ + "macros", + "serde", + "serde-well-known", +] } futures = "0.3.21" indicatif = "0.17.0" filenamify = "0.1.0" ress = "0.11.4" -phf = "0.11.1" +phf = "0.11.1" base64 = "0.13.0" -quick-xml = {version = "0.26.0", features = ["serialize"], optional = true} +quick-xml = { version = "0.26.0", features = ["serialize"], optional = true } [dev-dependencies] env_logger = "0.10.0" test-log = "0.2.11" rstest = "0.16.0" temp_testdir = "0.2.3" -insta = {version = "1.17.1", features = ["ron", "redactions"]} +insta = { version = "1.17.1", features = ["ron", "redactions"] } +path_macro = "1.0.0" velcro = "0.5.3" diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 9841503..28aca53 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -rustypipe = {path = "../"} +rustypipe = { path = "../" } reqwest = "0.11.11" -tokio = {version = "1.20.0", features = ["macros", "rt-multi-thread"]} +tokio = { version = "1.20.0", features = ["macros", "rt-multi-thread"] } futures = "0.3.21" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.82" @@ -20,3 +20,4 @@ once_cell = "1.12.0" fancy-regex = "0.10.0" indicatif = "0.17.0" num_enum = "0.5.7" +path_macro = "1.0.0" diff --git a/codegen/src/collect_album_types.rs b/codegen/src/collect_album_types.rs index 2bd6009..4a8740a 100644 --- a/codegen/src/collect_album_types.rs +++ b/codegen/src/collect_album_types.rs @@ -1,6 +1,7 @@ use std::{collections::BTreeMap, fs::File, io::BufReader, path::Path}; use futures::stream::{self, StreamExt}; +use path_macro::path; use rustypipe::{ client::{ClientType, RustyPipe, RustyPipeQuery, YTContext}, model::AlbumType, @@ -11,8 +12,7 @@ use serde::{Deserialize, Serialize}; use crate::util::{self, TextRuns}; pub async fn collect_album_types(project_root: &Path, concurrency: usize) { - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/album_type_samples.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "album_type_samples.json"); let album_types = [ (AlbumType::Album, "MPREb_nlBWQROfvjo"), @@ -49,8 +49,7 @@ pub async fn collect_album_types(project_root: &Path, concurrency: usize) { } pub fn write_samples_to_dict(project_root: &Path) { - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/album_type_samples.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "album_type_samples.json"); let json_file = File::open(json_path).unwrap(); let collected: BTreeMap> = diff --git a/codegen/src/collect_large_numbers.rs b/codegen/src/collect_large_numbers.rs index ca2aee8..be9eb5f 100644 --- a/codegen/src/collect_large_numbers.rs +++ b/codegen/src/collect_large_numbers.rs @@ -5,6 +5,7 @@ use anyhow::{Context, Result}; use fancy_regex::Regex; use futures::{stream, StreamExt}; use once_cell::sync::Lazy; +use path_macro::path; use reqwest::{header, Client}; use rustypipe::param::{locale::LANGUAGES, Language}; use serde::Deserialize; @@ -30,11 +31,9 @@ type CollectedNumbers = BTreeMap>; /// outputs view counts both in approximated and exact format, so we can use /// the exact counts to figure out the tokens. pub async fn collect_large_numbers(project_root: &Path, concurrency: usize) { - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/large_number_samples.json"); - - let mut json_path_all = project_root.to_path_buf(); - json_path_all.push("testfiles/dict/large_number_samples_all.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "large_number_samples.json"); + let json_path_all = + path!(project_root / "testfiles" / "dict" / "large_number_samples_all.json"); let channels = [ "UCq-Fj5jknLsUf-MWSy4_brA", // 10e8 (225M) @@ -117,8 +116,7 @@ pub fn write_samples_to_dict(project_root: &Path) { "M": 6 */ - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/large_number_samples.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "large_number_samples.json"); let json_file = File::open(json_path).unwrap(); let collected_nums: CollectedNumbers = diff --git a/codegen/src/collect_playlist_dates.rs b/codegen/src/collect_playlist_dates.rs index c10e528..21f5ae6 100644 --- a/codegen/src/collect_playlist_dates.rs +++ b/codegen/src/collect_playlist_dates.rs @@ -7,6 +7,7 @@ use std::{ }; use futures::{stream, StreamExt}; +use path_macro::path; use rustypipe::{ client::RustyPipe, param::{locale::LANGUAGES, Language}, @@ -62,8 +63,7 @@ enum DateCase { /// Because the relative dates change with time, the first three playlists /// have to checked and eventually changed before running the program. pub async fn collect_dates(project_root: &Path, concurrency: usize) { - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/playlist_samples.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "playlist_samples.json"); // These are the sample playlists let cases = [ @@ -116,8 +116,7 @@ pub async fn collect_dates(project_root: &Path, concurrency: usize) { /// The ND (no digit) tokens (today, tomorrow) of some languages cannot be /// parsed automatically and require manual work. pub fn write_samples_to_dict(project_root: &Path) { - let mut json_path = project_root.to_path_buf(); - json_path.push("testfiles/dict/playlist_samples.json"); + let json_path = path!(project_root / "testfiles" / "dict" / "playlist_samples.json"); let json_file = File::open(json_path).unwrap(); let collected_dates: CollectedDates = diff --git a/codegen/src/util.rs b/codegen/src/util.rs index e5244d3..ceb2d8a 100644 --- a/codegen/src/util.rs +++ b/codegen/src/util.rs @@ -1,9 +1,17 @@ -use std::{collections::BTreeMap, fs::File, io::BufReader, path::Path, str::FromStr}; +use std::{ + collections::BTreeMap, + fs::File, + io::BufReader, + path::{Path, PathBuf}, + str::FromStr, +}; +use once_cell::sync::Lazy; +use path_macro::path; use rustypipe::{model::AlbumType, param::Language}; use serde::{Deserialize, Serialize}; -const DICT_PATH: &str = "testfiles/dict/dictionary.json"; +static DICT_PATH: Lazy = Lazy::new(|| path!("testfiles" / "dict" / "dictionary.json")); type Dictionary = BTreeMap; @@ -62,15 +70,13 @@ pub struct Text { } pub fn read_dict(project_root: &Path) -> Dictionary { - let mut json_path = project_root.to_path_buf(); - json_path.push(DICT_PATH); + let json_path = path!(project_root / *DICT_PATH); let json_file = File::open(json_path).unwrap(); serde_json::from_reader(BufReader::new(json_file)).unwrap() } pub fn write_dict(project_root: &Path, dict: &Dictionary) { - let mut json_path = project_root.to_path_buf(); - json_path.push(DICT_PATH); + let json_path = path!(project_root / *DICT_PATH); let json_file = File::create(json_path).unwrap(); serde_json::to_writer_pretty(json_file, dict).unwrap(); } diff --git a/src/client/channel.rs b/src/client/channel.rs index f140cec..4673c55 100644 --- a/src/client/channel.rs +++ b/src/client/channel.rs @@ -509,8 +509,9 @@ fn combine_channel_data(channel_data: Channel<()>, content: T) -> Channel #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use crate::{ @@ -532,8 +533,7 @@ mod tests { #[case::shorts("shorts", "UCh8gHdtzO2tXd593_bjErWg")] #[case::livestreams("livestreams", "UC2DjFE7Xf11URZqWBigcVOQ")] fn map_channel_videos(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/channel/channel_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "channel" / format!("channel_{}.json", name)); let json_file = File::open(json_path).unwrap(); let channel: response::Channel = @@ -560,7 +560,7 @@ mod tests { #[test] fn map_channel_playlists() { - let json_path = Path::new("testfiles/channel/channel_playlists.json"); + let json_path = path!("testfiles" / "channel" / "channel_playlists.json"); let json_file = File::open(json_path).unwrap(); let channel: response::Channel = @@ -579,7 +579,7 @@ mod tests { #[test] fn map_channel_info() { - let json_path = Path::new("testfiles/channel/channel_info.json"); + let json_path = path!("testfiles" / "channel" / "channel_info.json"); let json_file = File::open(json_path).unwrap(); let channel: response::Channel = diff --git a/src/client/channel_rss.rs b/src/client/channel_rss.rs index 4da7c24..8ce758d 100644 --- a/src/client/channel_rss.rs +++ b/src/client/channel_rss.rs @@ -60,10 +60,11 @@ impl RustyPipeQuery { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; use crate::{client::response, model::ChannelRss}; + use path_macro::path; use rstest::rstest; #[rstest] @@ -71,8 +72,7 @@ mod tests { #[case::no_likes("no_likes")] #[case::no_channel_id("no_channel_id")] fn map_channel_rss(#[case] name: &str) { - let filename = format!("testfiles/channel_rss/{}.xml", name); - let xml_path = Path::new(&filename); + let xml_path = path!("testfiles" / "channel_rss" / format!("{}.xml", name)); let xml_file = File::open(xml_path).unwrap(); let feed: response::ChannelRss = diff --git a/src/client/music_artist.rs b/src/client/music_artist.rs index 3c0f0b7..de79cd9 100644 --- a/src/client/music_artist.rs +++ b/src/client/music_artist.rs @@ -305,12 +305,9 @@ impl MapResponse> for response::MusicArtistAlbums { #[cfg(test)] mod tests { - use std::{ - fs::File, - io::BufReader, - path::{Path, PathBuf}, - }; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use crate::param::Language; @@ -324,16 +321,13 @@ mod tests { #[case::no_artist("no_artist", "UCh8gHdtzO2tXd593_bjErWg")] #[case::only_more_singles("only_more_singles", "UC0aXrjVxG5pZr99v77wZdPQ")] fn map_music_artist(#[case] name: &str, #[case] id: &str) { - let filename_base = format!("testfiles/music_artist/artist_{}", name); - - let filename = filename_base.clone() + ".json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_artist" / format!("artist_{}.json", name)); let json_file = File::open(json_path).unwrap(); let mut album_page_paths = Vec::new(); for i in 1..=2 { - let filename = format!("{}_{}.json", filename_base, i); - let json_path = PathBuf::from(filename); + let json_path = + path!("testfiles" / "music_artist" / format!("artist_{}_{}.json", name, i)); if !json_path.exists() { break; } @@ -373,7 +367,7 @@ mod tests { #[test] fn map_music_artist_no_cont() { - let json_path = Path::new("testfiles/music_artist/artist_default.json"); + let json_path = path!("testfiles" / "music_artist" / "artist_default.json"); let json_file = File::open(json_path).unwrap(); let artist: response::MusicArtist = diff --git a/src/client/music_details.rs b/src/client/music_details.rs index 07ae014..69bcf58 100644 --- a/src/client/music_details.rs +++ b/src/client/music_details.rs @@ -365,8 +365,9 @@ impl MapResponse for response::MusicRelated { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use super::*; @@ -376,8 +377,7 @@ mod tests { #[case::mv("mv", "ZeerrnuLi5E")] #[case::track("track", "7nigXQS1Xb0")] fn map_music_details(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/music_details/details_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_details" / format!("details_{}.json", name)); let json_file = File::open(json_path).unwrap(); let details: response::MusicDetails = @@ -397,8 +397,7 @@ mod tests { #[case::mv("mv", "RDAMVMZeerrnuLi5E")] #[case::track("track", "RDAMVM7nigXQS1Xb0")] fn map_music_radio(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/music_details/radio_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_details" / format!("radio_{}.json", name)); let json_file = File::open(json_path).unwrap(); let radio: response::MusicDetails = @@ -416,7 +415,7 @@ mod tests { #[test] fn map_lyrics() { - let json_path = Path::new("testfiles/music_details/lyrics.json"); + let json_path = path!("testfiles" / "music_details" / "lyrics.json"); let json_file = File::open(json_path).unwrap(); let lyrics: response::MusicLyrics = @@ -433,7 +432,7 @@ mod tests { #[test] fn map_related() { - let json_path = Path::new("testfiles/music_details/related.json"); + let json_path = path!("testfiles" / "music_details" / "related.json"); let json_file = File::open(json_path).unwrap(); let lyrics: response::MusicRelated = diff --git a/src/client/music_new.rs b/src/client/music_new.rs index 7885054..1195bf5 100644 --- a/src/client/music_new.rs +++ b/src/client/music_new.rs @@ -77,8 +77,9 @@ impl MapResponse> for response::MusicNew { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use super::*; @@ -87,8 +88,7 @@ mod tests { #[rstest] #[case::default("default")] fn map_music_new_albums(#[case] name: &str) { - let filename = format!("testfiles/music_new/albums_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_new" / format!("albums_{}.json", name)); let json_file = File::open(json_path).unwrap(); let new_albums: response::MusicNew = @@ -107,8 +107,7 @@ mod tests { #[rstest] #[case::default("default")] fn map_music_new_videos(#[case] name: &str) { - let filename = format!("testfiles/music_new/videos_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_new" / format!("videos_{}.json", name)); let json_file = File::open(json_path).unwrap(); let new_albums: response::MusicNew = diff --git a/src/client/music_playlist.rs b/src/client/music_playlist.rs index 65d9c37..1e4f2cf 100644 --- a/src/client/music_playlist.rs +++ b/src/client/music_playlist.rs @@ -257,8 +257,9 @@ impl MapResponse for response::MusicPlaylist { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use super::*; @@ -269,8 +270,7 @@ mod tests { #[case::long("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ")] #[case::nomusic("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe")] fn map_music_playlist(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/music_playlist/playlist_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_playlist" / format!("playlist_{}.json", name)); let json_file = File::open(json_path).unwrap(); let playlist: response::MusicPlaylist = @@ -294,8 +294,7 @@ mod tests { #[case::single("single", "MPREb_bHfHGoy7vuv")] #[case::description("description", "MPREb_PiyfuVl6aYd")] fn map_music_album(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/music_playlist/album_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_playlist" / format!("album_{}.json", name)); let json_file = File::open(json_path).unwrap(); let playlist: response::MusicPlaylist = diff --git a/src/client/music_search.rs b/src/client/music_search.rs index 3cc7ba1..dcdaf6a 100644 --- a/src/client/music_search.rs +++ b/src/client/music_search.rs @@ -354,7 +354,10 @@ impl MapResponse> for response::MusicSearchSuggestion { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + + use path_macro::path; + use rstest::rstest; use crate::{ client::{response, MapResponse}, @@ -366,15 +369,12 @@ mod tests { serializer::MapResult, }; - use rstest::rstest; - #[rstest] #[case::default("default")] #[case::typo("typo")] #[case::radio("radio")] fn map_music_search_main(#[case] name: &str) { - let filename = format!("testfiles/music_search/main_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / format!("main_{}.json", name)); let json_file = File::open(json_path).unwrap(); let search: response::MusicSearch = @@ -397,8 +397,7 @@ mod tests { #[case::videos("videos")] #[case::no_artist_link("no_artist_link")] fn map_music_search_tracks(#[case] name: &str) { - let filename = format!("testfiles/music_search/tracks_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / format!("tracks_{}.json", name)); let json_file = File::open(json_path).unwrap(); let search: response::MusicSearch = @@ -417,8 +416,7 @@ mod tests { #[test] fn map_music_search_albums() { - let filename = "testfiles/music_search/albums.json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / "albums.json"); let json_file = File::open(json_path).unwrap(); let search: response::MusicSearch = @@ -437,8 +435,7 @@ mod tests { #[test] fn map_music_search_artists() { - let filename = "testfiles/music_search/artists.json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / "artists.json"); let json_file = File::open(json_path).unwrap(); let search: response::MusicSearch = @@ -459,8 +456,7 @@ mod tests { #[case::ytm("ytm")] #[case::community("community")] fn map_music_search_playlists(#[case] name: &str) { - let filename = format!("testfiles/music_search/playlists_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / format!("playlists_{}.json", name)); let json_file = File::open(json_path).unwrap(); let search: response::MusicSearch = @@ -481,8 +477,7 @@ mod tests { #[case::default("default")] #[case::empty("empty")] fn map_music_search_suggestion(#[case] name: &str) { - let filename = format!("testfiles/music_search/suggestion_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "music_search" / format!("suggestion_{}.json", name)); let json_file = File::open(json_path).unwrap(); let suggestion: response::MusicSearchSuggestion = diff --git a/src/client/pagination.rs b/src/client/pagination.rs index 69f3251..5ec9e7e 100644 --- a/src/client/pagination.rs +++ b/src/client/pagination.rs @@ -307,8 +307,9 @@ paginator!(PlaylistVideo); #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader, path::PathBuf}; + use path_macro::path; use rstest::rstest; use super::*; @@ -316,12 +317,11 @@ mod tests { use crate::param::Language; #[rstest] - #[case("search", "search/cont")] - #[case("startpage", "trends/startpage_cont")] - #[case("recommendations", "video_details/recommendations")] - fn map_continuation_items(#[case] name: &str, #[case] path: &str) { - let filename = format!("testfiles/{}.json", path); - let json_path = Path::new(&filename); + #[case("search", path!("search" / "cont.json"))] + #[case("startpage", path!("trends" / "startpage_cont.json"))] + #[case("recommendations", path!("video_details" / "recommendations.json"))] + fn map_continuation_items(#[case] name: &str, #[case] path: PathBuf) { + let json_path = path!("testfiles" / path); let json_file = File::open(json_path).unwrap(); let items: response::Continuation = @@ -340,10 +340,9 @@ mod tests { } #[rstest] - #[case("channel_playlists", "channel/channel_playlists_cont")] - fn map_continuation_playlists(#[case] name: &str, #[case] path: &str) { - let filename = format!("testfiles/{}.json", path); - let json_path = Path::new(&filename); + #[case("channel_playlists", path!("channel" / "channel_playlists_cont.json"))] + fn map_continuation_playlists(#[case] name: &str, #[case] path: PathBuf) { + let json_path = path!("testfiles" / path); let json_file = File::open(json_path).unwrap(); let items: response::Continuation = @@ -362,12 +361,11 @@ mod tests { } #[rstest] - #[case("playlist_tracks", "music_playlist/playlist_cont")] - #[case("search_tracks", "music_search/tracks_cont")] - #[case("radio_tracks", "music_details/radio_cont")] - fn map_continuation_tracks(#[case] name: &str, #[case] path: &str) { - let filename = format!("testfiles/{}.json", path); - let json_path = Path::new(&filename); + #[case("playlist_tracks", path!("music_playlist" / "playlist_cont.json"))] + #[case("search_tracks", path!("music_search" / "tracks_cont.json"))] + #[case("radio_tracks", path!("music_details" / "radio_cont.json"))] + fn map_continuation_tracks(#[case] name: &str, #[case] path: PathBuf) { + let json_path = path!("testfiles" / path); let json_file = File::open(json_path).unwrap(); let items: response::MusicContinuation = @@ -386,10 +384,9 @@ mod tests { } #[rstest] - #[case("playlist_related", "music_playlist/playlist_related")] - fn map_continuation_music_playlists(#[case] name: &str, #[case] path: &str) { - let filename = format!("testfiles/{}.json", path); - let json_path = Path::new(&filename); + #[case("playlist_related", path!("music_playlist" / "playlist_related.json"))] + fn map_continuation_music_playlists(#[case] name: &str, #[case] path: PathBuf) { + let json_path = path!("testfiles" / path); let json_file = File::open(json_path).unwrap(); let items: response::MusicContinuation = diff --git a/src/client/player.rs b/src/client/player.rs index 27ec7b8..ac0abcf 100644 --- a/src/client/player.rs +++ b/src/client/player.rs @@ -611,12 +611,13 @@ fn get_audio_codec(codecs: Vec<&str>) -> AudioCodec { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; - use crate::deobfuscate::DeobfData; + use path_macro::path; + use rstest::rstest; use super::*; - use rstest::rstest; + use crate::deobfuscate::DeobfData; static DEOBFUSCATOR: Lazy = Lazy::new(|| { Deobfuscator::from(DeobfData { @@ -634,8 +635,7 @@ mod tests { #[case::android("android")] #[case::ios("ios")] fn map_player_data(#[case] name: &str) { - let filename = format!("testfiles/player/{}_video.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "player" / format!("{}_video.json", name)); let json_file = File::open(json_path).unwrap(); let resp: response::Player = serde_json::from_reader(BufReader::new(json_file)).unwrap(); diff --git a/src/client/playlist.rs b/src/client/playlist.rs index ad00e27..2c54c5d 100644 --- a/src/client/playlist.rs +++ b/src/client/playlist.rs @@ -227,8 +227,9 @@ fn map_playlist_items( #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use super::*; @@ -238,8 +239,7 @@ mod tests { #[case::long("long", "PL5dDx681T4bR7ZF1IuWzOv1omlRbE7PiJ")] #[case::nomusic("nomusic", "PL1J-6JOckZtE_P9Xx8D3b2O6w0idhuKBe")] fn map_playlist_data(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/playlist/playlist_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "playlist" / format!("playlist_{}.json", name)); let json_file = File::open(json_path).unwrap(); let playlist: response::Playlist = @@ -258,7 +258,7 @@ mod tests { #[test] fn map_playlist_cont() { - let json_path = Path::new("testfiles/playlist/playlist_cont.json"); + let json_path = path!("testfiles" / "playlist" / "playlist_cont.json"); let json_file = File::open(json_path).unwrap(); let playlist: response::PlaylistCont = diff --git a/src/client/response/music_item.rs b/src/client/response/music_item.rs index 74ec617..c505b02 100644 --- a/src/client/response/music_item.rs +++ b/src/client/response/music_item.rs @@ -935,13 +935,15 @@ pub(crate) fn map_queue_item(item: QueueMusicItem, lang: Language) -> TrackItem #[cfg(test)] mod tests { - use std::{collections::BTreeMap, fs::File, io::BufReader, path::Path}; + use std::{collections::BTreeMap, fs::File, io::BufReader}; + + use path_macro::path; use super::*; #[test] fn map_album_type_samples() { - let json_path = Path::new("testfiles/dict/album_type_samples.json"); + let json_path = path!("testfiles" / "dict" / "album_type_samples.json"); let json_file = File::open(json_path).unwrap(); let atype_samples: BTreeMap> = serde_json::from_reader(BufReader::new(json_file)).unwrap(); diff --git a/src/client/search.rs b/src/client/search.rs index 0194b7e..2e7baae 100644 --- a/src/client/search.rs +++ b/src/client/search.rs @@ -124,7 +124,10 @@ impl MapResponse for response::Search { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + + use path_macro::path; + use rstest::rstest; use crate::{ client::{response, MapResponse}, @@ -133,16 +136,13 @@ mod tests { serializer::MapResult, }; - use rstest::rstest; - #[rstest] #[case::default("default")] #[case::playlists("playlists")] #[case::empty("empty")] #[case::ab3_channel_handles("20221121_AB3_channel_handles")] fn t_map_search(#[case] name: &str) { - let filename = format!("testfiles/search/{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "search" / format!("{}.json", name)); let json_file = File::open(json_path).unwrap(); let search: response::Search = serde_json::from_reader(BufReader::new(json_file)).unwrap(); diff --git a/src/client/trends.rs b/src/client/trends.rs index 6d0f8a3..8b884ae 100644 --- a/src/client/trends.rs +++ b/src/client/trends.rs @@ -118,7 +118,9 @@ fn map_startpage_videos( #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + + use path_macro::path; use crate::{ client::{response, MapResponse}, @@ -129,8 +131,7 @@ mod tests { #[test] fn map_startpage() { - let filename = "testfiles/trends/startpage.json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "trends" / "startpage.json"); let json_file = File::open(json_path).unwrap(); let startpage: response::Startpage = @@ -151,8 +152,7 @@ mod tests { #[test] fn map_trending() { - let filename = "testfiles/trends/trending.json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "trends" / "trending.json"); let json_file = File::open(json_path).unwrap(); let startpage: response::Trending = diff --git a/src/client/video_details.rs b/src/client/video_details.rs index 62300e4..7910556 100644 --- a/src/client/video_details.rs +++ b/src/client/video_details.rs @@ -526,8 +526,9 @@ fn map_comment( #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use crate::{ @@ -546,8 +547,8 @@ mod tests { #[case::new_cont("20221011_new_continuation", "ZeerrnuLi5E")] #[case::no_recommends("20221011_rec_isr", "nFDBxBUfE74")] fn map_video_details(#[case] name: &str, #[case] id: &str) { - let filename = format!("testfiles/video_details/video_details_{}.json", name); - let json_path = Path::new(&filename); + let json_path = + path!("testfiles" / "video_details" / format!("video_details_{}.json", name)); let json_file = File::open(json_path).unwrap(); let details: response::VideoDetails = @@ -567,8 +568,7 @@ mod tests { #[test] fn map_video_details_not_found() { - let filename = "testfiles/video_details/video_details_not_found.json"; - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "video_details" / "video_details_not_found.json"); let json_file = File::open(json_path).unwrap(); let details: response::VideoDetails = @@ -584,8 +584,7 @@ mod tests { #[case::top("top")] #[case::latest("latest")] fn map_comments(#[case] name: &str) { - let filename = format!("testfiles/video_details/comments_{}.json", name); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "video_details" / format!("comments_{}.json", name)); let json_file = File::open(json_path).unwrap(); let comments: response::VideoComments = diff --git a/src/deobfuscate.rs b/src/deobfuscate.rs index f4723c8..03e1b98 100644 --- a/src/deobfuscate.rs +++ b/src/deobfuscate.rs @@ -314,13 +314,12 @@ fn get_sts(player_js: &str) -> Result { #[cfg(test)] mod tests { - use std::path::Path; - use super::*; + use path_macro::path; use test_log::test; static TEST_JS: Lazy = Lazy::new(|| { - let js_path = Path::new("testfiles/deobf/dummy_player.js"); + let js_path = path!("testfiles" / "deobf" / "dummy_player.js"); std::fs::read_to_string(js_path).unwrap() }); diff --git a/src/param/stream_filter.rs b/src/param/stream_filter.rs index 64d32c5..c33420e 100644 --- a/src/param/stream_filter.rs +++ b/src/param/stream_filter.rs @@ -318,22 +318,23 @@ impl VideoPlayer { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; use super::*; use once_cell::sync::Lazy; + use path_macro::path; use rstest::rstest; use velcro::hash_set; static PLAYER_ML: Lazy = Lazy::new(|| { - let json_path = Path::new("testfiles/player_model/multilanguage.json"); + let json_path = path!("testfiles" / "player_model" / "multilanguage.json"); let json_file = File::open(json_path).unwrap(); serde_json::from_reader(BufReader::new(json_file)).unwrap() }); static PLAYER_HDR: Lazy = Lazy::new(|| { - let json_path = Path::new("testfiles/player_model/hdr.json"); + let json_path = path!("testfiles" / "player_model" / "hdr.json"); let json_file = File::open(json_path).unwrap(); serde_json::from_reader(BufReader::new(json_file)).unwrap() diff --git a/src/serializer/text.rs b/src/serializer/text.rs index e00d8a3..587daf0 100644 --- a/src/serializer/text.rs +++ b/src/serializer/text.rs @@ -469,14 +469,15 @@ impl<'de> DeserializeAs<'de, String> for AccessibilityText { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; - - use super::*; + use std::{fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use serde::Deserialize; use serde_with::serde_as; + use super::*; + #[rstest] #[case( r#"{ @@ -783,8 +784,7 @@ mod tests { #[test] fn t_attributed_description() { - let filename = format!("testfiles/text/attributed_description.json"); - let json_path = Path::new(&filename); + let json_path = path!("testfiles" / "text" / "attributed_description.json"); let json_file = File::open(json_path).unwrap(); let res: SAttributed = serde_json::from_reader(BufReader::new(json_file)).unwrap(); insta::assert_debug_snapshot!(res); diff --git a/src/timeago.rs b/src/timeago.rs index 299a49a..92634ed 100644 --- a/src/timeago.rs +++ b/src/timeago.rs @@ -285,8 +285,9 @@ pub(crate) fn parse_textual_date_or_warn( #[cfg(test)] mod tests { - use std::{collections::BTreeMap, fs::File, io::BufReader, path::Path}; + use std::{collections::BTreeMap, fs::File, io::BufReader}; + use path_macro::path; use rstest::rstest; use time::macros::{date, datetime}; @@ -308,7 +309,7 @@ mod tests { #[test] fn t_testfile() { - let json_path = Path::new("testfiles/dict/timeago_samples.json"); + let json_path = path!("testfiles" / "dict" / "timeago_samples.json"); let expect = [ TimeAgo { @@ -491,7 +492,7 @@ mod tests { cases: BTreeMap, } - let json_path = Path::new("testfiles/dict/timeago_table.json"); + let json_path = path!("testfiles" / "dict" / "timeago_table.json"); let json_file = File::open(json_path).unwrap(); let timeago_table: TimeagoTable = serde_json::from_reader(BufReader::new(json_file)).unwrap(); @@ -538,7 +539,7 @@ mod tests { #[test] fn t_parse_date_samples() { - let json_path = Path::new("testfiles/dict/playlist_samples.json"); + let json_path = path!("testfiles" / "dict" / "playlist_samples.json"); let json_file = File::open(json_path).unwrap(); let date_samples: BTreeMap> = serde_json::from_reader(BufReader::new(json_file)).unwrap(); diff --git a/src/util/mod.rs b/src/util/mod.rs index eff901a..fac8ea7 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -351,12 +351,13 @@ pub fn video_id_from_thumbnail_url(url: &str) -> Option { #[cfg(test)] mod tests { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; + + use path_macro::path; + use rstest::rstest; use super::*; - use rstest::rstest; - #[rstest] #[case("1.000", 1000)] #[case("4 Hello World 2", 42)] @@ -439,7 +440,7 @@ mod tests { #[test] fn t_parse_large_numstr_samples() { - let json_path = Path::new("testfiles/dict/large_number_samples.json"); + let json_path = path!("testfiles" / "dict" / "large_number_samples.json"); let json_file = File::open(json_path).unwrap(); let number_samples: BTreeMap> = serde_json::from_reader(BufReader::new(json_file)).unwrap(); @@ -453,7 +454,7 @@ mod tests { #[test] fn t_parse_large_numstr_samples2() { - let json_path = Path::new("testfiles/dict/large_number_samples_all.json"); + let json_path = path!("testfiles" / "dict" / "large_number_samples_all.json"); let json_file = File::open(json_path).unwrap(); let number_samples: BTreeMap> = serde_json::from_reader(BufReader::new(json_file)).unwrap();