thinwybk
thinwybk

Reputation: 4743

How can I fake hard links in fake filesystem in Python tests?

I'd like to check if Python production code interacting with the filesystem considers hard links (file names pointing to the same inode) the right way.

In a unit test I'm using a fake filesystem (created with pytest fixture tmp_path, is a pathlib/pathlib2.Path object) and would like to fake hard links in it.

The fake filesystem in the test shall contain three hard links hard_link_0...2.txt pointing at the same inode in the fake filesystem.

hard_link_0.txt
hard_link_1.txt
/dir
  hard_link_2.txt

Upvotes: 0

Views: 309

Answers (1)

Amadan
Amadan

Reputation: 198436

Since tmp_path creates an actual directory (as opposed to mocking pathlib), you can treat everything in tmp_path normally. Thus, to create a hard link, you can use os.link(srcpath, dstpath), as you normally would (from Python 3.6+; otherwise, use os.link(str(srcpath), str(dstpath))).

Upvotes: 1

Related Questions