Skip to content

Commit

Permalink
Add info subcommand (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 19, 2022
1 parent 99931bd commit e80b838
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
mod epochs;
mod find;
mod index;
mod info;
mod list;
mod name;
mod range;
Expand All @@ -18,6 +19,7 @@ pub(crate) enum Subcommand {
Name(name::Name),
Range(range::Range),
Supply,
Info,
Traits(traits::Traits),
}

Expand All @@ -31,6 +33,7 @@ impl Subcommand {
Self::Name(name) => name.run(),
Self::Range(range) => range.run(),
Self::Supply => supply::run(),
Self::Info => info::run(),
Self::Traits(traits) => traits.run(),
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/subcommand/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::*;

pub(crate) fn run() -> Result {
let database = unsafe { Database::open("index.redb")? };

let stats = database.stats()?;

println!("tree height: {}", stats.tree_height());
println!("free pages: {}", stats.free_pages());
println!("stored bytes: {}", stats.stored_bytes());
println!("overhead bytes: {}", stats.overhead_bytes());
println!("fragmented bytes: {}", stats.fragmented_bytes());

Ok(())
}
20 changes: 20 additions & 0 deletions tests/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::*;

#[test]
fn basic() -> Result {
let output = Test::new()?.command("index").block().output()?;

Test::with_tempdir(output.tempdir)
.command("info")
.stdout_regex(
r"
tree height: \d+
free pages: \d+
stored bytes: \d+
overhead bytes: \d+
fragmented bytes: \d+
"
.unindent(),
)
.run()
}
46 changes: 35 additions & 11 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use {
mod epochs;
mod find;
mod index;
mod info;
mod list;
mod name;
mod range;
Expand All @@ -23,6 +24,12 @@ mod traits;

type Result<T = ()> = std::result::Result<T, Box<dyn Error>>;

enum Expected {
String(String),
Regex(Regex),
Ignore,
}

struct Output {
stdout: String,
tempdir: TempDir,
Expand Down Expand Up @@ -55,22 +62,24 @@ struct Test {
blocks: Vec<Block>,
expected_status: i32,
expected_stderr: String,
expected_stdout: String,
ignore_stdout: bool,
expected_stdout: Expected,
tempdir: TempDir,
}

impl Test {
fn new() -> Result<Self> {
Ok(Self {
Ok(Self::with_tempdir(TempDir::new()?))
}

fn with_tempdir(tempdir: TempDir) -> Self {
Self {
args: Vec::new(),
blocks: Vec::new(),
expected_status: 0,
expected_stderr: String::new(),
expected_stdout: String::new(),
ignore_stdout: false,
tempdir: TempDir::new()?,
})
expected_stdout: Expected::String(String::new()),
tempdir,
}
}

fn command(self, args: &str) -> Self {
Expand All @@ -93,7 +102,16 @@ impl Test {

fn expected_stdout(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: expected_stdout.as_ref().to_owned(),
expected_stdout: Expected::String(expected_stdout.as_ref().to_owned()),
..self
}
}

fn stdout_regex(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::Regex(
Regex::new(&format!("^{}$", expected_stdout.as_ref())).unwrap(),
),
..self
}
}
Expand All @@ -114,7 +132,7 @@ impl Test {

fn ignore_stdout(self) -> Self {
Self {
ignore_stdout: true,
expected_stdout: Expected::Ignore,
..self
}
}
Expand Down Expand Up @@ -150,8 +168,14 @@ impl Test {

let stdout = str::from_utf8(&output.stdout)?;

if !self.ignore_stdout {
assert_eq!(stdout, self.expected_stdout);
match self.expected_stdout {
Expected::String(expected_stdout) => assert_eq!(stdout, expected_stdout),
Expected::Regex(expected_stdout) => assert!(
expected_stdout.is_match(stdout),
"stdout did not match regex: {}",
stdout
),
Expected::Ignore => {}
}

Ok(Output {
Expand Down

0 comments on commit e80b838

Please sign in to comment.