From 79ef0717512ed55596c2dec748c7160f4fb31fbd Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sat, 22 Apr 2023 11:59:32 +0200 Subject: [PATCH 1/2] fix: add expect message to client construction --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ae4f684..e8b014c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,7 @@ impl Musixmatch { .gzip(true) .cookie_store(true) .build() - .unwrap(); + .expect("http client could not be constructed"); Self { inner: Arc::new(MusixmatchRef { From 7589ec5051b304a76034430c7cce34f9f808b9e8 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 23 Apr 2023 15:04:20 +0200 Subject: [PATCH 2/2] fix: testfile paths --- .env.example | 2 ++ Cargo.toml | 1 + README.md | 9 +++++++++ tests/tests.rs | 35 ++++++++++++++++++++++------------- 4 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7ab1bf9 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +MUSIXMATCH_EMAIL=mail@example.com +MUSIXMATCH_PASSWORD=super-secret diff --git a/Cargo.toml b/Cargo.toml index 17b0119..525922b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ env_logger = "0.10.0" dotenvy = "0.15.5" tokio = { version = "1.20.0", features = ["macros"] } futures = "0.3.21" +path_macro = "1.0.0" [profile.release] strip = true diff --git a/README.md b/README.md index 95a10f6..9e83d62 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,12 @@ lyrics site/app. If you want to use Musixmatch data for this purpose, you will have to give them money (see their [commercial plans](https://developer.musixmatch.com/plans)) and use their [official API](https://developer.musixmatch.com/documentation). + +## Development info + +Running the tests requires Musixmatch credentials. The credentials are read +from the `MUSIXMATCH_EMAIL` and `MUSIXMATCH_PASSWORD` environment variables. + +To make local development easier, I have included `dotenvy` to read the +credentials from an `.env` file. Copy the `.env.example` file +in the root directory, rename it to `.env` and fill in your credentials. diff --git a/tests/tests.rs b/tests/tests.rs index 00a3663..2159cfb 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,3 +1,6 @@ +use std::path::{Path, PathBuf}; + +use path_macro::path; use rstest::rstest; use time::macros::{date, datetime}; @@ -19,7 +22,7 @@ fn init() { .unwrap(); } -pub fn new_mxm() -> Musixmatch { +fn new_mxm() -> Musixmatch { Musixmatch::new( &std::env::var("MUSIXMATCH_EMAIL").unwrap(), &std::env::var("MUSIXMATCH_PASSWORD").unwrap(), @@ -27,6 +30,10 @@ pub fn new_mxm() -> Musixmatch { ) } +fn testfile>(name: P) -> PathBuf { + path!(env!("CARGO_MANIFEST_DIR") / "testfiles" / name) +} + mod album { use super::*; use musixmatch_inofficial::models::AlbumType; @@ -679,7 +686,7 @@ mod track { mod lyrics { use futures::stream::{self, StreamExt}; - use std::{fs::File, io::BufWriter, path::Path}; + use std::{fs::File, io::BufWriter}; use super::*; @@ -756,7 +763,7 @@ mod lyrics { #[tokio::test] async fn download_testdata() { - let json_path = Path::new("testfiles/lyrics.json"); + let json_path = testfile("lyrics.json"); if json_path.exists() { return; } @@ -772,7 +779,7 @@ mod lyrics { #[tokio::test] async fn download_testdata_translation() { - let json_path = Path::new("testfiles/translation.json"); + let json_path = testfile("translation.json"); if json_path.exists() { return; } @@ -818,7 +825,7 @@ mod lyrics { } mod subtitles { - use std::{fs::File, io::BufWriter, path::Path}; + use std::{fs::File, io::BufWriter}; use super::*; use musixmatch_inofficial::models::SubtitleFormat; @@ -925,7 +932,7 @@ mod subtitles { #[tokio::test] async fn download_testdata() { - let json_path = Path::new("testfiles/subtitles.json"); + let json_path = testfile("subtitles.json"); if json_path.exists() { return; } @@ -946,15 +953,17 @@ mod subtitles { } mod translation { - use std::{fs::File, io::BufReader, path::Path}; + use std::{fs::File, io::BufReader}; use musixmatch_inofficial::models::{Lyrics, Subtitle, TranslationList, TranslationMap}; + use crate::testfile; + #[test] fn translation_test() { - let lyrics_path = Path::new("testfiles/lyrics.json"); - let subtitles_path = Path::new("testfiles/subtitles.json"); - let translation_path = Path::new("testfiles/translation.json"); + let lyrics_path = testfile("lyrics.json"); + let subtitles_path = testfile("subtitles.json"); + let translation_path = testfile("translation.json"); let lyrics: Lyrics = serde_json::from_reader(BufReader::new(File::open(lyrics_path).unwrap())).unwrap(); @@ -966,13 +975,13 @@ mod translation { let t_map = TranslationMap::from(translations); let lyrics_trans = t_map.translate_lyrics(&lyrics.lyrics_body); - let expected_lyrics = std::fs::read_to_string("testfiles/translated_lyrics.txt").unwrap(); + let expected_lyrics = std::fs::read_to_string(testfile("translated_lyrics.txt")).unwrap(); assert_eq!(lyrics_trans.trim(), expected_lyrics.trim()); let subtitles_trans = t_map.translate_subtitles(&subtitle.to_lines().unwrap()); - let expected_lrc = std::fs::read_to_string("testfiles/translated_subtitles.lrc").unwrap(); - let expected_ttml = std::fs::read_to_string("testfiles/translated_subtitles.xml").unwrap(); + let expected_lrc = std::fs::read_to_string(testfile("translated_subtitles.lrc")).unwrap(); + let expected_ttml = std::fs::read_to_string(testfile("translated_subtitles.xml")).unwrap(); assert_eq!(subtitles_trans.to_lrc().trim(), expected_lrc.trim()); assert_eq!(subtitles_trans.to_ttml().trim(), expected_ttml.trim());