Ejemplo n.º 1
0
def test_create_dir(tmp_path):
    # since tmp_path creates a directory we will create a subdirectory "test" in tmp_path using create_dir
    assert is_dir(tmp_path)
    dirname = os.path.join(tmp_path, "test")
    # check we dont have a directory before creation
    assert not is_dir(dirname)
    # creating directory
    create_dir(dirname)
    # check if directory is created  after invoking create_dir
    assert is_dir(dirname)

    with pytest.raises(BuildTestError):
        create_dir("/xyz")
Ejemplo n.º 2
0
    def load_paths(self):
        """Add all paths to search for buildspecs. We must read configuration file
        and check property ``buildspec_roots`` for list of directories to search.
        We check all directories exist, if any fail we don't add them to path.
        In addition, we add the default buildspec path where we find tutorials
        and general tests.
        """

        buildspec_paths = self.configuration.target_config.get(
            "buildspec_roots") or []

        if buildspec_paths:
            self.roots += buildspec_paths

        # only load default buildspecs if 'load_default_buildspecs' set to True
        if self.configuration.target_config.get("load_default_buildspecs"):
            self.paths += BUILDSPEC_DEFAULT_PATH

        # for every root buildspec defined in configuration or via --root option,
        # we resolve path and if path exist add to self.paths. The path must be a
        # directory. If its file, we ignore it
        if self.roots:

            for root in self.roots:
                path = resolve_path(root, exist=False)
                if not os.path.exists(path):
                    print(f"Path: {path} does not exist!")

                if is_file(path):
                    print(f"Path: {path} must be a directory not a file")

                if is_dir(path):
                    self.paths.append(path)
Ejemplo n.º 3
0
def test_write_file_exceptions(tmp_path):
    msg = "hi my name is Bob"
    file = os.path.join(tmp_path, "name.txt")
    print(f"Writing content: {msg} to file {file}")
    write_file(file, msg)

    # testing invalid type for file stream
    with pytest.raises(BuildTestError):
        print("Passing 'None' as input filestream to write_file")
        write_file(None, msg)

    assert is_dir(tmp_path)
    # testing if directory is passed as filepath, this is also not allowed and expected to raise error
    with pytest.raises(BuildTestError):
        print(
            f"Passing directory: {tmp_path} as input filestream to method write_file"
        )
        write_file(tmp_path, msg)

    filename = "".join(random.choice(string.ascii_letters) for i in range(10))
    path = os.path.join("/", filename)
    print(f"Can't write to path: {path} due to permissions")

    with pytest.raises(BuildTestError):
        write_file(path, msg)

    # input content must be a string, will return None upon
    with pytest.raises(BuildTestError):
        write_file(os.path.join(tmp_path, "null.txt"), ["hi"])
Ejemplo n.º 4
0
def test_resolve_path():
    assert resolve_path("$HOME")
    assert resolve_path("~")

    random_name = "".join(random.choice(string.ascii_letters) for i in range(10))
    # test a directory path that doesn't exist in $HOME with random key, but setting exist=False will return
    # path but doesn't mean file exists
    path = resolve_path(os.path.join("$HOME", random_name), exist=False)

    # checking if path is not file, or directory and not None. This is only valid when exist=False is set
    assert not is_file(path)
    assert not is_dir(path)
    assert path is not None
Ejemplo n.º 5
0
    def __init__(self, buildspec, buildexecutor):
        """The init method will run some checks against buildspec before loading
        buildspec. We retrieve available schemas via method
        ``get_schemas_available`` and check if ``type`` in buildspec
        match available schema. We validate the entire buildspec with
        global.schema.json and validate each test section with the designated
        type schema. If there is any error during the init method, an
        exception will be raised.

        :param buildspec: the pull path to the Buildspec file, must exist.
        :type buildspec: str, required
        :param buildexecutor: an instance of BuildExecutor class defines Executors from configuration file
        :type buildexecutor: BuildExecutor, required
        """

        self.logger = logging.getLogger(__name__)

        if not isinstance(buildexecutor, BuildExecutor):
            raise BuildTestError(
                "Invalid type argument for 'buildexecutor', must be of type BuildExecutor"
            )

        self.buildexecutors = buildexecutor.list_executors()

        # if invalid input for buildspec
        if not buildspec:
            raise BuildTestError(
                "Invalid input type for Buildspec, must be of type 'string'.")

        self.buildspec = resolve_path(buildspec)

        if not self.buildspec:
            raise BuildTestError("There is no file named: %s " % buildspec)

        if is_dir(self.buildspec):
            raise BuildTestError(
                f"Detected {self.buildspec} is a directory, please provide a file path (not a directory path) to BuildspecParser."
            )

        self.recipe = load_recipe(self.buildspec)
        # ensure self.recipe exists after loading recipe
        assert self.recipe

        # validate each schema defined in the recipes
        self._validate()
Ejemplo n.º 6
0
import coverage
import pytest
import os
import shutil
import sys

from buildtest.defaults import BUILDTEST_USER_HOME, var_root
from buildtest.utils.file import is_dir

if not os.getenv("BUILDTEST_ROOT"):
    sys.exit(
        "Please check your buildtest installation by running 'source setup.sh'"
    )

html_dir = os.path.join(os.getenv("BUILDTEST_ROOT"), "htmlcov")
if is_dir(var_root):
    shutil.rmtree(var_root)

if is_dir(BUILDTEST_USER_HOME):
    shutil.rmtree(BUILDTEST_USER_HOME)

cov = coverage.Coverage()
cov.erase()
cov.start()
pytest.main()
cov.stop()
cov.save()
cov.html_report(directory=html_dir)
cov.report()

print("\n\n")
Ejemplo n.º 7
0
def test_directory_expansion():
    dir1 = "$HOME"
    dir2 = "~"

    assert is_dir(dir1)
    assert is_dir(dir2)
Ejemplo n.º 8
0
def test_checking_directory():
    dirname = str(uuid.uuid4())
    assert not is_dir(dirname)