sapling/eden/integration/unlink_test.py
generatedunixname89002005287564 6216b83d43 Pyre Configurationless migration for] [batch:13/244]
Reviewed By: connernilsen

Differential Revision: D54470890

fbshipit-source-id: 1778b533643b6f0ac94af1ef1801707ca97fefa6
2024-03-04 08:24:00 -08:00

135 lines
4.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
# pyre-unsafe
import errno
import os
import shutil
import sys
from typing import Tuple
from .lib import testcase
@testcase.eden_repo_test
class UnlinkTest(testcase.EdenRepoTest):
def populate_repo(self) -> None:
self.repo.write_file("hello", "hola\n")
self.repo.write_file("adir/file", "foo!\n")
self.repo.symlink("slink", "hello")
self.repo.commit("Initial commit.")
def test_unlink(self) -> None:
filename = os.path.join(self.mount, "hello")
# This file is part of the git repo
with open(filename, "r") as f:
self.assertEqual("hola\n", f.read())
# Removing should succeed
os.unlink(filename)
with self.assertRaises(OSError) as context:
os.lstat(filename)
self.assertEqual(
context.exception.errno,
errno.ENOENT,
msg="lstat on a removed file raises ENOENT",
)
def test_unlink_bogus_file(self) -> None:
with self.assertRaises(OSError) as context:
os.unlink(os.path.join(self.mount, "this-file-does-not-exist"))
self.assertEqual(
context.exception.errno,
errno.ENOENT,
msg="unlink raises ENOENT for nonexistent file",
)
def get_expected_errno(self) -> Tuple[int, str]:
if sys.platform == "linux":
return errno.EISDIR, "EISDIR"
elif sys.platform == "win32":
return errno.EACCES, "EACCES"
else:
return errno.EPERM, "EPERM"
def test_unlink_dir(self) -> None:
adir = os.path.join(self.mount, "adir")
with self.assertRaises(OSError) as context:
os.unlink(adir)
err, errstr = self.get_expected_errno()
self.assertEqual(
context.exception.errno,
err,
msg=f"unlink on a dir raises {errstr}",
)
def test_unlink_empty_dir(self) -> None:
adir = os.path.join(self.mount, "an-empty-dir")
os.mkdir(adir)
with self.assertRaises(OSError) as context:
os.unlink(adir)
err, errstr = self.get_expected_errno()
self.assertEqual(
context.exception.errno,
err,
msg=f"unlink on an empty dir raises {errstr}",
)
def test_rmdir_file(self) -> None:
filename = os.path.join(self.mount, "hello")
with self.assertRaises(OSError) as context:
os.rmdir(filename)
self.assertEqual(
context.exception.errno, errno.ENOTDIR, msg="rmdir on a file raises ENOTDIR"
)
def test_rmdir(self) -> None:
adir = os.path.join(self.mount, "adir")
with self.assertRaises(OSError) as context:
os.rmdir(adir)
self.assertEqual(
context.exception.errno,
errno.ENOTEMPTY,
msg="rmdir on a non-empty dir raises ENOTEMPTY",
)
shutil.rmtree(adir)
with self.assertRaises(OSError) as context:
os.lstat(adir)
self.assertEqual(
context.exception.errno,
errno.ENOENT,
msg="lstat on a removed dir raises ENOENT",
)
def test_rmdir_overlay(self) -> None:
# Ensure that removing dirs works with things we make in the overlay
deep_root = os.path.join(self.mount, "buck-out")
deep_name = os.path.join(deep_root, "foo", "bar", "baz")
os.makedirs(deep_name)
with self.assertRaises(OSError) as context:
os.rmdir(deep_root)
self.assertEqual(
context.exception.errno,
errno.ENOTEMPTY,
msg="rmdir on a non-empty dir raises ENOTEMPTY",
)
shutil.rmtree(deep_root)
with self.assertRaises(OSError) as context:
os.lstat(deep_root)
self.assertEqual(
context.exception.errno,
errno.ENOENT,
msg="lstat on a removed dir raises ENOENT",
)