Skip to content

Commit

Permalink
test(unittest): Rewrite unittests to meet new requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
MortalHappiness committed Oct 24, 2023
1 parent 3b2e812 commit f27de8b
Showing 1 changed file with 124 additions and 26 deletions.
150 changes: 124 additions & 26 deletions tests/test_missing.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit f27de8b

Please sign in to comment.