Skip to content

Commit

Permalink
chore: hacky test and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
techno-disaster committed Jul 22, 2024
1 parent 8efe014 commit fc9f338
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust Check Format and Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
check-format-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Check formatting
run: cargo fmt -- --check

- name: Test
run: cargo test -- --nocapture
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "lyricsrs"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "lyricsrs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
8 changes: 1 addition & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;

use lofty::file::AudioFile;
use lofty::probe::Probe;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::AtomicUsize;
Expand All @@ -20,6 +19,7 @@ use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct Track {
id: f64,
name: String,
Expand All @@ -32,12 +32,6 @@ struct Track {
synced_lyrics: Option<String>,
}

impl fmt::Display for Track {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "id: {}, name: {}, track name: {}, artist name: {}, album name: {}, duration: {}, instrumental: {}, plain lyrics available: {}, synced lyrics available: {}" , self.id, self.name, self.track_name, self.artist_name, self.album_name, self.duration, self.instrumental, self.plain_lyrics.is_some(), self.synced_lyrics.is_some())
}
}

#[tokio::main]
async fn main() {
let start_time = Instant::now();
Expand Down
88 changes: 88 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::{io::Error, path::PathBuf, str::FromStr};

use tokio::fs;

pub const SONGS: [&str; 5] = [
"Taylor Swift/THE TORTURED POETS DEPARTMENT: THE ANTHOLOGY/1. Fortnight (Ft. Post Malone).flac",
"Taylor Swift/THE TORTURED POETS DEPARTMENT: THE ANTHOLOGY/2. The Tortured Poets Department.mp3",
"Taylor Swift/THE TORTURED POETS DEPARTMENT: THE ANTHOLOGY/3. My Boy Only Breaks His Favorite Toys.m4a",
"Taylor Swift/reputation/2. End Game.flac",
"Lou Reed/The Best of Lou Reed/8. Perfect Day.m4a",
];

pub const BASE_DIR: &str = "Music";

async fn create_files_with_names(output_file: &PathBuf) {
let dirs = output_file.parent().expect("could not parse dirs");
let file_name = output_file.file_name().expect("could not parse name");

let _dirs = fs::create_dir_all(dirs).await;

let format = file_name
.to_str()
.expect("hm wtf")
.split(".")
.last()
.expect("could not get extension");

let source_file = match format {
"flac" => "tests/data/template.flac",
"mp3" => "tests/data/template.mp3",
"m4a" => "tests/data/template.m4a",
_ => todo!(),
};

let _copy = fs::copy(PathBuf::from(source_file), output_file).await;
}

pub async fn setup() {
let mut tasks = vec![];

for song_path in SONGS.iter() {
let mut path = PathBuf::from(BASE_DIR);
path.push(song_path);

tasks.push(tokio::spawn(async move {
create_files_with_names(&path).await;
}));
}

// Await all tasks to complete
for task in tasks {
task.await.expect("Failed to execute task");
}
println!("Files created successfully in folder: {}", BASE_DIR);
}

pub async fn cleanup() {
let _remove = fs::remove_dir_all(BASE_DIR).await.expect("cleanup failed");
}

pub async fn check_lrcs() -> bool {
let mut song_names: Vec<String> = Vec::new();

SONGS.iter().for_each(|song_path| {
let song_name = song_path
.split_at(song_path.rfind(".").expect("invalid song path"))
.0
.to_owned()
+ ".lrc";

song_names.push(song_name);
});
let mut all_exist = true;
for file in song_names.iter() {
let mut song_path = std::env::current_dir().expect("could not get curr dir");
song_path.push(BASE_DIR);
song_path.push(file);
if let Err(_) = fs::metadata(song_path.clone()).await {
println!("file {} does not exist", song_path.to_string_lossy());
all_exist = false;
break;
} else {
println!("Found lrc file {}", song_path.to_string_lossy());
}
}

all_exist
}
Binary file added tests/data/template.flac
Binary file not shown.
Binary file added tests/data/template.m4a
Binary file not shown.
Binary file added tests/data/template.mp3
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;
mod common;

#[tokio::test]
async fn test_cli() {
common::setup().await;

let target_dir = env::var("CARGO_MANIFEST_DIR").expect("could not get target dir");
let mut current_dir = env::current_dir().expect("could not get current dir");
current_dir.push(common::BASE_DIR);

let mut path = PathBuf::from(target_dir);

path.push("target/release/lyricsrs");
let output = Command::new(path)
.arg(current_dir)
.output()
.expect("Failed to execute command");

println!("{:?}", output);

assert!(output.status.success());

let stdout_str = String::from_utf8_lossy(&output.stdout);
// keep in sync with SONGS in common/mod.rs
let to_find = format!(
"Successful tasks: {}\nFailed tasks: 0\nTotal tasks: {}",
common::SONGS.len(),
common::SONGS.len()
);
assert!(stdout_str.contains(&to_find));

assert!(common::check_lrcs().await);

common::cleanup().await;
}

0 comments on commit fc9f338

Please sign in to comment.