polkadot_subsystem_bench/utils.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17//! Test utils
18
19use std::{fs::File, io::Write};
20
21// Saves a given string to a file
22pub fn save_to_file(path: &str, value: String) -> color_eyre::eyre::Result<()> {
23 let output = std::process::Command::new(env!("CARGO"))
24 .arg("locate-project")
25 .arg("--workspace")
26 .arg("--message-format=plain")
27 .output()
28 .unwrap()
29 .stdout;
30 let workspace_dir = std::path::Path::new(std::str::from_utf8(&output).unwrap().trim())
31 .parent()
32 .unwrap();
33 let path = workspace_dir.join(path);
34 if let Some(dir) = path.parent() {
35 std::fs::create_dir_all(dir)?;
36 }
37 let mut file = File::create(path)?;
38 file.write_all(value.as_bytes())?;
39
40 Ok(())
41}