Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat:) finish test file submodule #1

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ authors = ["Thanapat Chotipun <[email protected]>"]
edition = "2021"

[dependencies]
clap = { version = "2.33.3", features =["yaml","color","suggestions"] }
ansi_term = "0.12"
clap = { version = "^2.34.0", features =["yaml","color","suggestions"] }
glob = "0.3.0"
log = { version = "0.4.14", features = ["std"] }
rand = "0.8.4"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "^1.13.0", features = ["full"] }
postgres = "0.19.2"
walkdir = "2.3.2"
uuid = { version = "0.8", features = ["v4"] }
walkdir = "2.3.2"
65 changes: 0 additions & 65 deletions src/cli.yaml

This file was deleted.

85 changes: 77 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,85 @@
#![allow(non_snake_case)]

#[macro_use]
extern crate clap;
use clap::App;
use clap::{App, AppSettings, Arg, SubCommand};
use ansi_term::Colour;

mod modules;

fn main() {
let yaml = load_yaml!("cli.yaml");
let args = App::from_yaml(yaml).get_matches();
if args.subcommand.is_none() {}
if let Some(_testing) = args.subcommand_matches("test") {}
let args = App::new("AutoGrader")
.setting(AppSettings::SubcommandRequiredElseHelp)
.setting(AppSettings::ColoredHelp)
.version("0.0.1")
.about("Programming Competition Scoreboard")
.author("Thanapat Chotipun <[email protected]>")
.subcommands([
SubCommand::with_name("test")
.setting(AppSettings::ColoredHelp)
.about("Test configuration file for AutoGrader")
.arg(
Arg::with_name("input")
.required(true)
.takes_value(true)
.help("Config file to check")
.value_name("file"),
),
SubCommand::with_name("run")
.setting(AppSettings::ColoredHelp)
.about("Run single test case and solution")
.args(&[
Arg::with_name("test")
.short("t")
.takes_value(true)
.value_name("file")
.required(true)
.help("Autograder Test Config"),
Arg::with_name("input")
.short("i")
.takes_value(true)
.value_name("file")
.required(true)
.help("File to be tested"),
]),
SubCommand::with_name("serve")
.setting(AppSettings::ColoredHelp)
.about("Run AutoGrader Session")
.args(&[Arg::with_name("config")
.short("c")
.required(true)
.takes_value(true)
.value_name("file")
.help("AutoGRader Session Config file"),
Arg::with_name("logginf").short("l")
.value_name("level")
.help("Set logging level")
.long_help("0 - No logging\n1 - Logging to file\n2 - Logging to STDOUT\n3 - Logging both file and STDOUT")
]
),
]).get_matches();
if let Some(_testing) = args.subcommand_matches("test") {
let filename = _testing.value_of("input").unwrap();
match modules::parser::parse_ext(&filename)
.ok()
.unwrap_or([""].to_vec())[..]
{
["test.", "json"] => {
println!("{} Parse as {} config....",Colour::Blue.bold().paint("\u{24D8}"),Colour::Purple.bold().paint("Session"));
if modules::config::parse_test_config(&filename).is_some() {
println!("{} This file is valid {} config",Colour::Green.bold().paint("\u{2714}"),Colour::Purple.bold().paint("Session"));
} else {
println!("{} Not valid!!!",Colour::Red.bold().paint("\u{2718}"));
}
}
["json"] => {
println!("{} Parse as {} config....",Colour::Blue.bold().paint("\u{24D8}"),Colour::Purple.bold().paint("Session"));
if modules::config::parse_root_config(&filename).is_some() {
println!("{} This file is valid {} config",Colour::Green.bold().paint("\u{2714}"),Colour::Purple.bold().paint("Session"));
} else {
println!("{} Not valid!!!",Colour::Red.bold().paint("\u{2718}"));
}
}
_ => println!("This file isn't AutoGraderConfig file"),
}
}
if let Some(_run) = args.subcommand_matches("run") {}
if let Some(_serve) = args.subcommand_matches("serve") {}
}
8 changes: 4 additions & 4 deletions src/modules/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ pub struct TestConfig {
limit: Option<TestLimitConfig>,
}

