Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cksum: stops when one of given files doesn't exist #5809 #5820

Merged
merged 13 commits into from
Jan 15, 2024
Merged

cksum: stops when one of given files doesn't exist #5809 #5820

merged 13 commits into from
Jan 15, 2024

Conversation

biplab5464
Copy link
Contributor

continue if the file not found

@cakebaker cakebaker linked an issue Jan 10, 2024 that may be closed by this pull request
@uutils uutils deleted a comment from github-actions bot Jan 10, 2024
@uutils uutils deleted a comment from github-actions bot Jan 10, 2024
@uutils uutils deleted a comment from github-actions bot Jan 10, 2024
@cakebaker
Copy link
Contributor

I don't know if you have seen that one tests fails:

run: /home/runner/work/coreutils/coreutils/target/debug/coreutils cksum asdf
test test_cksum::test_nonexisting_file ... FAILED

@biplab5464
Copy link
Contributor Author

Thank you for pointing out

@biplab5464
Copy link
Contributor Author

image

now i am correctly printing the error still the test case is failing

image

any idea what to do

@sylvestre
Copy link
Sponsor Contributor

@biplab5464 please replace the screenshot by text. Screenshots are terrible for accessibility and search

@biplab5464
Copy link
Contributor Author

**TEST CASE **
Executing task: /home/biplab/.cargo/bin/cargo test --package coreutils --test tests -- test_cksum::test_nonexisting_file --exact --nocapture

Compiling uu_cksum v0.0.23 (/home/biplab/project/coreutils/src/uu/cksum)
Compiling coreutils v0.0.23 (/home/biplab/project/coreutils)
Finished test [unoptimized + debuginfo] target(s) in 6.69s
Running tests/tests.rs (target/debug/deps/tests-ddba0b3dfe6b5059)

running 1 test
run: /home/biplab/project/coreutils/target/debug/coreutils cksum asdf
thread 'test_cksum::test_nonexisting_file' panicked at tests/by-util/test_cksum.rs:78:10:
Command was expected to fail.
stdout =
stderr = cksum: asdf: No such file or directory

stack backtrace:
0: rust_begin_unwind
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
1: core::panicking::panic_fmt
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
2: tests::common::util::CmdResult::failure
at ./tests/common/util.rs:400:9
3: tests::common::util::UCommand::fails
at ./tests/common/util.rs:1577:9
4: tests::test_cksum::test_nonexisting_file
at ./tests/by-util/test_cksum.rs:76:5
5: tests::test_cksum::test_nonexisting_file::{{closure}}
at ./tests/by-util/test_cksum.rs:73:28
6: core::ops::function::FnOnce::call_once
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
7: core::ops::function::FnOnce::call_once
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.
test test_cksum::test_nonexisting_file ... FAILED

failures:

failures:
test_cksum::test_nonexisting_file

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 2637 filtered out; finished in 0.05s

error: test failed, to rerun pass -p coreutils --test tests

  • The terminal process "/home/biplab/.cargo/bin/cargo 'test', '--package', 'coreutils', '--test', 'tests', '--', 'test_cksum::test_nonexisting_file', '--exact', '--nocapture'" terminated with exit code: 101.
  • Terminal will be reused by tasks, press any key to close it.

OUTPUT
biplab@BIPLAB:~/project/coreutils/src/uu/cksum/src$ cargo run cksum.rs asdf main.rs
Compiling uu_cksum v0.0.23 (/home/biplab/project/coreutils/src/uu/cksum)
Finished dev [unoptimized + debuginfo] target(s) in 1.08s
Running /home/biplab/project/coreutils/target/debug/cksum cksum.rs asdf main.rs
3105057993 12683 cksum.rs
cksum: asdf: No such file or directory
1517491646 24 main.rs

Thanks for letting me know @sylvestre

@cakebaker
Copy link
Contributor

cakebaker commented Jan 10, 2024

Well, the test expects that cksum asdf fails, i.e. it returns an exit code other than 0. It ensures that uutils cksum outputs the same as GNU cksum in the following case:

$ cksum asdf
cksum: asdf: No such file or directory
$ echo $?
1

Hope that helps :)

@biplab5464
Copy link
Contributor Author

biplab5464 commented Jan 10, 2024

i have check with cksum the output is same only but i am using eprintln. this shoud print to the std error std right

> file_buf = match File::open(filename) {
>                 Ok(file) => file,
>                 Err(_) => {
>                     eprintln!("cksum: {}: No such file or directory", filename.display());
>                     continue;
>                 }
>             };

can you guide me

@cakebaker
Copy link
Contributor

Yes, that's correct. You can save the "cksum: " part by using our show_error! macro. And for the exit code there is also a function set_exit_code().

@biplab5464
Copy link
Contributor Author

Thanks for the help :)

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/cksum/cksum is no longer failing!

@biplab5464
Copy link
Contributor Author

i understand why code quality fails but i don't understand why the other 2 fails

