Skip to content

Commit

Permalink
refactor: simplified stdout/stderr mocking in tests (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Dec 13, 2023
1 parent 13255b1 commit f83a556
Show file tree
Hide file tree
Showing 66 changed files with 354 additions and 443 deletions.
6 changes: 1 addition & 5 deletions .markdown-link-check.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{
"ignorePatterns": [
{
"pattern": "^https://crates.io"
}
]
"ignorePatterns": [{ "pattern": "^https://crates.io" }]
}
5 changes: 2 additions & 3 deletions src/cli/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::config::Config;
use crate::dirs;
use crate::env::RTX_EXE;
use crate::file::touch_dir;
use crate::output::Output;
use crate::shell::{get_shell, ShellType};

/// Initializes rtx in the current shell
Expand Down Expand Up @@ -46,15 +45,15 @@ pub struct Activate {
}

impl Activate {
pub fn run(self, _config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, _config: Config) -> Result<()> {
let shell = get_shell(self.shell_type.or(self.shell))
.expect("no shell provided, use `--shell=zsh`");

// touch ROOT to allow hook-env to run
let _ = touch_dir(&dirs::DATA);

let output = shell.activate(&RTX_EXE, self.status);
out.stdout.write(output);
rtxprint!("{output}");

Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions src/cli/alias/get.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use color_eyre::eyre::{eyre, Result};

use crate::config::Config;
use crate::output::Output;

/// Show an alias for a plugin
///
Expand All @@ -17,10 +16,10 @@ pub struct AliasGet {
}

impl AliasGet {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
match config.get_all_aliases().get(&self.plugin) {
Some(plugin) => match plugin.get(&self.alias) {
Some(alias) => Ok(rtxprintln!(out, "{}", alias)),
Some(alias) => Ok(rtxprintln!("{}", alias)),
None => Err(eyre!("Unknown alias: {}", &self.alias)),
},
None => Err(eyre!("Unknown plugin: {}", &self.plugin)),
Expand Down
8 changes: 4 additions & 4 deletions src/cli/alias/ls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre::Result;

use crate::config::Config;
use crate::output::Output;

use crate::plugins::PluginName;

/// List aliases
Expand All @@ -21,7 +21,7 @@ pub struct AliasLs {
}

impl AliasLs {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
for (plugin_name, aliases) in config.get_all_aliases() {
if let Some(plugin) = &self.plugin {
if plugin_name != plugin {
Expand All @@ -35,9 +35,9 @@ impl AliasLs {
continue;
}
if self.plugin.is_some() {
rtxprintln!(out, "{:20} {}", from, to);
rtxprintln!("{:20} {}", from, to);
} else {
rtxprintln!(out, "{:20} {:20} {}", plugin_name, from, to);
rtxprintln!("{:20} {:20} {}", plugin_name, from, to);
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/cli/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clap::Subcommand;
use color_eyre::eyre::Result;

use crate::config::Config;
use crate::output::Output;
use crate::plugins::PluginName;

mod get;
Expand Down Expand Up @@ -30,22 +29,22 @@ enum Commands {
}

impl Commands {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
match self {
Self::Get(cmd) => cmd.run(config, out),
Self::Ls(cmd) => cmd.run(config, out),
Self::Set(cmd) => cmd.run(config, out),
Self::Unset(cmd) => cmd.run(config, out),
Self::Get(cmd) => cmd.run(config),
Self::Ls(cmd) => cmd.run(config),
Self::Set(cmd) => cmd.run(config),
Self::Unset(cmd) => cmd.run(config),
}
}
}

impl Alias {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
let cmd = self.command.unwrap_or(Commands::Ls(ls::AliasLs {
plugin: self.plugin,
}));

cmd.run(config, out)
cmd.run(config)
}
}
3 changes: 1 addition & 2 deletions src/cli/alias/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use color_eyre::eyre::Result;

use crate::config::config_file::ConfigFile;
use crate::config::Config;
use crate::output::Output;

/// Add/update an alias for a plugin
///
Expand All @@ -19,7 +18,7 @@ pub struct AliasSet {
}

impl AliasSet {
pub fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
pub fn run(self, mut config: Config) -> Result<()> {
config
.global_config
.set_alias(&self.plugin, &self.alias, &self.value);
Expand Down
3 changes: 1 addition & 2 deletions src/cli/alias/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use color_eyre::eyre::Result;

use crate::config::config_file::ConfigFile;
use crate::config::Config;
use crate::output::Output;

/// Clears an alias for a plugin
///
Expand All @@ -17,7 +16,7 @@ pub struct AliasUnset {
}

impl AliasUnset {
pub fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
pub fn run(self, mut config: Config) -> Result<()> {
config.global_config.remove_alias(&self.plugin, &self.alias);
config.global_config.save()
}
Expand Down
22 changes: 11 additions & 11 deletions src/cli/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::Itertools;

use crate::cli::Cli;
use crate::config::Config;
use crate::output::Output;

use crate::toolset::ToolsetBuilder;

/// [internal] simulates asdf for plugins that call "asdf" internally
Expand All @@ -17,32 +17,32 @@ pub struct Asdf {
}

impl Asdf {
pub fn run(mut self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(mut self, config: Config) -> Result<()> {
let mut args = vec![String::from("rtx")];
args.append(&mut self.args);

match args.get(1).map(|s| s.as_str()) {
Some("reshim") => Cli::new().run(config, &args, out),
Some("list") => list_versions(config, out, &args),
Some("reshim") => Cli::new().run(config, &args),
Some("list") => list_versions(config, &args),
Some("install") => {
if args.len() == 4 {
let version = args.pop().unwrap();
args[2] = format!("{}@{}", args[2], version);
}
Cli::new().run(config, &args, out)
Cli::new().run(config, &args)
}
_ => Cli::new().run(config, &args, out),
_ => Cli::new().run(config, &args),
}
}
}

fn list_versions(config: Config, out: &mut Output, args: &Vec<String>) -> Result<()> {
fn list_versions(config: Config, args: &Vec<String>) -> Result<()> {
if args[2] == "all" {
let mut new_args: Vec<String> = vec!["rtx".into(), "ls-remote".into()];
if args.len() >= 3 {
new_args.push(args[3].clone());
}
return Cli::new().run(config, &new_args, out);
return Cli::new().run(config, &new_args);
}
let ts = ToolsetBuilder::new().build(&config)?;
let mut versions = ts.list_installed_versions(&config)?;
Expand All @@ -53,16 +53,16 @@ fn list_versions(config: Config, out: &mut Output, args: &Vec<String>) -> Result
if let Some(plugin) = plugin {
versions.retain(|(_, v)| v.plugin_name.as_str() == plugin);
for (_, version) in versions {
rtxprintln!(out, "{}", version.version);
rtxprintln!("{}", version.version);
}
} else {
for (plugin, versions) in &versions
.into_iter()
.group_by(|(_, v)| v.plugin_name.to_string())
{
rtxprintln!(out, "{}", plugin);
rtxprintln!("{}", plugin);
for (_, tv) in versions {
rtxprintln!(out, " {}", tv.version);
rtxprintln!(" {}", tv.version);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/bin_paths.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre::Result;

use crate::config::Config;
use crate::output::Output;

use crate::toolset::ToolsetBuilder;

/// List all the active runtime bin paths
Expand All @@ -10,10 +10,10 @@ use crate::toolset::ToolsetBuilder;
pub struct BinPaths {}

impl BinPaths {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;
for p in ts.list_paths(&config) {
rtxprintln!(out, "{}", p.display());
rtxprintln!("{}", p.display());
}
Ok(())
}
Expand Down
7 changes: 3 additions & 4 deletions src/cli/cache/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use color_eyre::eyre::Result;
use crate::config::Config;
use crate::dirs::CACHE;
use crate::file::{display_path, remove_all};
use crate::output::Output;

/// Deletes all cache files in rtx
#[derive(Debug, clap::Args)]
Expand All @@ -15,7 +14,7 @@ pub struct CacheClear {
}

impl CacheClear {
pub fn run(self, _config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, _config: Config) -> Result<()> {
let cache_dirs = match &self.plugin {
Some(plugins) => plugins.iter().map(|p| CACHE.join(p)).collect(),
None => vec![CACHE.to_path_buf()],
Expand All @@ -27,8 +26,8 @@ impl CacheClear {
}
}
match &self.plugin {
Some(plugins) => rtxstatusln!(out, "cache cleared for {}", plugins.join(", ")),
None => rtxstatusln!(out, "cache cleared"),
Some(plugins) => rtxstatusln!("cache cleared for {}", plugins.join(", ")),
None => rtxstatusln!("cache cleared"),
}
Ok(())
}
Expand Down
11 changes: 5 additions & 6 deletions src/cli/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use color_eyre::eyre::Result;

use crate::config::Config;
use crate::env;
use crate::output::Output;

mod clear;

Expand All @@ -23,20 +22,20 @@ enum Commands {
}

impl Commands {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
match self {
Self::Clear(cmd) => cmd.run(config, out),
Self::Clear(cmd) => cmd.run(config),
}
}
}

impl Cache {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config) -> Result<()> {
match self.command {
Some(cmd) => cmd.run(config, out),
Some(cmd) => cmd.run(config),
None => {
// just show the cache dir
rtxprintln!(out, "{}", env::RTX_CACHE_DIR.display());
rtxprintln!("{}", env::RTX_CACHE_DIR.display());
Ok(())
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/cli/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use color_eyre::eyre::Result;
use std::fmt::Display;

use crate::config::Config;
use crate::output::Output;

/// Generate shell completions
#[derive(Debug, clap::Args)]
Expand All @@ -20,13 +19,13 @@ pub struct Completion {
}

impl Completion {
pub fn run(self, _config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, _config: Config) -> Result<()> {
let c = match self.shell.or(self.shell_type).unwrap() {
Shell::Bash => include_str!("../../completions/rtx.bash"),
Shell::Fish => include_str!("../../completions/rtx.fish"),
Shell::Zsh => include_str!("../../completions/_rtx"),
};
rtxprintln!(out, "{}", c.trim());
rtxprintln!("{}", c.trim());

Ok(())
}
Expand Down
Loading

0 comments on commit f83a556

Please sign in to comment.