Skip to content
Merged
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
15 changes: 15 additions & 0 deletions src/output/render/flags_bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ impl f::Flags {
Some(wrapper_flags_to_string(self.0))
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_zero_flags_render() {
let flags = f::Flags(0);
assert_eq!(flags.render_json(FlagsFormat::Short), Some("-".to_string()));
assert_eq!(flags.render_json(FlagsFormat::Long), Some("-".to_string()));

let cell = flags.render(Style::default(), FlagsFormat::Short);
assert_eq!(*cell.width, 1);
}
}
75 changes: 75 additions & 0 deletions src/output/render/flags_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,78 @@ impl f::Flags {
})
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_empty_flags() {
assert_eq!(flags_to_windows_string(0), "-");
assert_eq!(flags_to_bsd_string(0), "-");
}

#[test]
fn test_single_flags() {
assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_READONLY), "R");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_READONLY), "readonly");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_HIDDEN), "H");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_HIDDEN), "hidden");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_SYSTEM), "S");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_SYSTEM), "system");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_ARCHIVE), "A");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_ARCHIVE), "archive");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_TEMPORARY), "T");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_TEMPORARY), "temporary");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_COMPRESSED), "C");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_COMPRESSED), "compressed");

assert_eq!(flags_to_windows_string(FILE_ATTRIBUTE_ENCRYPTED), "E");
assert_eq!(flags_to_bsd_string(FILE_ATTRIBUTE_ENCRYPTED), "encrypted");
}

#[test]
fn test_multiple_flags() {
let flags = FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE;
assert_eq!(flags_to_windows_string(flags), "RHSA");
assert_eq!(flags_to_bsd_string(flags), "readonly-hidden-system-archive");
}

#[test]
fn test_all_flags() {
let mut all = 0u32;
for attr in &ATTRIBUTES {
all |= attr.flag;
}
assert_eq!(flags_to_windows_string(all), "RHSATCOIEXUPM");
assert_eq!(
flags_to_bsd_string(all),
"readonly-hidden-system-archive-temporary-compressed-offline-not indexed-encrypted-no scrub-unpinned-pinned-recall on data access"
);
}

#[test]
fn test_render_and_json() {
let flags = f::Flags(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_ARCHIVE);
assert_eq!(
flags.render_json(FlagsFormat::Short),
Some("RA".to_string())
);
assert_eq!(
flags.render_json(FlagsFormat::Long),
Some("readonly-archive".to_string())
);

let empty = f::Flags(0);
assert_eq!(empty.render_json(FlagsFormat::Short), Some("-".to_string()));
assert_eq!(empty.render_json(FlagsFormat::Long), Some("-".to_string()));
}
}
116 changes: 116 additions & 0 deletions src/output/render/permissions_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,119 @@ impl f::Attributes {
"-"
}
}