file_buf = match File::open(filename) {
Ok(file) => file,
Err(_) => {
eprintln!("cksum: {}: No such file or directory", filename.display());
Copy link
Member

Choose a reason for hiding this comment

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

I think you got the control flow right, but the error message would now be wrong if there is another reason that the file can't be opened, for example if the permissions are incorrect.

A more correct approach would look like this:

file_buf = match File::open(filename) {
    Ok(file) => file,
    Err(e) => {
        show!(e.map_err_context(|| filename.to_string_lossy().to_string());
        continue;
    }
};

This will automatically add "cksum" and print the error message corresponding to the io error that occurred.

See also the docs for

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the kind gesture

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)

@D9nni
Copy link
Contributor

D9nni commented Jan 10, 2024

I think watching this solution for a similar case would help you: https://github.com/uutils/coreutils/pull/5806/files

@biplab5464
Copy link
Contributor Author

I think watching this solution for a similar case would help you: https://github.com/uutils/coreutils/pull/5806/files

can give little bit more hint

@biplab5464
Copy link
Contributor Author

biplab5464 commented Jan 11, 2024

run: /Users/runner/work/coreutils/coreutils/target/x86_64-apple-darwin/debug/coreutils stat -
test_stat::test_stdin_pipe_fifo2 stdout

can anyone confirm this test is failing because of my code and if it is , why

@cakebaker
Copy link
Contributor

This error is unrelated to your PR.

@biplab5464
Copy link
Contributor Author

Thank you for information

@cakebaker
Copy link
Contributor

The code looks good :) Can you please add a test (or adapt test_nonexisting_file) so we don't regress in the future?

@biplab5464
Copy link
Contributor Author

biplab5464 commented Jan 12, 2024

I was busy so missed your comment
yeah, can you tell me little bit more about it

@cakebaker
Copy link
Contributor

test_nonexisting_file() (in tests/by-util/test_cksum.rs) currently tests whether there is an error message if the specified file doesn't exist. But it doesn't cover the situation you solved with your patch: that cksum continues to process files even if one or more of them don't exist. So a test case should ensure that something like the following works:

$ cksum asdf file
cksum: asdf: No such file or directory
4294967295 0 file

You can run the cksum tests with cargo test --features "cksum" --no-default-features.

Hope that helps :)

@biplab5464
Copy link
Contributor Author

i am sorry i tried but i couldn't do it

@cakebaker
Copy link
Contributor

What do you struggle with?

Today I merged a PR similar to this one that shows an error if the user specifies a directory. Its test might give you an idea for your test: https://github.com/uutils/coreutils/blob/main/tests/by-util/test_cksum.rs#L346 The part relevant for you starts on line 346. I think this should allow you to adapt test_nonexisting_file() accordingly.

@biplab5464
Copy link
Contributor Author

ok i will try again i may need some time because i am busy with another task

@biplab5464
Copy link
Contributor Author

is this test case is okay

fn test_one_nonexisting_file(){
    let (at, mut ucmd) = at_and_ucmd!();

    at.touch("abc.txt");
    at.touch("xyz.txt");

    ucmd
        .arg("abc.txt")
        .arg("asdf.txt")
        .arg("xyz.txt")
        .fails()
        .stdout_contains_line("4294967295 0 xyz.txt")
        .stderr_contains("asdf.txt: No such file or directory")
        .stdout_contains_line("4294967295 0 abc.txt");
}```

@D9nni
Copy link
Contributor

D9nni commented Jan 14, 2024

is this test case is okay

fn test_one_nonexisting_file(){
    let (at, mut ucmd) = at_and_ucmd!();

    at.touch("abc.txt");
    at.touch("xyz.txt");

    ucmd
        .arg("abc.txt")
        .arg("asdf.txt")
        .arg("xyz.txt")
        .fails()
        .stdout_contains_line("4294967295 0 xyz.txt")
        .stderr_contains("asdf.txt: No such file or directory")
        .stdout_contains_line("4294967295 0 abc.txt");
}```

Something like that. I would do it like the others, by creating a new file in tests/fixtures/cksum.

fn test_nonexisting_file_continue() {
    let file_name = "asdf";

    new_ucmd!()
        .arg(file_name)
        .arg("lorem_ipsum.txt")
        .arg("alice_in_wonderland.txt")
        .fails()
        .stdout_is_fixture("nonexisting_file_continue.expected")
        .stderr_contains(format!("cksum: {file_name}: No such file or directory"));
}

@biplab5464
Copy link
Contributor Author

biplab5464 commented Jan 14, 2024

oh,can you explain me a little, i mean do i need to change anything

i think my test case is when one file is missing when giving multiple files

@cakebaker
Copy link
Contributor

Two small things:

  • can you please run cargo fmt
  • and move your test function up to where the test_nonexisting_file function is

Thanks!

@biplab5464
Copy link
Contributor Author

okay i will do it

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)

@cakebaker cakebaker merged commit 076b905 into uutils:main Jan 15, 2024
60 of 61 checks passed
@cakebaker
Copy link
Contributor

Thanks for your PR :)

@biplab5464
Copy link
Contributor Author

Thank you for your cooperation, I learnt a lot

sylvestre pushed a commit to sylvestre/coreutils that referenced this pull request Jan 19, 2024
…ls#5820)

* cksum: stops when one of given files doesn't exist uutils#5809

* removed printld  cksum: stops when one of given files doesn't exist uutils#5809

* formatting the code cksum: stops when one of given files doesn't exist uutils#5809

* formatting the code cksum: stops when one of given files doesn't exist uutils#5809

* set exist status cksum: stops when one of given files doesn't exist uutils#5809

* code quality formatting issue cksum: stops when one of given files doesn't exist uutils#5809

* tertsdiepraam idea cksum: stops when one of given files doesn't exist uutils#5809

* one new test case and deleted duplicate show cksum: stops when one of given files doesn't exist uutils#5809

* formatting the test cases cksum: stops when one of given files doesn't exist uutils#5809

* Revert "formatting the test cases cksum: stops when one of given files doesn't exist uutils#5809"

This reverts commit 8c382f1.

* reverting the changes and committing again due to a mistake cksum: stops when one of given files doesn't exist uutils#5809

---------

Co-authored-by: biplab5464 <biplab5464@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cksum: stops when one of given files doesn't exist
5 participants