Skip to content

Commit

Permalink
tests: Start implementing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuyamat committed Dec 17, 2023
1 parent 834a1e4 commit f9f9fa3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.idea/
.idea/
servers/
4 changes: 2 additions & 2 deletions src/cli/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn execute(args: Args, mut config: Config, theme: &Theme) -> Result<(
let server = config
.get_server(&name)
.ok_or(Error::ResourceNotFound("Server not found".to_string()))?;
server.run()?;
server.run(false)?;
}
DJ::Config { action } => {
config_cli::manage_config_action(action, &config)?;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn handle_server_action(
let server = config
.get_server(&name)
.ok_or(Error::ResourceNotFound("Server not found".to_string()))?;
server.run()?;
server.run(false)?;
}
ServerAction::Delete { name } => {
let server = config
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod error;
mod minecraft;
mod themes;
mod utils;
mod tests;

use cli::constructor;
use cli::executor;
Expand Down
2 changes: 1 addition & 1 deletion src/minecraft/jars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Jar {
let response = reqwest::blocking::get(url)?;
let lower_name = self.name.to_lowercase();
let mut builds: Vec<u32>;
if &lower_name == "paper" {
if &lower_name == "paper" || &lower_name == "waterfall" || &lower_name == "velocity" {
let mut body = response.json::<PaperJarBuildInfo>()?;
builds = body.get_builds()?;
} else if &lower_name == "purpur" {
Expand Down
15 changes: 12 additions & 3 deletions src/minecraft/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Server {
version,
build,
location: location.to_path_buf(),
gui: false,
gui: true,
xms: None,
xmx: None,
};
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Server {
);
}

pub fn run(&self) -> Result<(), Error> {
pub fn run(&self, accept_eula: bool) -> Result<(), Error> {
let server_info = self.clone();
self.print_info();
println!(
Expand Down Expand Up @@ -153,13 +153,22 @@ impl Server {
for line in reader.lines() {
let text = line.unwrap();
if text.contains("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.") {
if accept_eula {
self.accept_eula();
let server_clone = self.clone();
let server_copy = thread::spawn(move || server_clone.run(false)); // Create
// thread so we can get "out" of the loop
server_copy.join().unwrap()?;
break;
}
let input = read_line("🚨 EULA not accepted! Would you like to accept? (y/n)")?.to_lowercase();
if input == "y" {
println!("🛑 Stopping server");
process.kill().expect("Failed to kill child");
self.accept_eula();
let server_clone = self.clone();
let server_copy = thread::spawn(move || server_clone.run()); // Create thread so we can get "out" of the loop
let server_copy = thread::spawn(move || server_clone.run(false)); // Create
// thread so we can get "out" of the loop
server_copy.join().unwrap()?;
break;
}
Expand Down
56 changes: 56 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[cfg(test)]
mod tests {
use std::fs;
use crate::error::Error;
use crate::minecraft::jars;

#[test]
fn create_paper_server() -> Result<(), Error> {
create_server("paper", "paper_test")
}

#[test]
fn create_purpur_server() -> Result<(), Error> {
create_server("purpur", "purpur_test")
}

//TODO: Make sure velocity and waterfall exit properly

#[test]
fn create_velocity_server() -> Result<(), Error> {
create_server("velocity", "velocity_test")
}

#[test]
fn create_waterfall_server() -> Result<(), Error> {
create_server("waterfall", "waterfall_test")
}

fn create_server(jar_name: &str, server_name: &str) -> Result<(), Error> {
let jar_info = jars::load()?;
let jar = jar_info.get_jar(jar_name).unwrap();

let current_dir = std::env::current_dir()?;
let server_location = current_dir.join("servers").join(server_name);
fs::create_dir_all(&server_location)?;

let version = jar.get_versions()?;
let version = version.first().unwrap().to_string();
let build = jar.get_latest_build(&version).unwrap().to_string();
let server = jar.download(&version, &build, server_name, &server_location)?;

assert_eq!(server.server_name, server_name);

server.run(true)?;

let plugins_dir = server_location.join("plugins");

assert!(plugins_dir.exists());

server.delete();

assert!(!server_location.exists());

Ok(())
}
}

0 comments on commit f9f9fa3

Please sign in to comment.