#[cfg(test)]
mod test {
use super::*;
use nu_ansi_term::Color::*;

struct TestColours;

#[rustfmt::skip]
impl Colours for TestColours {
fn dash(&self) -> Style { Fixed(11).normal() }
fn user_read(&self) -> Style { Fixed(101).normal() }
fn user_write(&self) -> Style { Fixed(102).normal() }
fn user_execute_file(&self) -> Style { Fixed(103).normal() }
fn user_execute_other(&self) -> Style { Fixed(113).normal() }
fn group_read(&self) -> Style { Fixed(104).normal() }
fn group_write(&self) -> Style { Fixed(105).normal() }
fn group_execute(&self) -> Style { Fixed(106).normal() }
fn other_read(&self) -> Style { Fixed(107).normal() }
fn other_write(&self) -> Style { Fixed(108).normal() }
fn other_execute(&self) -> Style { Fixed(109).normal() }
fn special_user_file(&self) -> Style { Fixed(110).normal() }
fn special_other(&self) -> Style { Fixed(111).normal() }
fn attribute(&self) -> Style { Fixed(112).normal() }
}

#[rustfmt::skip]
impl FiletypeColours for TestColours {
fn normal(&self) -> Style { Fixed(1).normal() }
fn directory(&self) -> Style { Fixed(2).bold() }
fn pipe(&self) -> Style { Fixed(3).normal() }
fn symlink(&self) -> crate::theme::LinkStyle {
crate::theme::LinkStyle::AnsiStyle(Fixed(4).normal())
}
fn block_device(&self) -> Style { Fixed(5).normal() }
fn char_device(&self) -> Style { Fixed(6).normal() }
fn socket(&self) -> Style { Fixed(7).normal() }
fn special(&self) -> Style { Fixed(8).normal() }
fn tag(&self, _tag: &f::TagColor) -> Style { Fixed(9).normal() }
}

#[test]
fn test_none_permissions_plus() {
let p: Option<f::PermissionsPlus> = None;
let cell = p.render(&TestColours);
assert_eq!(*cell.width, 0);
assert_eq!(p.render_json(), None);
}

#[test]
fn test_attributes_render_json_permutations() {
let empty_attr = f::Attributes {
archive: false,
readonly: false,
hidden: false,
system: false,
reparse_point: false,
directory: false,
};
assert_eq!(empty_attr.render_json(), vec!["-", "-", "-", "-"]);
assert_eq!(empty_attr.render_type_json(), "-");

let full_attr = f::Attributes {
archive: true,
readonly: true,
hidden: true,
system: true,
reparse_point: false,
directory: false,
};
assert_eq!(full_attr.render_json(), vec!["a", "r", "h", "s"]);

let dir_attr = f::Attributes {
archive: false,
readonly: true,
hidden: false,
system: false,
reparse_point: false,
directory: true,
};
assert_eq!(dir_attr.render_json(), vec!["-", "r", "-", "-"]);
assert_eq!(dir_attr.render_type_json(), "d");

let link_attr = f::Attributes {
archive: false,
readonly: false,
hidden: false,
system: false,
reparse_point: true,
directory: false,
};
assert_eq!(link_attr.render_type_json(), "l");
}

#[test]
fn test_permissions_plus_render_combined() {
let attr = f::Attributes {
archive: true,
readonly: false,
hidden: true,
system: false,
reparse_point: false,
directory: true,
};
let p = Some(f::PermissionsPlus {
file_type: f::Type::Directory,
attributes: attr,
xattrs: false,
mount: false,
});

let cell = p.render(&TestColours);
assert_eq!(*cell.width, 5);
assert_eq!(p.render_json(), Some("da-h-".to_string()));
}
}
82 changes: 82 additions & 0 deletions src/output/render/securityctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,85 @@ pub trait Colours {
fn selinux_type(&self) -> Style;
fn selinux_range(&self) -> Style;
}

#[cfg(test)]
mod test {
use super::*;
use nu_ansi_term::Color;

struct TestColours;

impl Colours for TestColours {
fn none(&self) -> Style {
Color::DarkGray.normal()
}
fn selinux_colon(&self) -> Style {
Color::White.normal()
}
fn selinux_user(&self) -> Style {
Color::Red.bold()
}
fn selinux_role(&self) -> Style {
Color::Green.normal()
}
fn selinux_type(&self) -> Style {
Color::Blue.normal()
}
fn selinux_range(&self) -> Style {
Color::Yellow.normal()
}
}

#[test]
fn test_none_security_context() {
let colours = TestColours;
let ctx = f::SecurityContext {
context: f::SecurityContextType::None,
};

let cell = ctx.render(&colours);
assert_eq!(*cell.width, 1);
assert_eq!(ctx.render_json(), None);
}

#[test]
fn test_selinux_standard_four_part_context() {
let colours = TestColours;
let raw = "unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023";
let ctx = f::SecurityContext {
context: f::SecurityContextType::SELinux(raw),
};

let cell = ctx.render(&colours);
assert_eq!(*cell.width, raw.len());

let json = ctx.render_json();
assert_eq!(json, Some(raw.to_string()));
}

#[test]
fn test_selinux_three_part_context() {
let colours = TestColours;
let raw = "system_u:object_r:default_t";
let ctx = f::SecurityContext {
context: f::SecurityContextType::SELinux(raw),
};

let cell = ctx.render(&colours);
assert_eq!(*cell.width, raw.len());
assert_eq!(ctx.render_json(), Some(raw.to_string()));
}

#[test]
fn test_selinux_single_part_context() {
let colours = TestColours;
let raw = "unlabeled";
let ctx = f::SecurityContext {
context: f::SecurityContextType::SELinux(raw),
};

let cell = ctx.render(&colours);
assert_eq!(*cell.width, raw.len());
assert_eq!(ctx.render_json(), Some(raw.to_string()));
}
}
Loading
Loading