Skip to content

Commit

Permalink
+test boot script
Browse files Browse the repository at this point in the history
  • Loading branch information
Yell0wflash committed Sep 3, 2018
1 parent 2c85beb commit 1aa14ca
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Empty file modified boot_django_for_runners.sh
100644 → 100755
Empty file.
123 changes: 123 additions & 0 deletions for_runners/tests/test_boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
from pprint import pprint

# https://github.com/jedie/django-for-runners
import for_runners
from for_runners import __version__, cli


def subprocess_output(*args, **kwargs):
kwargs["stderr"] = subprocess.STDOUT
print("_" * 100)
print("Call:", args, kwargs)
try:
output = subprocess.check_output(*args, **kwargs)
except subprocess.CalledProcessError as err:
print(err.output.decode("UTF-8"))
raise

output = output.decode("UTF-8")
print(output)
return output


def assert_is_file(path):
assert path.is_file(), "File '%s' not exist!" % path


def assert_is_dir(path):
assert path.is_dir(), "Direcotry '%s' not exist!" % path


class CLITest(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.cli_file_path = Path(cli.__file__)

def test_cli_executable(self):
assert_is_file(self.cli_file_path)
self.assertTrue(os.access(str(self.cli_file_path), os.X_OK), "File '%s' not executeable!" % self.cli_file_path)

def test_version(self):
output = subprocess_output(["%s" % self.cli_file_path, "--version"])
self.assertEqual(output, "%s\n" % __version__)

def test_help(self):
output = subprocess_output(["%s" % self.cli_file_path, "--help"])
self.assertIn("Just start this file without any arguments to run the dev. server", output)


class BootTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.boot_file_path = Path(Path(for_runners.__file__).parent.parent, "boot_django_for_runners.sh")

def test_boot_executable(self):
assert_is_file(self.boot_file_path)
self.assertTrue(
os.access(str(self.boot_file_path), os.X_OK), "File '%s' not executeable!" % self.boot_file_path
)

def test_boot(self):
env = os.environ.copy()

# We make the pip cache directory permanent.
# Actually only important when developing, if the test runs more often ;)
#
# - create e.g.: /tmp/.cache
# - symlink the .cache directory into e.g.: /tmp/for_runners_test_boot_XXXXX/.cache
#
temp_dir = tempfile.gettempdir()
cache_dir = Path(temp_dir, ".cache")
cache_dir.mkdir(mode=0o777, parents=True, exist_ok=True)

with tempfile.TemporaryDirectory(prefix="for_runners_test_boot_") as temp_path:
cache_dir_inner = Path(temp_path, ".cache")
self.assertFalse(cache_dir_inner.is_dir(), "Already exists: %s" % cache_dir_inner)
cache_dir_inner.symlink_to(cache_dir, target_is_directory=True)
self.assertTrue(cache_dir_inner.exists(), "Don't exists: %s" % cache_dir_inner)

env["HOME"] = temp_path
env["TERM"] = "dumb"
# pprint(env)

output = subprocess_output(["%s" % self.boot_file_path], env=env)

env_path = Path(temp_path, "Django-ForRunners")
assert_is_dir(env_path)

self.assertIn(
"+ pip3 install -e git+https://github.com/jedie/django-for-runners.git@master#egg=django-for-runners",
output
)
self.assertIn(
"+ pip3 install -r %s/Django-ForRunners/src/django-for-runners/requirements.txt" % temp_path, output
)
self.assertIn("+ ./for_runners --version", output)
self.assertIn("%s" % __version__, output)

bin_path = Path(env_path, "bin")
assert_is_dir(bin_path)
# subprocess_output(["ls", "-la", "%s" % bin_path], env=env)

pip_path = Path(bin_path, "pip3")
assert_is_file(pip_path)

output = subprocess_output(["%s" % pip_path, "freeze"], env=env)
self.assertIn("Django==2.1", output)
self.assertIn("#egg=django_for_runners", output)

manage_path = Path(bin_path, "manage")
assert_is_file(manage_path)

for_runners_path = Path(bin_path, "for_runners")
assert_is_file(for_runners_path)

output = subprocess_output(["%s" % for_runners_path, "--version"], env=env)
self.assertIn(__version__, output)

0 comments on commit 1aa14ca

Please sign in to comment.