Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/export/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::{File, OpenOptions};
use std::fs::File;
use std::io::Write;

mod asciidoc;
Expand Down Expand Up @@ -156,7 +156,8 @@ impl ExportManager {

/// Write the given content to a file with the specified name
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
let mut file = OpenOptions::new().write(true).open(filename)?;
let mut file = File::create(filename)
.with_context(|| format!("Could not create export file '{filename}'"))?;
file.write_all(content)
.with_context(|| format!("Failed to export results to '{filename}'"))
}
14 changes: 13 additions & 1 deletion src/export/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use super::Exporter;
use super::{write_to_file, Exporter};
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::export::asciidoc::AsciidocExporter;
use crate::export::orgmode::OrgmodeExporter;
use crate::util::units::Unit;
use crate::{export::markdown::MarkdownExporter, options::SortOrder};
use std::collections::BTreeMap;

#[test]
fn write_to_file_truncates_existing_content() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("results.md");
let filename = path.to_str().unwrap();

write_to_file(filename, b"long benchmark export").unwrap();
write_to_file(filename, b"short").unwrap();

assert_eq!(std::fs::read_to_string(path).unwrap(), "short");
}

fn get_output<E: Exporter + Default>(
results: &[BenchmarkResult],
unit: Option<Unit>,
Expand Down