Skip to content

Commit

Permalink
style: Improve includes
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Oct 27, 2023
1 parent 6471cc1 commit 3cbddaa
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 45 deletions.
20 changes: 12 additions & 8 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use directories::BaseDirs;
use log::info;
#[cfg(windows)]
use log::warn;
#[cfg(windows)]
use std::env;
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
Expand Down Expand Up @@ -58,7 +57,7 @@ pub fn get_export_file(export_file: Option<PathBuf>) -> Result<PathBuf, Error> {
if export_file.is_absolute() {
Ok(export_file)
} else {
let current_dir = std::env::current_dir()?;
let current_dir = env::current_dir()?;
Ok(current_dir.join(export_file))
}
} else {
Expand Down Expand Up @@ -113,7 +112,12 @@ pub fn export_environment(export_file: &Path) -> Result<(), Error> {
mod tests {
use crate::env::{create_export_file, get_export_file, DEFAULT_EXPORT_FILE};
use directories::BaseDirs;
use std::{env::current_dir, path::PathBuf};
use std::{
env::current_dir,
fs::{create_dir_all, read_to_string},
path::PathBuf,
};
use tempfile::TempDir;

#[test]
#[allow(unused_variables)]
Expand Down Expand Up @@ -142,20 +146,20 @@ mod tests {
#[test]
fn test_create_export_file() {
// Creates the export file and writes the correct content to it
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let export_file = temp_dir.path().join("export.sh");
let exports = vec![
"export VAR1=value1".to_string(),
"export VAR2=value2".to_string(),
];
create_export_file(&export_file, &exports).unwrap();
let contents = std::fs::read_to_string(export_file).unwrap();
let contents = read_to_string(export_file).unwrap();
assert_eq!(contents, "export VAR1=value1\nexport VAR2=value2\n");

// Returns the correct error when it fails to create the export file (it already exists)
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let export_file = temp_dir.path().join("export.sh");
std::fs::create_dir_all(&export_file).unwrap();
create_dir_all(&export_file).unwrap();
let exports = vec![
"export VAR1=value1".to_string(),
"export VAR2=value2".to_string(),
Expand Down
23 changes: 9 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ use clap::{CommandFactory, Parser};
use espup::env::set_environment_variable;
use espup::{
cli::{CompletionsOpts, InstallOpts, UninstallOpts},
error::Error,
logging::initialize_logger,
toolchain::{
gcc::uninstall_gcc_toolchains, install as toolchain_install, llvm::Llvm,
rust::get_rustup_home, InstallMode,
gcc::uninstall_gcc_toolchains,
install as toolchain_install,
llvm::Llvm,
rust::{get_rustup_home, XtensaRust},
InstallMode,
},
update::check_for_update,
};
use log::info;
use miette::Result;
use std::env;
use tokio::fs::remove_dir_all;
use std::{env, io::stdout};

#[derive(Parser)]
#[command(about, version)]
Expand Down Expand Up @@ -43,12 +44,7 @@ async fn completions(args: CompletionsOpts) -> Result<()> {

info!("Generating completions for {} shell", args.shell);

clap_complete::generate(
args.shell,
&mut Cli::command(),
"espup",
&mut std::io::stdout(),
);
clap_complete::generate(args.shell, &mut Cli::command(), "espup", &mut stdout());

info!("Completions successfully generated!");

Expand Down Expand Up @@ -80,9 +76,8 @@ async fn uninstall(args: UninstallOpts) -> Result<()> {
"Deleting the Xtensa Rust toolchain located in '{}'",
&toolchain_dir.display()
);
remove_dir_all(&toolchain_dir)
.await
.map_err(|_| Error::RemoveDirectory(toolchain_dir.display().to_string()))?;

XtensaRust::uninstall(&toolchain_dir).await?;

#[cfg(windows)]
set_environment_variable("PATH", &env::var("PATH").unwrap())?;
Expand Down
10 changes: 6 additions & 4 deletions src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{
use async_trait::async_trait;
use log::{debug, info, warn};
use miette::Result;
#[cfg(windows)]
use std::env;
use std::path::{Path, PathBuf};
use tokio::fs::remove_dir_all;

Expand Down Expand Up @@ -85,9 +87,9 @@ impl Installable for Gcc {
"$Env:PATH = \"{};\" + $Env:PATH",
&self.get_bin_path()
));
std::env::set_var(
env::set_var(
"PATH",
self.get_bin_path().replace('/', "\\") + ";" + &std::env::var("PATH").unwrap(),
self.get_bin_path().replace('/', "\\") + ";" + &env::var("PATH").unwrap(),
);
}
#[cfg(unix)]
Expand Down Expand Up @@ -139,9 +141,9 @@ pub async fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error
DEFAULT_GCC_RELEASE,
toolchain
);
std::env::set_var(
env::set_var(
"PATH",
std::env::var("PATH")
env::var("PATH")
.unwrap()
.replace(&format!("{gcc_path};"), ""),
);
Expand Down
8 changes: 5 additions & 3 deletions src/toolchain/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use async_trait::async_trait;
use log::{info, warn};
use miette::Result;
use regex::Regex;
#[cfg(windows)]
use std::env;
use std::path::{Path, PathBuf};
use tokio::fs::remove_dir_all;

Expand Down Expand Up @@ -123,7 +125,7 @@ impl Llvm {
if cfg!(windows) {
delete_environment_variable("LIBCLANG_PATH")?;
delete_environment_variable("CLANG_PATH")?;
let mut updated_path = std::env::var("PATH").unwrap().replace(
let mut updated_path = env::var("PATH").unwrap().replace(
&format!(
"{}\\{}\\esp-clang\\bin;",
llvm_path.display().to_string().replace('/', "\\"),
Expand Down Expand Up @@ -186,9 +188,9 @@ impl Installable for Llvm {
&format!("{}\\libclang.dll", self.get_lib_path().replace('/', "\\")),
)?;

std::env::set_var(
env::set_var(
"PATH",
self.get_lib_path().replace('/', "\\") + ";" + &std::env::var("PATH").unwrap(),
self.get_lib_path().replace('/', "\\") + ";" + &env::var("PATH").unwrap(),
);
}
#[cfg(unix)]
Expand Down
4 changes: 2 additions & 2 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use retry::{delay::Fixed, retry};
use std::{
env,
fs::{create_dir_all, remove_file, File},
io::Write,
io::{copy, Write},
path::{Path, PathBuf},
};
use tar::Archive;
Expand Down Expand Up @@ -93,7 +93,7 @@ pub async fn download_file(
} else {
create_dir_all(outpath.parent().unwrap())?;
let mut outfile = File::create(&outpath)?;
std::io::copy(&mut file, &mut outfile)?;
copy(&mut file, &mut outfile)?;
}
}
} else {
Expand Down
32 changes: 18 additions & 14 deletions src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use std::fs::create_dir_all;
use std::{
env,
fmt::Debug,
fs::{read_dir, remove_dir_all, remove_file},
fs::read_dir,
io,
path::{Path, PathBuf},
process::{Command, Stdio},
};
#[cfg(unix)]
use tempfile::tempdir_in;
use tokio::fs::{remove_dir_all, remove_file};

/// Xtensa Rust Toolchain repository
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
Expand Down Expand Up @@ -157,7 +159,7 @@ impl XtensaRust {
}

/// Removes the Xtensa Rust toolchain.
pub fn uninstall(toolchain_path: &Path) -> Result<(), Error> {
pub async fn uninstall(toolchain_path: &Path) -> Result<(), Error> {
info!("Uninstalling Xtensa Rust toolchain");
let dir = read_dir(toolchain_path)?;
for entry in dir {
Expand All @@ -169,10 +171,10 @@ impl XtensaRust {
{
if entry_path.is_dir() {
remove_dir_all(Path::new(&entry_name))
.await
.map_err(|_| Error::RemoveDirectory(entry_name))?;
} else {
// If the entry is a file, delete the file
remove_file(&entry_name)?;
remove_file(&entry_name).await?;
}
}
}
Expand Down Expand Up @@ -208,7 +210,7 @@ impl Installable for XtensaRust {
if !rustc_version.status.success() {
warn!("Failed to detect version of Xtensa Rust, reinstalling it");
}
Self::uninstall(&self.toolchain_destination)?;
Self::uninstall(&self.toolchain_destination).await?;
}
}

Expand Down Expand Up @@ -254,7 +256,7 @@ impl Installable for XtensaRust {
.status
.success()
{
Self::uninstall(&self.toolchain_destination)?;
Self::uninstall(&self.toolchain_destination).await?;
return Err(Error::XtensaRust);
}

Expand All @@ -281,7 +283,7 @@ impl Installable for XtensaRust {
.status
.success()
{
Self::uninstall(&self.toolchain_destination)?;
Self::uninstall(&self.toolchain_destination).await?;
return Err(Error::XtensaRustSrc);
}
}
Expand Down Expand Up @@ -421,7 +423,7 @@ pub async fn check_rust_installation() -> Result<(), Error> {
.stdout(Stdio::piped())
.output()
{
if let std::io::ErrorKind::NotFound = e.kind() {
if let io::ErrorKind::NotFound = e.kind() {
return Err(Error::MissingRust);
} else {
return Err(Error::RustupDetection(e.to_string()));
Expand All @@ -438,6 +440,8 @@ mod tests {
toolchain::rust::{get_cargo_home, get_rustup_home, XtensaRust},
};
use directories::BaseDirs;
use std::env;
use tempfile::TempDir;

#[test]
fn test_xtensa_rust_parse_version() {
Expand All @@ -459,30 +463,30 @@ mod tests {
#[test]
fn test_get_cargo_home() {
// No CARGO_HOME set
std::env::remove_var("CARGO_HOME");
env::remove_var("CARGO_HOME");
assert_eq!(
get_cargo_home(),
BaseDirs::new().unwrap().home_dir().join(".cargo")
);
// CARGO_HOME set
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let cargo_home = temp_dir.path().to_path_buf();
std::env::set_var("CARGO_HOME", cargo_home.to_str().unwrap());
env::set_var("CARGO_HOME", cargo_home.to_str().unwrap());
assert_eq!(get_cargo_home(), cargo_home);
}

#[test]
fn test_get_rustup_home() {
// No RUSTUP_HOME set
std::env::remove_var("RUSTUP_HOME");
env::remove_var("RUSTUP_HOME");
assert_eq!(
get_rustup_home(),
BaseDirs::new().unwrap().home_dir().join(".rustup")
);
// RUSTUP_HOME set
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let rustup_home = temp_dir.path().to_path_buf();
std::env::set_var("RUSTUP_HOME", rustup_home.to_str().unwrap());
env::set_var("RUSTUP_HOME", rustup_home.to_str().unwrap());
assert_eq!(get_rustup_home(), rustup_home);
}
}

0 comments on commit 3cbddaa

Please sign in to comment.