From 61dbb5b7cea1b8d00ada896fbe3ee18f3906f468 Mon Sep 17 00:00:00 2001 From: kmg0308 <113580700+kmg0308@users.noreply.github.com> Date: Mon, 29 Jun 2026 19:40:36 +0900 Subject: [PATCH] Truncate export files when rewriting --- src/export/mod.rs | 5 +++-- src/export/tests.rs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/export/mod.rs b/src/export/mod.rs index 3947a2e50..9132b8e31 100644 --- a/src/export/mod.rs +++ b/src/export/mod.rs @@ -1,4 +1,4 @@ -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::Write; mod asciidoc; @@ -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}'")) } diff --git a/src/export/tests.rs b/src/export/tests.rs index bd7ea7963..f249ca7c4 100644 --- a/src/export/tests.rs +++ b/src/export/tests.rs @@ -1,4 +1,4 @@ -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; @@ -6,6 +6,18 @@ 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( results: &[BenchmarkResult], unit: Option,