Skip to content

Commit

Permalink
fix(test): ensure that binary is built before the test runs (#11)
Browse files Browse the repository at this point in the history
The `app.rs` test requires that the `binsider` binary has been built. By
moving the test into the `tests` directory, `cargo` will ensure that the
binary has been built before the test can be run.
  • Loading branch information
samueltardieu authored Sep 12, 2024
1 parent 3c98161 commit efb73b1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 53 deletions.
53 changes: 0 additions & 53 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,3 @@ impl<'a> Analyzer<'a> {
});
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::{fs, path::PathBuf};

fn get_test_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("debug")
.join(env!("CARGO_PKG_NAME"))
}

fn get_test_bytes() -> Result<Vec<u8>> {
let debug_binary = get_test_path();
Ok(fs::read(debug_binary)?)
}

#[test]
fn test_init() -> Result<()> {
Analyzer::new(
FileInfo::new(
get_test_path().to_str().expect("failed to get test path"),
get_test_bytes()?.as_slice(),
)?,
4,
vec![],
)
.map(|_| ())
}

#[test]
fn test_extract_strings() -> Result<()> {
let test_bytes = get_test_bytes()?;
let test_path = get_test_path();
let mut analyzer = Analyzer::new(
FileInfo::new(
test_path.to_str().expect("failed to get test path"),
test_bytes.as_slice(),
)?,
4,
vec![],
)?;
let (tx, rx) = mpsc::channel();
analyzer.extract_strings(tx);
if let Event::FileStrings(strings) = rx.recv()? {
assert!(strings?.iter().map(|(s, _)| s).any(|v| v == ".debug_str"));
} else {
panic!("strings did not succeed");
}
Ok(())
}
}
46 changes: 46 additions & 0 deletions tests/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use binsider::{app::Analyzer, error::Result, file::FileInfo, prelude::Event};
use std::{fs, path::PathBuf, sync::mpsc};

fn get_test_path() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_binsider"))
}

fn get_test_bytes() -> Result<Vec<u8>> {
let debug_binary = get_test_path();
Ok(fs::read(debug_binary)?)
}

#[test]
fn test_init() -> Result<()> {
Analyzer::new(
FileInfo::new(
get_test_path().to_str().expect("failed to get test path"),
get_test_bytes()?.as_slice(),
)?,
4,
vec![],
)
.map(|_| ())
}

#[test]
fn test_extract_strings() -> Result<()> {
let test_bytes = get_test_bytes()?;
let test_path = get_test_path();
let mut analyzer = Analyzer::new(
FileInfo::new(
test_path.to_str().expect("failed to get test path"),
test_bytes.as_slice(),
)?,
4,
vec![],
)?;
let (tx, rx) = mpsc::channel();
analyzer.extract_strings(tx);
if let Event::FileStrings(strings) = rx.recv()? {
assert!(strings?.iter().map(|(s, _)| s).any(|v| v == ".debug_str"));
} else {
panic!("strings did not succeed");
}
Ok(())
}

0 comments on commit efb73b1

Please sign in to comment.