Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/usethis/_file/ini/test_ini_io_.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ def relative_path(self) -> Path:
pass

class TestReadFile:
def test_empty_file(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("valid.ini")

valid_file = tmp_path / "valid.ini"
valid_file.touch()

# Act
with change_cwd(tmp_path), MyINIFileManager() as manager:
result = manager.read_file()

# Assert
assert list(result.sections()) == []

def test_invalid_file(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
Expand Down Expand Up @@ -162,6 +180,42 @@ def relative_path(self) -> Path:
assert result["section"]["key"].value == "value"

class TestContains:
def test_empty_file_no_keys(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("valid.ini")

valid_file = tmp_path / "valid.ini"
valid_file.touch()

# Act
with change_cwd(tmp_path), MyINIFileManager() as manager:
result = [] in manager

# Assert
assert result is True

def test_empty_file_section_missing(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("valid.ini")

valid_file = tmp_path / "valid.ini"
valid_file.touch()

# Act
with change_cwd(tmp_path), MyINIFileManager() as manager:
result = ["section"] in manager

# Assert
assert result is False

def test_option_key_exists(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
Expand Down Expand Up @@ -362,6 +416,24 @@ def relative_path(self) -> Path:
assert result is True

class TestGetItem:
def test_empty_file(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("valid.ini")

valid_file = tmp_path / "valid.ini"
valid_file.touch()

# Act
with change_cwd(tmp_path), MyINIFileManager() as manager:
result = manager[[]]

# Assert
assert result == {}

def test_file_doesnt_exist_raises(self, tmp_path: Path):
# Arrange
class MyINIFileManager(INIFileManager):
Expand Down
42 changes: 42 additions & 0 deletions tests/usethis/_file/toml/test_toml_io_.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ def relative_path(self) -> Path:
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
assert [] in manager

def test_empty_file_no_keys(self, tmp_path: Path) -> None:
# Arrange
class MyTOMLFileManager(TOMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("pyproject.toml")

(tmp_path / "pyproject.toml").touch()

# Act, Assert
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
assert [] in manager

def test_empty_file_key_missing(self, tmp_path: Path) -> None:
# Arrange
class MyTOMLFileManager(TOMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("pyproject.toml")

(tmp_path / "pyproject.toml").touch()

# Act, Assert
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
assert ["a"] not in manager

def test_list_of_keys_not_in_scalar(self, tmp_path: Path) -> None:
# Arrange
class MyTOMLFileManager(TOMLFileManager):
Expand Down Expand Up @@ -79,6 +107,20 @@ def relative_path(self) -> Path:
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
assert manager[[]] == {"tool": {"usethis": {"a": "b"}}}

def test_empty_file(self, tmp_path: Path) -> None:
# Arrange
class MyTOMLFileManager(TOMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("pyproject.toml")

(tmp_path / "pyproject.toml").touch()

# Act, Assert
with change_cwd(tmp_path), MyTOMLFileManager() as manager:
assert manager[[]] == {}

def test_scalar_not_mapping(self, tmp_path: Path) -> None:
# Arrange
class MyTOMLFileManager(TOMLFileManager):
Expand Down
67 changes: 67 additions & 0 deletions tests/usethis/_file/yaml/test_yaml_io_.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ def relative_path(self) -> Path:
assert isinstance(manager._content, YAMLDocument)
assert isinstance(manager._content.content, CommentedMap)

def test_empty_file(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("my_yaml_file.yaml")

(tmp_path / "my_yaml_file.yaml").touch()

with change_cwd(tmp_path), MyYAMLFileManager() as manager:
# Act
manager.read_file()

# Assert
assert isinstance(manager._content, YAMLDocument)
assert isinstance(manager._content.content, CommentedMap)
assert len(manager._content.content) == 0

def test_file_not_found(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
Expand Down Expand Up @@ -239,6 +258,38 @@ def relative_path(self) -> Path:
# Act, Assert
assert manager.__contains__(["key"])

def test_empty_file_no_keys(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("my_yaml_file.yaml")

(tmp_path / "my_yaml_file.yaml").touch()

with change_cwd(tmp_path), MyYAMLFileManager() as manager:
manager.read_file()

# Act, Assert
assert manager.__contains__([])

def test_empty_file_key_missing(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("my_yaml_file.yaml")

(tmp_path / "my_yaml_file.yaml").touch()

with change_cwd(tmp_path), MyYAMLFileManager() as manager:
manager.read_file()

# Act, Assert
assert not manager.__contains__(["key"])

def test_single_map_two_keys(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
Expand Down Expand Up @@ -344,6 +395,22 @@ def relative_path(self) -> Path:
# Assert
assert value == "value"

def test_empty_file(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
@property
@override
def relative_path(self) -> Path:
return Path("my_yaml_file.yaml")

(tmp_path / "my_yaml_file.yaml").touch()

with change_cwd(tmp_path), MyYAMLFileManager() as manager:
manager.read_file()

# Act, Assert
assert manager[[]] == {}

def test_empty_keys(self, tmp_path: Path):
# Arrange
class MyYAMLFileManager(YAMLFileManager):
Expand Down
Loading