#![cfg(unix)]
use std::process::Command;
use tempfile::NamedTempFile;
const FSTOOL: &str = env!("CARGO_BIN_EXE_fstool");
fn which(tool: &str) -> bool {
Command::new(tool)
.arg("--version")
.output()
.ok()
.is_some_and(|o| o.status.success())
}
#[test]
fn repack_into_tar_gz_then_inspect() {
if !which("mke2fs") {
eprintln!("skipping: mke2fs not installed");
return;
}
let srcdir = tempfile::tempdir().unwrap();
std::fs::write(srcdir.path().join("hello.txt"), b"hello compressed\n").unwrap();
std::fs::create_dir(srcdir.path().join("sub")).unwrap();
std::fs::write(srcdir.path().join("sub/nested.txt"), b"nested body\n").unwrap();
let src_img = NamedTempFile::new().unwrap();
let out = Command::new(FSTOOL)
.args([
"ext-build",
"--kind",
"ext2",
srcdir.path().to_str().unwrap(),
"-o",
src_img.path().to_str().unwrap(),
])
.output()
.unwrap();
assert!(
out.status.success(),
"ext-build failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let tarball = tempfile::Builder::new()
.suffix(".tar.gz")
.tempfile()
.unwrap();
let out = Command::new(FSTOOL)
.args([
"repack",
src_img.path().to_str().unwrap(),
tarball.path().to_str().unwrap(),
])
.output()
.unwrap();
assert!(
out.status.success(),
"repack ext2 → tar.gz failed:\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
let magic = std::fs::read(tarball.path()).unwrap();
assert!(
magic.len() >= 2 && magic[0] == 0x1f && magic[1] == 0x8b,
"expected gzip magic at start of {}",
tarball.path().display()
);
let out = Command::new(FSTOOL)
.args(["ls", tarball.path().to_str().unwrap(), "/"])
.output()
.unwrap();
assert!(
out.status.success(),
"fstool ls on .tar.gz failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let listing = String::from_utf8_lossy(&out.stdout);
assert!(
listing.contains("hello.txt"),
"expected hello.txt in listing:\n{listing}"
);
let out = Command::new(FSTOOL)
.args(["cat", tarball.path().to_str().unwrap(), "/hello.txt"])
.output()
.unwrap();
assert!(out.status.success());
assert_eq!(out.stdout, b"hello compressed\n");
}