Skip to content

Add tests in mod.rs #1123

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add tests in mod.rs #1123

wants to merge 2 commits into from

Conversation

RagnarokMew
Copy link

@RagnarokMew RagnarokMew commented Mar 23, 2025

Added 2 tests for calculate_total_file_size(), creating files with certain sizes and testing if the function correctly calculates their sizes.

This PR is targeted at issue #46

TODO

  • Use cargo fmt
  • Add necessary tests
  • Update default config/theme in README (if applicable)
  • Update man page at lsd/doc/lsd.md (if applicable)

@RagnarokMew RagnarokMew requested a review from zwpaper as a code owner March 23, 2025 15:06
@RagnarokMew
Copy link
Author

RagnarokMew commented Mar 23, 2025

Wanted to also write a test for a full hierarchy, but am unsure how to proceed since the size of the directories can vary (at least that's what I think can happen based on each OS this is ran on). The hierarchy itself is simple:

tempdir
-> 100b-file.txt
-> child
--> 100b-file.txt

Testing on this hierarchy in the code below returns 340 bytes;
Deleting the 100b-files, the changed hierarchy would return 140 bytes, while an empty folder returns 60 bytes.
Any help with this would be greatly appreciated.

My test:

#[test]
    fn test_calculate_total_file_size_directory() {
        let dir_parent = assert_fs::TempDir::new().unwrap();
        let path_file = dir_parent.path().join("100B-text.txt");
        let file = File::create(&path_file).expect("failed to create file");

        let mut buff_writer = BufWriter::new(file);

        let buffer = vec![0u8;100];

        buff_writer.write_all(&buffer).expect("failed to write bytes to file");
        buff_writer.flush().expect("failed to write all bytes to file");
        let dir_child_path = dir_parent.path().join("child");
        fs::create_dir(&dir_child_path).expect("failed to create subdirectory");
        let path_file = dir_child_path.join("100B-text.txt");
        let file = File::create(&path_file).expect("failed to create subdirectory file");

        let mut buff_writer = BufWriter::new(file);

        let buffer = vec![0u8;100];
        buff_writer.write_all(&buffer).expect("failed to write bytes to subdirectory file");
        buff_writer.flush().expect("failed to write all bytes to subdirectory file");


        println!("{}",Meta::calculate_total_file_size(dir_parent.path()));

        assert!(Meta::calculate_total_file_size(dir_parent.path()) == 340);
    }

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.

1 participant