Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/uu/dircolors/src/dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub enum OutputFmt {
Unknown,
}

pub fn guess_syntax() -> OutputFmt {
match env::var("SHELL") {
Ok(ref s) if !s.is_empty() => {
pub fn guess_syntax(env: Option<OsString>) -> OutputFmt {
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn guess_syntax(env: Option<OsString>) -> OutputFmt {
pub fn guess_syntax<P: AsRef<Path>>(path: Option<P>) -> OutputFmt {

We can make this more type-safe and avoid .into() at the call sites.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then x.as_os_str().is_empty() until path_is_empty is stabilised.

match env {
Comment thread
oech3 marked this conversation as resolved.
Some(s) if !s.is_empty() => {
let shell_path: &Path = s.as_ref();
if let Some(name) = shell_path.file_name() {
if name == "csh" || name == "tcsh" {
Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};

if out_format == OutputFmt::Unknown {
match guess_syntax() {
match guess_syntax(env::var_os("SHELL")) {
OutputFmt::Unknown => {
return Err(USimpleError::new(
1,
Expand Down
48 changes: 8 additions & 40 deletions tests/by-util/test_dircolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,14 @@ fn test_invalid_arg() {

#[test]
fn test_shell_syntax() {
use std::env;
let last = env::var("SHELL");
unsafe {
env::set_var("SHELL", "/path/csh");
}
assert_eq!(OutputFmt::CShell, guess_syntax());
unsafe {
env::set_var("SHELL", "csh");
}
assert_eq!(OutputFmt::CShell, guess_syntax());
unsafe {
env::set_var("SHELL", "/path/bash");
}
assert_eq!(OutputFmt::Shell, guess_syntax());
unsafe {
env::set_var("SHELL", "bash");
}
assert_eq!(OutputFmt::Shell, guess_syntax());
unsafe {
env::set_var("SHELL", "/asd/bar");
}
assert_eq!(OutputFmt::Shell, guess_syntax());
unsafe {
env::set_var("SHELL", "foo");
}
assert_eq!(OutputFmt::Shell, guess_syntax());
unsafe {
env::set_var("SHELL", "");
}
assert_eq!(OutputFmt::Unknown, guess_syntax());
unsafe {
env::remove_var("SHELL");
}
assert_eq!(OutputFmt::Unknown, guess_syntax());

if let Ok(s) = last {
unsafe {
env::set_var("SHELL", s);
}
}
assert_eq!(OutputFmt::CShell, guess_syntax(Some("/path/csh".into())));
assert_eq!(OutputFmt::CShell, guess_syntax(Some("csh".into())));
assert_eq!(OutputFmt::Shell, guess_syntax(Some("/path/bash".into())));
assert_eq!(OutputFmt::Shell, guess_syntax(Some("bash".into())));
assert_eq!(OutputFmt::Shell, guess_syntax(Some("/asd/bar".into())));
assert_eq!(OutputFmt::Shell, guess_syntax(Some("foo".into())));
assert_eq!(OutputFmt::Unknown, guess_syntax(Some(String::new().into())));
assert_eq!(OutputFmt::Unknown, guess_syntax(None));
}

#[test]
Expand Down
Loading