Skip to content

Commit

Permalink
Fix CI tests on win and osx
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Mar 3, 2024
1 parent d23748b commit 7e27595
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 47 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test:

# Generate docs
docs:
cargo run --package xtask -- docs
just xtask docs

# Build
build *ARGS:
Expand All @@ -50,6 +50,14 @@ run *ARGS:
x PATTERN:
cargo run -- x "{{PATTERN}}"

# Run xtask
xtask TASK:
cargo run --package xtask -- {{TASK}}

# Compute md5 from stdin
md5:
just xtask md5

# Run fuzzer
fuzz:
cargo +nightly fuzz run --jobs {{num_cpus()}} pattern
Expand Down
1 change: 1 addition & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Better shell specific examples for `rew x`.
- Refactor and split `x.rs` into multiple files.
- Finish user guide (performance, caveats, global opts propagation in `rew x`, forward compatibility, versioning).
- Make `rew x` escape sequences forward compatible.
- Setup CI (gh-pages on different path /rew-ng).
- Setup CI (release).
- Setup man generator.
Expand Down
20 changes: 10 additions & 10 deletions tests/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ command_test!("cat", {
defalt_empty: [ cmd assert "" => "" ],
defalt_items: [ cmd assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij" ],
defalt_non_utf8: [ cmd assert &[0x00, 0x9f, 0x92, 0x96] => vec![0x00, 0x9f, 0x92, 0x96] ],
// seq 1 10000 | md5sum
default_big: [ sh "seq 1 10000 | %cmd% | md5sum" assert "" => "72d4ff27a28afbc066d5804999d5a504 -\n" ],
// seq 1 10000 | just md5
default_big: [ sh "seq 1 10000 | %cmd% | %md5%" assert "" => "72d4ff27a28afbc066d5804999d5a504\n" ],
bytes_empty: [ cmd "-b" assert "" => "" ],
bytes_items: [ cmd "-b" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij" ],
bytes_buf_mode: [ cmd "-b" "--buf-mode=line" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij" ],
bytes_buf_size: [ cmd "-b" "--buf-size=8" assert "123456789" => "123456789" ],
bytes_non_utf8: [ cmd "-b" assert &[0x00, 0x9f, 0x92, 0x96] => vec![0x00, 0x9f, 0x92, 0x96] ],
// seq 1 10000 | md5sum
bytes_big: [ sh "seq 1 10000 | %cmd% -b | md5sum" assert "" => "72d4ff27a28afbc066d5804999d5a504 -\n" ],
// seq 1 10000 | just md5
bytes_big: [ sh "seq 1 10000 | %cmd% -b | %md5%" assert "" => "72d4ff27a28afbc066d5804999d5a504\n" ],
chars_empty: [ cmd "-c" assert "" => "" ],
chars_items: [ cmd "-c" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij" ],
chars_buf_mode: [ cmd "-c" "--buf-mode=line" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij" ],
chars_buf_size: [ cmd "-c" "--buf-size=8" assert "123456789" => "123456789" ],
chars_non_utf8: [ cmd "-c" assert &[0x00, 0x9f, 0x92, 0x96] => vec![0x00, 0x9f, 0x92, 0x96] ],
chars_buf_over: [ cmd "-c" "--buf-size=1" assert "á" => err "could not fetch utf-8 character longer than '1' bytes\n" ],
// seq 1 10000 | md5sum
chars_big: [ sh "seq 1 10000 | %cmd% -c | md5sum" assert "" => "72d4ff27a28afbc066d5804999d5a504 -\n" ],
// seq 1 10000 | just md5
chars_big: [ sh "seq 1 10000 | %cmd% -c | %md5%" assert "" => "72d4ff27a28afbc066d5804999d5a504\n" ],
lines_empty: [ cmd "-l" assert "" => "" ],
lines_items: [ cmd "-l" assert "a\nbc\r\ndef\0ghij" => "a\nbc\ndef\0ghij\n" ],
lines_buf_mode: [ cmd "-l" "--buf-mode=line" assert "a\nbc\r\ndef\0ghij" => "a\nbc\ndef\0ghij\n" ],
Expand All @@ -31,15 +31,15 @@ command_test!("cat", {
lines_cr_buf_exact: [ cmd "-l" "--buf-size=8" assert "123456\r\n789" => "123456\n789\n" ],
lines_cr_buf_over: [ cmd "-l" "--buf-size=8" assert "1234567\r\n89" => err "could not fetch line longer than '8' bytes\n" ],
lines_non_utf8: [ cmd "-l" assert &[0x00, 0x9f, 0x92, 0x96] => vec![0x00, 0x9f, 0x92, 0x96, 0x0a] ],
// seq 1 10000 | md5sum
lines_big: [ sh "seq 1 10000 | %cmd% -l | md5sum" assert "" => "72d4ff27a28afbc066d5804999d5a504 -\n" ],
// seq 1 10000 | just md5
lines_big: [ sh "seq 1 10000 | %cmd% -l | %md5%" assert "" => "72d4ff27a28afbc066d5804999d5a504\n" ],
records_empty: [ cmd "-l0" assert "" => "" ],
records_items: [ cmd "-l0" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij\0" ],
records_buf_mode: [ cmd "-l0" "--buf-mode=line" assert "a\nbc\r\ndef\0ghij" => "a\nbc\r\ndef\0ghij\0" ],
records_buf_under: [ cmd "-l0" "--buf-size=8" assert "123456\x00789" => "123456\x00789\0" ],
records_buf_exact: [ cmd "-l0" "--buf-size=8" assert "1234567\089" => "1234567\089\0" ],
records_buf_over: [ cmd "-l0" "--buf-size=8" assert "12345678\09" => err "could not fetch line longer than '8' bytes\n" ],
records_non_utf8: [ cmd "-l0" assert &[0x00, 0x9f, 0x92, 0x96] => vec![0x00, 0x9f, 0x92, 0x96, 0x00] ],
// seq 1 10000 | tr '\n' '\0' | md5sum
records_big: [ sh "seq 1 10000 | tr '\\n' '\\0' | %cmd% -l0 | md5sum" assert "" => "05cb1e39ef75550ae349fb6f08cd6000 -\n" ],
// seq 1 10000 | tr '\n' '\0' | just md5
records_big: [ sh "seq 1 10000 | tr '\\n' '\\0' | %cmd% -l0 | %md5%" assert "" => "05cb1e39ef75550ae349fb6f08cd6000\n" ],
});
4 changes: 2 additions & 2 deletions tests/commands/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ command_test!("first", {
buf_exact: [ cmd "--buf-size=8" assert "aaaaaaa\nb\n" => "aaaaaaa\n" ],
buf_over: [ cmd "--buf-size=8" assert "aaaaaaaa\nb\n" => "aaaaaaaa\n" ],
buf_over_2: [ cmd "--buf-size=8" assert "aaaaaaaaa\nb\n" => "aaaaaaaaa\n" ],
// seq 1 10000 | head -n9999 | md5sum
many: [ sh "seq 1 10000 | %cmd% 9999 | md5sum" assert "" => "05fda6bec6aabc94d0fc54380ace8412 -\n" ],
// seq 1 10000 | head -n9999 | just md5
many: [ sh "seq 1 10000 | %cmd% 9999 | %md5%" assert "" => "05fda6bec6aabc94d0fc54380ace8412\n" ],
});
4 changes: 2 additions & 2 deletions tests/commands/last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ command_test!("last", {
buf_exact: [ cmd "--buf-size=8" assert "aaaaaaa\nb\n" => "b\n" ],
buf_over: [ cmd "--buf-size=8" assert "aaaaaaaa\nb\n" => "b\n" ],
buf_over_2: [ cmd "--buf-size=8" assert "aaaaaaaaa\nb\n" => "b\n" ],
// seq 1 20000 | tail -n10000 | md5sum
many: [ sh "seq 1 20000 | %cmd% 10000 | md5sum" assert "" => "8857ef28723cc4788a8ca7456214fc0c -\n" ],
// seq 1 20000 | tail -n10000 | just md5
many: [ sh "seq 1 20000 | %cmd% 10000 | %md5%" assert "" => "8857ef28723cc4788a8ca7456214fc0c\n" ],
});
8 changes: 4 additions & 4 deletions tests/commands/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ command_test!("loop", {
multi_2: [ cmd "2" assert "a\nbc\n" => "a\nbc\na\nbc\n" ],
multi_3: [ cmd "3" assert "a\nbc\n" => "a\nbc\na\nbc\na\nbc\n" ],
buff_over: [ cmd "8" "--buf-size=8" assert "012345" => "012345012345012345012345012345012345012345012345" ],
// for((i=0; i<3; i++)); do seq 1 10000; done | md5sum
big_finite: [ sh "seq 1 10000 | %cmd% 3 | md5sum" assert "" => "5f0f7f173c062a0d128ff75ded51b09b -\n" ],
// for((i=0; i<10; i++)); do seq 1 10000; done | head -n40000 | md5sum
big_infinite: [ sh "seq 1 10000 | %cmd% | head -n40000 | md5sum" assert "" => "3aeff2d35d8836dfbbdfe882848b5d30 -\n" ],
// for((i=0; i<3; i++)); do seq 1 10000; done | just md5
big_finite: [ sh "seq 1 10000 | %cmd% 3 | %md5%" assert "" => "5f0f7f173c062a0d128ff75ded51b09b\n" ],
// for((i=0; i<10; i++)); do seq 1 10000; done | head -n40000 | just md5
big_infinite: [ sh "seq 1 10000 | %cmd% | head -n40000 | %md5%" assert "" => "3aeff2d35d8836dfbbdfe882848b5d30\n" ],
});
6 changes: 3 additions & 3 deletions tests/commands/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ command_test!("skip", {
buf_exact: [ cmd "1" "--buf-size=8" assert "aaaaaaa\nb\n" => "b\n" ],
buf_over: [ cmd "1" "--buf-size=8" assert "aaaaaaaa\nb\n" => "b\n" ],
buf_over_2: [ cmd "1" "--buf-size=8" assert "aaaaaaaaa\nb\n" => "b\n" ],
// seq 1 20000 | tail -n-10000 | md5sum
many: [ sh "seq 1 20000 | %cmd% 10000 | md5sum" assert "" => "8857ef28723cc4788a8ca7456214fc0c -\n" ],
many_buf_line: [ sh "seq 1 20000 | %cmd% --buf-mode=line 10000 | md5sum" assert "" => "8857ef28723cc4788a8ca7456214fc0c -\n" ],
// seq 1 20000 | tail -n-10000 | just md5
many: [ sh "seq 1 20000 | %cmd% 10000 | %md5%" assert "" => "8857ef28723cc4788a8ca7456214fc0c\n" ],
many_buf_line: [ sh "seq 1 20000 | %cmd% --buf-mode=line 10000 | %md5%" assert "" => "8857ef28723cc4788a8ca7456214fc0c\n" ],
});
15 changes: 10 additions & 5 deletions tests/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ pub fn assert_command(name: &str, args: &[&str], stdin: impl Into<Vec<u8>>) -> A
}

pub fn assert_shell(template: &str, cmd: &str, stdin: impl Into<Vec<u8>>) -> Assert {
let bin = get_bin();
let bin = get_bin(crate_name!());
let sh = env::var_os("SHELL").unwrap_or("sh".into());
let sh_cmd = template
let mut sh_cmd = template
.replace("%bin%", &bin)
.replace("%cmd%", &format!("{bin} {cmd}"));

if sh_cmd.contains("%md5%") {
let md5 = format!("{} md5", get_bin("xtask"));
sh_cmd = sh_cmd.replace("%md5%", &md5);
}

Command::new(sh)
.arg("-c")
.arg(sh_cmd)
Expand All @@ -88,11 +93,11 @@ pub fn assert_shell(template: &str, cmd: &str, stdin: impl Into<Vec<u8>>) -> Ass
}

pub fn expand_err(cmd: &str, message: &str) -> String {
format!("{} {cmd}: error: {message}", get_bin())
format!("{} {cmd}: error: {message}", get_bin(crate_name!()))
}

fn get_bin() -> String {
process::Command::cargo_bin(crate_name!())
fn get_bin(name: &str) -> String {
process::Command::cargo_bin(name)
.unwrap()
.get_program()
.to_string_lossy()
Expand Down
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ publish = false
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true, features = ["cargo", "derive"] }
md5 = "0.7.0"
rew = { path = ".." }
58 changes: 38 additions & 20 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ mod command;
mod docs;

use anyhow::format_err;
use anyhow::Result;
use clap::Parser;
use clap::Subcommand;
use command::Adapter;
use docs::generate_reference;
use docs::generate_summary;
use rew::app;
use rew::commands::METAS;
use rew::io::ByteChunkReader;
use std::env;
use std::fs::create_dir_all;
use std::io::stdin;
use std::path::Path;

/// Execute project task.
Expand All @@ -28,36 +31,51 @@ pub enum Task {
/// Generate man pages.
#[command()]
Man,
/// Compute md5 from stdin.
#[command()]
Md5,
}

fn main() -> anyhow::Result<()> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
fn main() -> Result<()> {
let cli = Cli::parse();

match cli.task {
Task::Docs => task_docs(),
Task::Man => unimplemented!(),
Task::Md5 => task_md5(),
}
}

fn task_docs() -> Result<()> {
let mut app = app::build(METAS);
app.build();

let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let root_path = Path::new(&manifest_dir)
.parent()
.ok_or_else(|| format_err!("'{}' does not have parent", manifest_dir))?;

let cli = Cli::parse();
let docs_path = root_path.join("docs");
let reference_path = docs_path.join("reference");
let summary_path = docs_path.join("SUMMARY.md");

let mut app = app::build(METAS);
app.build();
if !reference_path.exists() {
create_dir_all(&reference_path)?;
}

match cli.task {
Task::Docs => {
let docs_path = root_path.join("docs");
let reference_path = docs_path.join("reference");
let summary_path = docs_path.join("SUMMARY.md");

if !reference_path.exists() {
create_dir_all(&reference_path)?;
}

let adapter = Adapter::new(&app);
generate_summary(&adapter, &summary_path)?;
generate_reference(&adapter, &reference_path)?;
}
Task::Man => unimplemented!(),
let adapter = Adapter::new(&app);
generate_summary(&adapter, &summary_path)?;
generate_reference(&adapter, &reference_path)
}

fn task_md5() -> Result<()> {
let mut context = md5::Context::new();
let mut reader = ByteChunkReader::new(stdin().lock(), 32 * 1024);

while let Some(chunk) = reader.read_chunk()? {
context.consume(chunk);
}

println!("{:?}", context.compute());
Ok(())
}

0 comments on commit 7e27595

Please sign in to comment.