pub mod run;
use anyhow::{bail, Error};
use clap::{Parser, Subcommand};
use log::LevelFilter;
use run::zip;
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Zip { zip_dir: String },
}
#[allow(dead_code)]
enum Subcommands {
Add,
}
pub struct App {
zip_dir: PathBuf,
}
impl App {
pub fn new() -> Result<Self, Error> {
let cli = Cli::parse();
let Commands::Zip { zip_dir } = &cli.command;
let zip_dir = PathBuf::from(zip_dir);
if !zip_dir.exists() {
bail!("path {zip_dir:?} does not exist...");
}
Ok(Self { zip_dir })
}
pub fn run(&self) -> Result<(), Error> {
zip(&self.zip_dir)?;
Ok(())
}
}
pub fn init_logger() {
env_logger::builder().filter_level(LevelFilter::Info).init();
}
#[cfg(test)] mod test {
use std::{fs, path::Path, io::Write, env::set_current_dir, process::Command};
use log::LevelFilter;
use super::*;
#[test]
fn test() {
init_logger();
fs::create_dir_all("./test/cs261/assignment1").unwrap();
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(Path::new("./test/cs261/assignment1/hello.c"))
.unwrap();
file.write_all("hello world".as_bytes()).unwrap();
set_current_dir(Path::new("./test/cs261")).unwrap();
zip(Path::new("./assignment1")).unwrap();
let _ = Command::new("unzip")
.args(["assignment1.zip"])
.output()
.expect("failed to unzip the assignment");
let p = Path::new("./hello.c");
let result = p.exists() && fs::read_to_string(p).unwrap() == "hello world";
set_current_dir(Path::new("../..")).unwrap();
fs::remove_dir_all(Path::new("test")).unwrap();
assert!(result);
}
#[test]
#[ignore] fn test_current_dir() {
env_logger::builder().filter_level(LevelFilter::Info).init();
let p = Path::new(".");
dbg!(p.canonicalize().unwrap());
set_current_dir(Path::new("./src")).unwrap();
dbg!(p.canonicalize().unwrap());
let np = Path::new(".");
dbg!(np.canonicalize().unwrap());
}
}