Skip to content

Commit

Permalink
fix: Switch to fs_err crate for better file error messages (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek818 authored May 19, 2024
1 parent 16d70df commit f23a8be
Show file tree
Hide file tree
Showing 34 changed files with 163 additions and 141 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ tracing-subscriber = { version = "0.3", default-features = false, optional = tru
[target.'cfg(not(windows))'.dependencies]
openssl = { version = "0.10", features = ["vendored"] }

fs-err = { version = "2.11.0" }

[dev-dependencies]
trim-margin = "0.1.0"
buildkite-test-collector = "0.1.1"
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ mod tests {
let fixtures_root = get_fixtures_root(env!("CARGO_MANIFEST_DIR"))?;
let pattern_grit = fixtures_root.join("stdlib").join("raw_no_console_log.grit");
let pattern_dest = tempdir.path().join("raw_no_console_log.grit");
std::fs::copy(pattern_grit, pattern_dest.clone())?;
fs_err::copy(pattern_grit, pattern_dest.clone())?;
let input = fixtures_root.join("stdlib").join("simple.js");
let input_dest = tempdir.path().join("simple.js");
std::fs::copy(input, &input_dest)?;
fs_err::copy(input, &input_dest)?;
env::set_current_dir(tempdir.path())?;
test_apply(
"raw_no_console_log.grit".to_string(),
vec![input_dest.clone()],
)
.await?;
let content = std::fs::read_to_string(&input_dest)?;
let content = fs_err::read_to_string(&input_dest)?;
assert_eq!(content, "\n".to_owned());
Ok(())
}
Expand All @@ -176,10 +176,10 @@ mod tests {
let fixtures_root = get_fixtures_root(env!("CARGO_MANIFEST_DIR"))?;
let pattern_grit = fixtures_root.join("openai").join("pattern.grit");
let pattern_dest = tempdir.path().join("pattern.grit");
std::fs::copy(pattern_grit, pattern_dest.clone())?;
fs_err::copy(pattern_grit, pattern_dest.clone())?;
let input = fixtures_root.join("openai").join("input.py");
let input_dest = tempdir.path().join("input.py");
std::fs::copy(input, &input_dest)?;
fs_err::copy(input, &input_dest)?;
env::set_current_dir(tempdir.path())?;
test_apply("pattern.grit".to_string(), vec![input_dest.clone()]).await?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ pub(crate) async fn run_check(
.collect::<HashSet<_>>();
for pattern in applicable_patterns {
let problem = compiled_map.get(pattern).unwrap();
let src = std::fs::read_to_string(file)?;
let src = fs_err::read_to_string(file)?;
let res = problem.execute_file(&RichFile::new(file.to_string(), src), &context);
for r in res {
if let MatchResult::Rewrite(r) = r {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/docgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) async fn run_docgen(arg: DocGenArgs) -> Result<()> {
hide_footer: true,
});

std::fs::write(arg.outpath, output)?;
fs_err::write(arg.outpath, output)?;

Ok(())
}
2 changes: 1 addition & 1 deletion crates/cli/src/commands/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(crate) async fn run_patterns_edit(arg: PatternsEditArgs) -> Result<()> {
let (_, repo) = resolve_from_cwd(&resolver::Source::All).await?;
let _pattern = collect_from_file(&arg.path, &Some(repo)).await?;

let content = std::fs::read_to_string(&arg.path)?;
let content = fs_err::read_to_string(&arg.path)?;
let payload = serde_json::to_value(OpenStudioSettings {
content,
path: arg.path.to_string_lossy().to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub enum PlumbingArgs {

fn read_input(shared_args: &SharedPlumbingArgs) -> Result<String> {
let buffer = if let Some(input) = &shared_args.input {
std::fs::read_to_string(input)?
fs_err::read_to_string(input)?
} else {
let mut buffer = String::new();
stdin().read_to_string(&mut buffer)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use log::info;
use marzano_core::{api::EnforcementLevel, fs::extract_ranges};
use marzano_gritmodule::config::ResolvedGritDefinition;
use marzano_gritmodule::utils::extract_path;
use std::fs::OpenOptions;
use fs_err::OpenOptions;
use std::io::prelude::*;

fn format_level(level: &EnforcementLevel) -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/messenger_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub async fn create_emitter<'a>(
_root_path: Option<&PathBuf>,
) -> Result<MessengerVariant<'a>> {
let writer: Option<Box<dyn Write + Send>> = if let Some(output_file) = output_file {
let file = std::fs::File::create(output_file)?;
let file = fs_err::File::create(output_file)?;
let bufwriter = io::BufWriter::new(file);
Some(Box::new(bufwriter))
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use marzano_core::api::{
use marzano_core::constants::DEFAULT_FILE_NAME;
use marzano_messenger::output_mode::OutputMode;
use std::fmt::Display;
use std::fs::read_to_string;
use fs_err::read_to_string;
use std::{
io::Write,
sync::{Arc, Mutex},
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ impl Updater {
{
use std::os::unix::fs::PermissionsExt;

let target_file = std::fs::File::open(&target_path)?;
let target_file = fs_err::File::open(&target_path)?;
let mut perms = target_file.metadata()?.permissions();
perms.set_mode(0o744);
if let Err(e) = target_file.set_permissions(perms) {
Expand Down Expand Up @@ -832,7 +832,7 @@ fn release_details_relative_url(release: &str) -> String {

#[cfg(test)]
mod tests {
use std::fs::create_dir_all;
use fs_err::create_dir_all;

use anyhow::Result;
use chrono::NaiveDate;
Expand Down
1 change: 1 addition & 0 deletions crates/cli_bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ marzano-cli = { path = "../cli", default-features = false }
marzano-util = { path = "../util", default-features = false }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json", "stream"] }
fs-err = { version = "2.11.0" }

tracing = { version = "0.1.40", default-features = false }
opentelemetry-otlp = { version = "0.14.0", optional = true, features = [
Expand Down
Loading

0 comments on commit f23a8be

Please sign in to comment.