pub fn parse_root_config(filename: &str) -> AutoGraderConfig {
pub fn parse_root_config(filename: &str) -> Option<AutoGraderConfig> {
let buffer = std::fs::read_to_string(filename);
match buffer {
Ok(d) => match from_str::<AutoGraderConfig>(&d) {
Ok(d) => d,
Err(e) => panic!("Config file cannot parse. Error: {}", e),
Ok(d) => Some(d),
Err(_) => None,
},
Err(e) => panic!("Config file not found. Error: {}", e),
Err(_) => None,
}
}

Expand Down
56 changes: 44 additions & 12 deletions src/modules/parser.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
use uuid::Uuid;
use walkdir::WalkDir;

pub fn parse_ext(filename: &str) -> Result<&str, &str> {
match filename.split_inclusive(".").last() {
Some(ext) => Ok(ext),
None => Err("No file extension found in file"),
pub struct Test {
id: Uuid,
cases: Option<Vec<TestCase>>,
score: u32,
}

pub struct TestCase {
id: Uuid,
input: String,
output: Option<String>,
score: Option<u32>,
}

pub fn parse_ext(filename: &str) -> Result<Vec<&str>, &str> {
let sep: Vec<&str> = filename.split_inclusive(".").collect();
if sep.len() > 0 {
return Ok(sep[1..].to_vec());
} else {
return Err("No file extension found.");
}
}

pub fn load_input(filename: &str) -> Option<String> {
pub fn load_testcase(filename: &str) -> Option<TestCase> {
let buffer = std::fs::read_to_string(filename);
match buffer {
Ok(data) => Some(data),
Ok(data) => Some(TestCase {
id: Uuid::new_v4(),
input: data,
output: None,
score: None,
}),
Err(_) => None,
}
}

pub fn find_input(dirname: &str) -> Option<Vec<String>> {
let mut input_streams: Vec<String> = vec![];
pub fn find_testcases(dirname: &str) -> Option<Vec<TestCase>> {
let mut input_streams: Vec<TestCase> = vec![];
for e in WalkDir::new(dirname).into_iter().filter_map(|e| e.ok()) {
if e.metadata().unwrap().is_file() {
let filepath = e.path().display().to_string();
if parse_ext(&filepath).ok() == Some("txt") {
match load_input(&filepath) {
if parse_ext(&filepath)
.ok()
.unwrap_or([""].to_vec())
.last()
.unwrap()
== &"txt"
{
match load_testcase(&filepath) {
Some(d) => input_streams.push(d),
None => (),
}
Expand All @@ -36,12 +63,17 @@ pub fn find_input(dirname: &str) -> Option<Vec<String>> {
}

#[cfg(test)]
#[test]
fn parse_config_and_input_files() {
assert_eq!(parse_ext("tests/config.json").unwrap(),["json"].to_vec());
}

#[test]
fn read_input_file() {
assert!(load_input("tests/sum/sum1.txt").is_some());
assert!(load_testcase("tests/sum/sum1.txt").is_some());
}

#[test]
fn find_input_cases() {
assert!(find_input("tests/sum").is_some());
assert!(find_testcases("tests/sum").is_some());
}
1 change: 0 additions & 1 deletion src/modules/tester.rs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

3 changes: 3 additions & 0 deletions tests/sum/sum.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <iostream>

int main() {
int n1,n2;
std::cin >> n1 >> n2;
std::cout << n1+n2 << std::endl;
return 0;
}
2 changes: 1 addition & 1 deletion tests/sum/sum1.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5 2 3 4 5
5 2
2 changes: 1 addition & 1 deletion tests/sum/sum2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1 5 2 4 5
1 5