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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Because it is a FAQ, the major differences between this project and uutils are:
- [x] bc
- [x] cksum
- [x] cmp
- [x] comm
- [x] compress (compress cat.)
- [x] cp
- [x] csplit
Expand Down Expand Up @@ -129,7 +130,6 @@ Because it is a FAQ, the major differences between this project and uutils are:

- [x] asa
- [x] cal
- [x] comm
- [x] df
- [x] du
- [x] echo
Expand Down
14 changes: 5 additions & 9 deletions text/src/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let mask = args_mask(&args);

const MASK2: u32 = NO1 | NO2;
let lead_dup = {
if (mask & MASK2) == 0 {
"\t\t"
} else if (mask & MASK2) == MASK2 {
""
} else {
"\t"
}
let lead_dup = match (args.no1, args.no2) {
(false, false) => "\t\t",
(false, true) => "\t",
(true, false) => "\t",
(true, true) => "",
};

let mut exit_code = 0;
Expand Down
67 changes: 67 additions & 0 deletions text/tests/comm-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//

use plib::{run_test, TestPlan};
use std::fs;
use std::path::PathBuf;

fn get_test_file_path(filename: &str) -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push("comm");
path.push(filename);
path
}

fn run_comm_test(args: Vec<&str>, expected_output_filename: &str) {
let expected_output = fs::read_to_string(get_test_file_path(expected_output_filename))
.expect("Failed to read expected output file");

let args: Vec<String> = args.iter().map(|&s| s.into()).collect();

run_test(TestPlan {
cmd: String::from("comm"),
args,
expected_out: expected_output,
expected_err: String::new(),
stdin_data: String::new(),
expected_exit_code: 0,
});
}
#[test]
fn comm_basic_functionality() {
run_comm_test(
vec!["tests/comm/comm.file1", "tests/comm/comm.file2"],
"comm.0",
);
}

#[test]
fn comm_suppress_first_column() {
run_comm_test(
vec!["-1", "tests/comm/comm.file1", "tests/comm/comm.file2"],
"comm.1",
);
}

#[test]
fn comm_suppress_second_column() {
run_comm_test(
vec!["-2", "tests/comm/comm.file1", "tests/comm/comm.file2"],
"comm.2",
);
}

#[test]
fn comm_suppress_third_column() {
run_comm_test(
vec!["-3", "tests/comm/comm.file1", "tests/comm/comm.file2"],
"comm.3",
);
}
4 changes: 4 additions & 0 deletions text/tests/comm/comm.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apple
banana
carrot
date
3 changes: 3 additions & 0 deletions text/tests/comm/comm.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
banana
carrot
date
3 changes: 3 additions & 0 deletions text/tests/comm/comm.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apple
banana
carrot
2 changes: 2 additions & 0 deletions text/tests/comm/comm.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apple
date
3 changes: 3 additions & 0 deletions text/tests/comm/comm.file1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apple
banana
carrot
3 changes: 3 additions & 0 deletions text/tests/comm/comm.file2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
banana
carrot
date
228 changes: 228 additions & 0 deletions text/tests/csplit-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//
// Copyright (c) 2024 Jeff Garzik
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//

use plib::{run_test, TestPlan};

fn csplit_test(args: &[&str], test_data: &str, expected_output: &str) {
let str_args: Vec<String> = args.iter().map(|s| String::from(*s)).collect();

run_test(TestPlan {
cmd: String::from("csplit"),
args: str_args,
stdin_data: String::from(test_data),
expected_out: String::from(expected_output),
expected_err: String::from(""),
expected_exit_code: 0,
});
}

#[test]
fn test_csplit_text_by_lines() {
csplit_test(
&["-f", "text", "-", "5", "{3}"],
"1sdfghnm
2sadsgdhjmf
3zcxbncvm vbm
4asdbncv
5adsbfdgfnfm
6sdfcvncbmcg
7zsdgdgfndcgmncg
8asdbsfdndcgmn
9sfbdxgfndcgmncgmn
10dvsd
11
12
13
14
15
16
17",
"43\n\n57\n\n31\n\n14\n\n",
);
std::fs::remove_file("text00").unwrap();
std::fs::remove_file("text01").unwrap();
std::fs::remove_file("text02").unwrap();
std::fs::remove_file("text03").unwrap();
}

#[test]
fn test_csplit_text_by_lines_from_file() {
csplit_test(
&["-f", "text_f", "tests/assets/test_file.txt", "5", "{3}"],
"",
"43\n\n57\n\n31\n\n14\n\n",
);
std::fs::remove_file("text_f00").unwrap();
std::fs::remove_file("text_f01").unwrap();
std::fs::remove_file("text_f02").unwrap();
std::fs::remove_file("text_f03").unwrap();
}

