Skip to content

test: add edge-case tests for FileIdentity#3076

Open
invo-coder19 wants to merge 1 commit intometabrainz:masterfrom
invo-coder19:feature/file-identity-edge-case-tests
Open

test: add edge-case tests for FileIdentity#3076
invo-coder19 wants to merge 1 commit intometabrainz:masterfrom
invo-coder19:feature/file-identity-edge-case-tests

Conversation

@invo-coder19
Copy link

Expand test/test_file_identity.py by adding 10 new tests that cover currently uncovered branches in FileIdentity.eq and FileIdentity.init.

Add the following tests:
1.test_both_missing_equal
Verifies that two FileIdentity instances created for a non-existent path compare as equal. This covers the branch where both files do not exist → considered equal.

2.test_eq_with_non_fileidentity_raises
Confirms that comparing a FileIdentity instance with an unrelated object raises an AttributeError, since the implementation does not include an isinstance guard.

3.test_hash_none_at_init_computed_lazily_during_eq
Tests the lazy hash computation path (lines 187–190 in file.py).
Use unittest.mock to ensure the hash is calculated only when equality comparison occurs.

4.test_identity_empty_file_equal
Ensures that two identities representing empty (0-byte) files compare as equal.

5.test_fast_hash_empty_file
Verifies that _fast_hash produces a stable hexadecimal hash string for an empty file.

6.test_identity_filepath_stored_as_path
Confirms that the _filepath attribute is stored as a pathlib.Path object, not a plain string.

7.test_identity_symlink_same_as_target (Unix only)
Checks that a symlink and its target compare as equal because they share the same inode.

8.test_identity_symlink_after_retarget_not_equal (Unix only)
Ensures that if a symlink is retargeted to a different file, the resulting identities are not equal due to the changed inode.

9.test_windows_inode_zero_same_file_equal (Windows only)
On Windows, where st_ino == 0, verifies that equality falls back to file size, modification time, and hash, allowing two identities of the same file to compare as equal.

10.test_windows_inode_zero_different_files_same_size_not_equal (Windows only)
Ensures that when inode values are always 0, the hash comparison correctly distinguishes two different files that happen to have the same size.

Expand test/test_file_identity.py with 10 new tests targeting
uncovered branches in FileIdentity.__eq__ and FileIdentity.__init__:

- test_both_missing_equal: verifies two identities for a non-existent
  path are equal (the 'both non-existent => equal' branch)
- test_eq_with_non_fileidentity_raises: documents that comparing with
  an unrelated object raises AttributeError (no isinstance guard)
- test_hash_none_at_init_computed_lazily_during_eq: exercises the lazy
  hash-computation path (lines 187-190 of file.py) using unittest.mock
- test_identity_empty_file_equal: equality for 0-byte files
- test_fast_hash_empty_file: _fast_hash returns a stable hex string for
  an empty file
- test_identity_filepath_stored_as_path: _filepath is a pathlib.Path
- test_identity_symlink_same_as_target: symlink and target share inode,
  so they compare equal (Unix only)
- test_identity_symlink_after_retarget_not_equal: retargeted symlink
  detects a different inode (Unix only)
- test_windows_inode_zero_same_file_equal: on Windows st_ino==0,
  equality falls through to size/mtime/hash (Windows only)
- test_windows_inode_zero_different_files_same_size_not_equal: hash
  distinguishes two different files when inode is always 0 (Windows only)

New imports: pathlib.Path, unittest.mock.patch
All platform-specific tests are gated with @unittest.skipIf/skipUnless.
@invo-coder19 invo-coder19 marked this pull request as draft March 10, 2026 11:09
@invo-coder19 invo-coder19 marked this pull request as ready for review March 10, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants