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

GitHub Actions用workflowを作成 #3

Merged
merged 4 commits into from
Nov 28, 2024
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
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-Dwarnings"

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Check
run: cargo check

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --verbose

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Lint with clippy
run: cargo clippy --all-targets --all-features

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Check formatting
run: cargo fmt --all --check
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ fn main() {
}

fn run_command(args: Cli) -> Result<(), anyhow::Error> {
Ok(match args.command {
match args.command {
Command::Init(args) => {
settings::gen_setting_file(&args)?;
}
Command::Run(args) => {
runner::run(args)?;
}
})
};
Ok(())
}
4 changes: 2 additions & 2 deletions src/runner/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(super) fn save_summary_log(
stats: &multi::TestStats,
comment: &str,
) -> Result<()> {
let mut writer = match OpenOptions::new().write(true).append(true).open(&path) {
let mut writer = match OpenOptions::new().append(true).open(&path) {
Ok(file) => BufWriter::new(file),
Err(_) => {
create_parent_dir(&path)?;
Expand Down Expand Up @@ -120,7 +120,7 @@ fn save_summary_log_inner(
.to_formatted_string(&Locale::en);

let score_log10 = stats.score_sum_log10;
let average_score_log10 = stats.score_sum_log10 as f64 / stats.results.len() as f64;
let average_score_log10 = stats.score_sum_log10 / stats.results.len() as f64;

writeln!(
writer,
Expand Down
7 changes: 3 additions & 4 deletions src/runner/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ mod test {
use crate::runner::single::{Objective, TestStep};
use printer::MockPrinter;
use regex::Regex;
use std::{cell::LazyCell, num::NonZero};
use std::num::NonZero;

const SCORE_REGEX: LazyCell<Regex> =
LazyCell::new(|| Regex::new(r"^\s*Score\s*=\s*(?P<score>\d+)\s*$").unwrap());
thread_local!(static SCORE_REGEX: Regex = Regex::new(r"^\s*Score\s*=\s*(?P<score>\d+)\s*$").unwrap());

#[test]
fn test_multi_case_runner() {
Expand All @@ -159,7 +158,7 @@ mod test {
None,
true,
)];
let single_runner = SingleCaseRunner::new(steps, SCORE_REGEX.clone());
let single_runner = SingleCaseRunner::new(steps, SCORE_REGEX.with(|r| r.clone()));
let test_cases = vec![
TestCase::new(0, NonZero::new(100), Objective::Max),
TestCase::new(1, NonZero::new(200), Objective::Max),
Expand Down
5 changes: 3 additions & 2 deletions src/runner/multi/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl Printer for ConsolePrinter {
match result.score() {
Ok(_) => writeln!(writer, "{}", record)?,
Err(e) => {
writeln!(writer, "{}", record.yellow().to_string())?;
writeln!(writer, "{}", e.to_string().yellow().to_string())?;
writeln!(writer, "{}", record.yellow())?;
writeln!(writer, "{}", e.to_string().yellow())?;
}
};

Expand Down Expand Up @@ -221,6 +221,7 @@ mod test {

#[test]
fn test_console_printer() {
colored::control::set_override(true);
let mut printer = ConsolePrinter::new(3);

let test_results = gen_test_results();
Expand Down
23 changes: 12 additions & 11 deletions src/runner/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,21 @@ impl SingleCaseRunner {
fn write_output(path: impl AsRef<OsStr>, contents: &[u8]) -> Result<()> {
let path = Path::new(&path);
Self::create_parent_dir_all(path)?;
std::fs::write(&path, contents)?;
std::fs::write(path, contents)?;

Ok(())
}

fn extract_score(&self, outputs: &[Vec<u8>]) -> Option<f64> {
outputs
.iter()
.map(|s| {
.filter_map(|s| {
let s = String::from_utf8_lossy(s);
self.score_pattern
.captures_iter(&s)
.filter_map(|m| m.name("score").map(|s| s.as_str().parse().ok()).flatten())
.filter_map(|m| m.name("score").and_then(|s| s.as_str().parse().ok()))
.last()
})
.flatten()
.last()
}

Expand All @@ -295,11 +294,9 @@ impl SingleCaseRunner {
#[cfg(test)]
mod test {
use super::*;
use std::cell::LazyCell;

const TEST_CASE: TestCase = TestCase::new(42, None, Objective::Max);
const SCORE_REGEX: LazyCell<Regex> =
LazyCell::new(|| Regex::new(r"^\s*Score\s*=\s*(?P<score>\d+)\s*$").unwrap());
thread_local!(static SCORE_REGEX: Regex = Regex::new(r"^\s*Score\s*=\s*(?P<score>\d+)\s*$").unwrap());

#[test]
fn test_calc_relative_score() {
Expand Down Expand Up @@ -345,15 +342,15 @@ mod test {
#[test]
fn run_test_ok() {
let steps = vec![gen_teststep("echo", Some("Score = 1234"))];
let runner = SingleCaseRunner::new(steps, SCORE_REGEX.clone());
let runner = SingleCaseRunner::new(steps, get_regex());
let result = runner.run(TEST_CASE);
assert_eq!(result.score(), &Ok(NonZeroU64::new(1234).unwrap()));
}

#[test]
fn run_test_score_zero() {
let steps = vec![gen_teststep("echo", Some("Score = 0"))];
let runner = SingleCaseRunner::new(steps, SCORE_REGEX.clone());
let runner = SingleCaseRunner::new(steps, get_regex());
let result = runner.run(TEST_CASE);

// 0点以下はWrong Answerとして扱う
Expand All @@ -363,15 +360,15 @@ mod test {
#[test]
fn run_test_fail() {
let steps = vec![gen_teststep("false", None)];
let runner = SingleCaseRunner::new(steps, SCORE_REGEX.clone());
let runner = SingleCaseRunner::new(steps, get_regex());
let result = runner.run(TEST_CASE);
assert!(result.score.is_err());
}

#[test]
fn run_test_invalid_output() {
let steps = vec![gen_teststep("echo", Some("invalid_output"))];
let runner = SingleCaseRunner::new(steps, SCORE_REGEX.clone());
let runner = SingleCaseRunner::new(steps, get_regex());
let result = runner.run(TEST_CASE);
assert!(result.score.is_err());
}
Expand All @@ -380,4 +377,8 @@ mod test {
let args = arg.iter().map(|s| s.to_string()).collect();
TestStep::new(program.to_string(), args, None, None, None, None, true)
}

fn get_regex() -> Regex {
SCORE_REGEX.with(|r| r.clone())
}
}
18 changes: 9 additions & 9 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn gen_run_steps(lang: Box<dyn Language>, is_interactive: bool) -> Vec<TestStep>

fn gen_gitignore(dir: impl AsRef<OsStr>) -> Result<()> {
let dir = Path::new(&dir);
std::fs::create_dir_all(&dir)?;
std::fs::create_dir_all(dir)?;

let path = dir.join(".gitignore");

Expand Down Expand Up @@ -288,9 +288,9 @@ impl Language for Cpp {

fn test_command(&self, is_interactive: bool) -> (String, Vec<String>) {
if is_interactive {
return ("../a.out".to_string(), vec![]);
("../a.out".to_string(), vec![])
} else {
return ("./a.out".to_string(), vec![]);
("./a.out".to_string(), vec![])
}
}
}
Expand Down Expand Up @@ -319,19 +319,19 @@ impl Language for Go {
"go".to_string(),
vec![
"build".to_string(),
"-o".to_string(),
"a.out".to_string(),
"main.go".to_string(),
"-o".to_string(),
"a.out".to_string(),
"main.go".to_string(),
],
None,
)]
}

fn test_command(&self, is_interactive: bool) -> (String, Vec<String>) {
if is_interactive {
return ("../a.out".to_string(), vec![]);
("../a.out".to_string(), vec![])
} else {
return ("./a.out".to_string(), vec![]);
("./a.out".to_string(), vec![])
}
}
}
}