#[test]
fn test_csplit_c_code_by_regex() {
csplit_test(
&[
"-f",
"code_c",
"tests/assets/test_file_c",
r"%main\(%",
"/^}/+1",
"{3}",
],
"",
"59\n\n53\n\n53\n\n54\n\n",
);
std::fs::remove_file("code_c00").unwrap();
std::fs::remove_file("code_c01").unwrap();
std::fs::remove_file("code_c02").unwrap();
std::fs::remove_file("code_c03").unwrap();
}

#[test]
fn test_csplit_c_code_by_regex_negative_offset() {
csplit_test(
&[
"-f",
"code_c_neg",
"tests/assets/test_file_c",
r"%main\(%",
"/^}/-2",
"{3}",
],
"",
"12\n\n46\n\n52\n\n107\n\n",
);
std::fs::remove_file("code_c_neg00").unwrap();
std::fs::remove_file("code_c_neg01").unwrap();
std::fs::remove_file("code_c_neg02").unwrap();
std::fs::remove_file("code_c_neg03").unwrap();
}

#[test]
fn test_csplit_c_code_by_regex_suppress() {
csplit_test(
&[
"-s",
"-f",
"code_c_s",
"tests/assets/test_file_c",
r"%main\(%",
"/^}/+1",
"{3}",
],
"",
"",
);
std::fs::remove_file("code_c_s00").unwrap();
std::fs::remove_file("code_c_s01").unwrap();
std::fs::remove_file("code_c_s02").unwrap();
std::fs::remove_file("code_c_s03").unwrap();
}

#[test]
fn test_csplit_c_code_by_regex_with_number() {
csplit_test(
&[
"-f",
"code_c_n",
"-n",
"3",
"tests/assets/test_file_c",
r"%main\(%",
"/^}/+1",
"{3}",
],
"",
"59\n\n53\n\n53\n\n54\n\n",
);
std::fs::remove_file("code_c_n000").unwrap();
std::fs::remove_file("code_c_n001").unwrap();
std::fs::remove_file("code_c_n002").unwrap();
std::fs::remove_file("code_c_n003").unwrap();
}

#[test]
fn test_csplit_regex_by_empty_lines() {
csplit_test(
&["-f", "empty_lines", "tests/assets/empty_line.txt", "/^$/"],
"",
"6\n\n7\n\n",
);
std::fs::remove_file("empty_lines00").unwrap();
std::fs::remove_file("empty_lines01").unwrap();
}

#[test]
fn test_csplit_regex_would_infloop() {
csplit_test(
&[
"-f",
"would_infloop",
"tests/assets/would_infloop.txt",
"/a/-1",
"{*}",
],
"",
"2\n\n",
);
std::fs::remove_file("would_infloop00").unwrap();
}

#[test]
fn test_csplit_regex_in_uniq() {
csplit_test(
&["-f", "in_uniq", "tests/assets/in_uniq", "/^$/", "{*}"],
"",
"6\n\n10\n\n8\n\n9\n\n",
);
std::fs::remove_file("in_uniq00").unwrap();
std::fs::remove_file("in_uniq01").unwrap();
std::fs::remove_file("in_uniq02").unwrap();
std::fs::remove_file("in_uniq03").unwrap();
}

#[test]
fn test_csplit_regex_in_uniq_2() {
csplit_test(
&["-f", "in_uniq_2_", "tests/assets/in_uniq", "/^$/-1", "{*}"],
"",
"3\n\n10\n\n8\n\n12\n\n",
);
std::fs::remove_file("in_uniq_2_00").unwrap();
std::fs::remove_file("in_uniq_2_01").unwrap();
std::fs::remove_file("in_uniq_2_02").unwrap();
std::fs::remove_file("in_uniq_2_03").unwrap();
}

#[test]
fn test_csplit_regex_in_uniq_3() {
csplit_test(
&["-f", "in_uniq_3_", "tests/assets/in_uniq", "/^$/1", "{*}"],
"",
"7\n\n10\n\n8\n\n8\n\n",
);
std::fs::remove_file("in_uniq_3_00").unwrap();
std::fs::remove_file("in_uniq_3_01").unwrap();
std::fs::remove_file("in_uniq_3_02").unwrap();
std::fs::remove_file("in_uniq_3_03").unwrap();
}

#[test]
fn test_csplit_regex_in_seq() {
csplit_test(
&["-f", "in_seq", "tests/assets/in_seq", "/2/", "/4/", "/6/"],
"",
"1\n\n3\n\n3\n\n1\n\n",
);
std::fs::remove_file("in_seq00").unwrap();
std::fs::remove_file("in_seq01").unwrap();
std::fs::remove_file("in_seq02").unwrap();
std::fs::remove_file("in_seq03").unwrap();
}
Loading