From f27de8b62f8fd6c1cff61b073cf9038a55bf6549 Mon Sep 17 00:00:00 2001 From: Chi-Sheng Liu Date: Tue, 24 Oct 2023 14:21:55 +0800 Subject: [PATCH] test(unittest): Rewrite unittests to meet new requirements --- tests/test_missing.py | 150 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 26 deletions(-) diff --git a/tests/test_missing.py b/tests/test_missing.py index 468dc44..73a00b8 100644 --- a/tests/test_missing.py +++ b/tests/test_missing.py @@ -1,51 +1,149 @@ +from __future__ import annotations import os +import subprocess import tempfile +import shutil +from pathlib import Path import pytest from missing import Missing +class File: + def __init__(self, name: str): + self.name = name + + +class Dir: + def __init__(self, name: str = ".", children: list[Dir | File] | None = None): + self.name = name + self.children: list[Dir | File] = children or [] + + +def create_directory_tree(directory: Dir): + for child in directory.children: + if isinstance(child, Dir): + os.mkdir(child.name) + os.chdir(child.name) + create_directory_tree(child) + os.chdir('..') + else: + with open(child.name, 'w'): + pass + + @pytest.fixture def temp_git_folder(): - # Create a temp git folder so we could conduct the integration test + # Create a temp git folder so that we could conduct the integration test with tempfile.TemporaryDirectory() as tmp_dir: os.chdir(tmp_dir) with open('.gitignore', 'w') as fout: - fout.write('test_ignore.py') - - os.mkdir('tests') - os.mkdir(os.path.join('tests', 'ignored')) - with open(os.path.join('tests', 'ignored', 'test_ignore.py'), 'w'): - pass - - os.mkdir(os.path.join('tests', 'untracked')) - with open(os.path.join('tests', 'untracked', 'test_untracked.py'), 'w'): - pass + lines = [ + "ignored.py", + "ignored_folder/", + ".hidden_ignored_folder/", + ] + fout.write("\n".join(lines)) - os.mkdir(os.path.join('tests', 'staged')) - with open(os.path.join('tests', 'staged', 'test_staged.py'), 'w'): - pass + tree = Dir( + children=[ + Dir("folder1", [ + Dir("folder1_1", [ + Dir("folder1_1_1", [ + File("folder1_1_1.py"), + ]), + Dir("folder1_1_2", [ + File("__init__.py"), + ]), + ]), + Dir("folder1_2", [ + File("folder1_2.py"), + ]), + Dir("folder_containing_only_ignored_files", [ + File("ignored.py"), + ]), + Dir("ignored_folder", [ + File("some_ignored_file.py"), + ]), + Dir(".hidden_ignored_folder", [ + File("some_ignored_file.py"), + ]), + Dir("empty_folder"), + File("folder1.py"), + ]), + Dir("folder2", [ + Dir("folder2_1", [ + File("__init__.py"), + ]), + Dir("folder2_2", [ + File("folder2_2.py"), + ]), + File("folder2.py"), + File("__init__.py"), + ]), + Dir("folder3", [ + File("other_languages.js"), + ]), + Dir("empty_folder"), + Dir("ignored_folder", [ + File("some_ignored_file.py"), + ]), + Dir(".hidden_ignored_folder", [ + File("some_ignored_file.py"), + ]), + File("toplevel.py"), + ] + ) - os.mkdir('data') - with open(os.path.join('data', 'user.xml'), 'w'): - pass + create_directory_tree(tree) - os.system('git init') - os.system('git add %s' % os.path.join('tests', 'staged')) + if shutil.which('tree'): + subprocess.run(["tree", "-a", "-F", "--dirsfirst"]) + subprocess.run(["git", "init"]) + subprocess.run(["git", "add", "-A"]) yield class TestMissing: - def test__obey_gitignore(self, temp_git_folder): + def test__default(self, temp_git_folder): assert 1 == Missing(exclude=[]).run() - assert not os.path.exists('data/__init__.py') - assert not os.path.exists('tests/ignored/__init__.py') - assert os.path.isfile('tests/untracked/__init__.py') - assert os.path.isfile('tests/staged/__init__.py') - assert os.path.isfile('tests/__init__.py') + p = Path(".") + assert not (p / "__init__.py").exists() + assert not (p / "folder1" / "folder1_1" / "__init__.py").exists() + assert not (p / "ignored_folder" / "__init__.py").exists() + assert not (p / ".hidden_ignored_folder" / "__init__.py").exists() + assert not (p / "empty_folder" / "__init__.py").exists() + assert not (p / "folder1" / "ignored_folder" / "__init__.py").exists() + assert not (p / "folder1" / ".hidden_ignored_folder" / "__init__.py").exists() + assert not (p / "folder1" / "empty_folder" / "__init__.py").exists() + assert not (p / "folder3" / "__init__.py").exists() + assert not (p / "folder1" / "folder_containing_only_ignored_files" / "__init__.py").exists() + assert (p / "folder1" / "__init__.py").is_file() + assert (p / "folder1" / "folder1_1" / "folder1_1_1" / "__init__.py").is_file() + assert (p / "folder1" / "folder1_2" / "__init__.py").is_file() + assert (p / "folder2" / "folder2_2" / "__init__.py").is_file() assert 0 == Missing(exclude=[]).run() def test__exclude(self, temp_git_folder): - assert 0 == Missing(exclude=['tests']).run() + exclude = [ + os.path.join("folder1", "folder1_1"), + ] + assert 1 == Missing(exclude=exclude).run() + p = Path(".") + assert not (p / "__init__.py").exists() + assert not (p / "folder1" / "folder1_1" / "__init__.py").exists() + assert not (p / "ignored_folder" / "__init__.py").exists() + assert not (p / ".hidden_ignored_folder" / "__init__.py").exists() + assert not (p / "empty_folder" / "__init__.py").exists() + assert not (p / "folder1" / "ignored_folder" / "__init__.py").exists() + assert not (p / "folder1" / ".hidden_ignored_folder" / "__init__.py").exists() + assert not (p / "folder1" / "empty_folder" / "__init__.py").exists() + assert not (p / "folder3" / "__init__.py").exists() + assert not (p / "folder1" / "folder_containing_only_ignored_files" / "__init__.py").exists() + assert not (p / "folder1" / "folder1_1" / "folder1_1_1" / "__init__.py").exists() + assert (p / "folder1" / "__init__.py").is_file() + assert (p / "folder1" / "folder1_2" / "__init__.py").is_file() + assert (p / "folder2" / "folder2_2" / "__init__.py").is_file() + assert 0 == Missing(exclude=exclude).run()