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

Display modification times of input files in context and unified diff #33

Merged
merged 18 commits into from
Apr 16, 2024

Conversation

TanmayPatil105
Copy link
Contributor

Fixes #31

tanmay@tanmay-lenovo:~/Desktop/projects/uutils/diffutils$ cargo run -- -c fruits_old.txt fruits_new.txt 
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/diffutils -c fruits_old.txt fruits_new.txt`
*** fruits_old.txt	2024-03-24 23:43:05.189597645 +0530
--- fruits_new.txt	2024-03-24 23:35:08.922581904 +0530
***************
*** 1,3 ****
  Apple
! Banana
  Cherry
--- 1,3 ----
  Apple
! Fig
  Cherry
tanmay@tanmay-lenovo:~/Desktop/projects/uutils/diffutils$ diff -c fruits_old.txt fruits_new.txt 
*** fruits_old.txt	2024-03-24 23:43:05.189597645 +0530
--- fruits_new.txt	2024-03-24 23:35:08.922581904 +0530
***************
*** 1,3 ****
  Apple
! Banana
  Cherry
--- 1,3 ----
  Apple
! Fig
  Cherry

@sylvestre
Copy link
Sponsor Collaborator

Could you please add tests

@sylvestre
Copy link
Sponsor Collaborator

Thanks

@TanmayPatil105
Copy link
Contributor Author

TanmayPatil105 commented Mar 27, 2024

Okay! The current tests are failing.

error: test failed, to rerun pass `--lib`
---- context_diff::tests::test_permutations_missing_lines stdout ----
thread 'context_diff::tests::test_permutations_missing_lines' panicked at src/context_diff.rs:269:44:
Failed to get metadata: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- context_diff::tests::test_permutations_empty_lines stdout ----
thread 'context_diff::tests::test_permutations_empty_lines' panicked at src/context_diff.rs:269:44:
Failed to get metadata: Os { code: 2, kind: NotFound, message: "No such file or directory" }

---- context_diff::tests::test_permutations stdout ----
thread 'context_diff::tests::test_permutations' panicked at src/context_diff.rs:269:44:
Failed to get metadata: Os { code: 2, kind: NotFound, message: "No such file or directory" }

---- context_diff::tests::test_permutations_reverse stdout ----
thread 'context_diff::tests::test_permutations_reverse' panicked at src/context_diff.rs:269:44:
Failed to get metadata: Os { code: 2, kind: NotFound, message: "No such file or directory" }

---- context_diff::tests::test_stop_early stdout ----
thread 'context_diff::tests::test_stop_early' panicked at src/context_diff.rs:269:44:
Failed to get metadata: Os { code: 2, kind: NotFound, message: "No such file or directory" }

Isn't this strange? We check if file exists in main.rs : https://github.com/uutils/diffutils/blob/main/src/main.rs#L52

Edit: These are unit tests. My bad!

@TanmayPatil105
Copy link
Contributor Author

Could you please add tests

Can you guide me more on these 😅?
Current tests are quite difficult to understand

@sylvestre
Copy link
Sponsor Collaborator

@TanmayPatil105
Copy link
Contributor Author

@sylvestre I was looking at the unit tests and I see we are not actually creating files but only passing filename literals to context_diff::diff. The tests are failing since we are looking for metadata for files which don't exist in the diff function. We are using expected_filename/actual_filename only for printing.
Should we create those files during the tests?

@sylvestre
Copy link
Sponsor Collaborator

yes please :)

@sylvestre sylvestre force-pushed the context-diff-modification-time branch from 392f726 to 9d4346e Compare March 30, 2024 14:34
src/context_diff.rs Outdated Show resolved Hide resolved
@TanmayPatil105
Copy link
Contributor Author

TanmayPatil105 commented Mar 31, 2024

I'm not sure on how the tests are failing on CI, we are creating the files now. It should have worked. Strange 🤔

The tests are working fine on my machine:

test context_diff::tests::test_permutations_missing_lines ... ok
test context_diff::tests::test_permutations_empty_lines ... ok
test context_diff::tests::test_permutations_reverse ... ok
test context_diff::tests::test_permutations ... ok
test normal_diff::tests::test_stop_early ... ok

@sylvestre
Copy link
Sponsor Collaborator

sorry, could you please fix the conflict? Sorry :(

Cargo.toml Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.85%. Comparing base (be66ff3) to head (5b814f8).

❗ Current head 5b814f8 differs from pull request most recent head aedd068. Consider uploading reports for the commit aedd068 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
- Coverage   76.29%   75.85%   -0.45%     
==========================================
  Files           9        9              
  Lines        2582     2597      +15     
  Branches      663      662       -1     
==========================================
  Hits         1970     1970              
- Misses        459      476      +17     
+ Partials      153      151       -2     
Flag Coverage Δ
macos_latest 76.00% <100.00%> (?)
ubuntu_latest 75.81% <100.00%> (-0.49%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TanmayPatil105
Copy link
Contributor Author

TanmayPatil105 commented Apr 3, 2024

I realized that the tests were passing locally because files were generated during previous test runs.
wondering if cargo should clean up the temp files generated during tests 🤔 . Is there any way to specify it? OR we should manually delete them after every unit test.

@sylvestre
Copy link
Sponsor Collaborator

Is there any way to specify it?

Yeah, just https://crates.io/crates/tempfile to create these files

src/context_diff.rs Outdated Show resolved Hide resolved
src/context_diff.rs Outdated Show resolved Hide resolved
src/context_diff.rs Outdated Show resolved Hide resolved
@TanmayPatil105 TanmayPatil105 changed the title Display modification times of input files in context diff Display modification times of input files in context and unified diff Apr 4, 2024
Copy link
Collaborator

@oSoMoN oSoMoN left a comment

Choose a reason for hiding this comment

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

Thanks for the update, this is looking promising!
See inline for a few suggestions.
Also, note that the read_from_stdin() integration test is failing. The expectation on the output will need to be updated to ignore the timestamps.

Cargo.toml Outdated Show resolved Hide resolved
src/utils.rs Show resolved Hide resolved
src/context_diff.rs Outdated Show resolved Hide resolved
@TanmayPatil105
Copy link
Contributor Author

Hey @oSoMoN , I have made the necessary changes! Can you review the MR?
Thanks!

src/utils.rs Outdated
let target = "target/utils";
let _ = std::fs::create_dir(target);
let filename = &format!("{target}/foo");
let temp = File::create(filename).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use a tempfile.NamedTempFile instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there are APIs to set modification time for a NamedTempFile.

Copy link
Collaborator

Choose a reason for hiding this comment

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

NamedTempFile::as_file() exposes a reference to the underlying file, so you could do something like this:

use tempfile::NamedTempFile;
let temp = NamedTempFile::new().unwrap();
let current = SystemTime::now();
let _ = temp.as_file().set_modified(current);assert_eq!(current, get_modification_time(&temp.path().to_string_lossy()));

src/utils.rs Outdated Show resolved Hide resolved
src/macros.rs Outdated Show resolved Hide resolved
src/macros.rs Show resolved Hide resolved
src/utils.rs Show resolved Hide resolved
src/context_diff.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@oSoMoN oSoMoN left a comment

Choose a reason for hiding this comment

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

Thanks Tanmay, that looks quite good!
I have left a handful of comments/suggestions, see inline.

@TanmayPatil105
Copy link
Contributor Author

Halfway through the changes. I'll complete them shortly!

Since we now returning SystemTime::now() for invalid file input,
there is no need to crate dummy files
Copy link
Collaborator

@oSoMoN oSoMoN left a comment

Choose a reason for hiding this comment

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

Thanks, that looks really good now. Almost ready to merge, see only my suggestion on the use of NamedTempFile.

src/utils.rs Show resolved Hide resolved
src/utils.rs Outdated
let target = "target/utils";
let _ = std::fs::create_dir(target);
let filename = &format!("{target}/foo");
let temp = File::create(filename).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

NamedTempFile::as_file() exposes a reference to the underlying file, so you could do something like this:

use tempfile::NamedTempFile;
let temp = NamedTempFile::new().unwrap();
let current = SystemTime::now();
let _ = temp.as_file().set_modified(current);assert_eq!(current, get_modification_time(&temp.path().to_string_lossy()));

@oSoMoN
Copy link
Collaborator

oSoMoN commented Apr 15, 2024

It's worth noting that with the changes in this PR, one test from the GNU test suite which currently fails in main now passes (the timezone test). Nice improvement!

@TanmayPatil105
Copy link
Contributor Author

Done!

@TanmayPatil105
Copy link
Contributor Author

Sorry for the noise! I forgot to run cargo fmt.

@oSoMoN oSoMoN merged commit 1b311c6 into uutils:main Apr 16, 2024
21 of 23 checks passed
@oSoMoN
Copy link
Collaborator

oSoMoN commented Apr 16, 2024

Merged, thanks Tanmay for the contribution!

@TanmayPatil105 TanmayPatil105 deleted the context-diff-modification-time branch April 16, 2024 16:53
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.

Context diff doesn't display the last modification timestamp
3 participants