Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase test coverage for BuildtestCompilers class and "buildtest inspect" command #575

Merged
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
4 changes: 2 additions & 2 deletions buildtest/menu/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ class BuildtestCompilers:
"cuda": {"cc": "nvcc", "cxx": "nvcc", "fc": None,},
}

def __init__(self, debug=False):
def __init__(self, debug=False, settings_file=None):
"""
:param compilers: compiler section from buildtest configuration.
:type compilers: dict
"""

self.configuration = load_settings()
self.configuration = load_settings(settings_file)
self.debug = debug

if not deep_get(self.configuration, "compilers", "compiler"):
Expand Down
16 changes: 5 additions & 11 deletions buildtest/menu/config.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import getpass
import os
import shutil
import sys
import yaml
from jsonschema import ValidationError
from buildtest import BUILDTEST_VERSION
from buildtest.schemas.utils import get_schema_fullpath, load_recipe
from buildtest.schemas.utils import load_recipe
from buildtest.config import check_settings, load_settings, resolve_settings_file
from buildtest.defaults import (
BUILDTEST_SETTINGS_FILE,
BUILDSPEC_CACHE_FILE,
)
from buildtest.defaults import supported_type_schemas, supported_schemas
from buildtest.defaults import supported_schemas
from buildtest.system import BuildTestSystem


Expand All @@ -37,9 +36,11 @@ def func_config_view(args=None):
"""View buildtest configuration file. This implements ``buildtest config view``"""

settings_file = resolve_settings_file()
print(f"Settings File: {settings_file}")
print("{:_<80}".format(""))
content = load_recipe(settings_file)

print(yaml.safe_dump(content, sys.stdout, sort_keys=False))
print(yaml.dump(content, default_flow_style=False, sort_keys=False))


def func_config_summary(args=None):
Expand Down Expand Up @@ -93,10 +94,3 @@ def func_config_summary(args=None):
print("Buildtest Schemas")
print("{:_<80}".format(""))
print("Available Schemas:", supported_schemas)
print("Supported Sub-Schemas")
print("{:_<80}".format(""))
for schema in supported_type_schemas:
path = get_schema_fullpath(schema)
print(schema, ":", path)
examples_dir = os.path.join(os.path.dirname(path), "examples")
print("Examples Directory for schema: ", examples_dir)
14 changes: 0 additions & 14 deletions buildtest/schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,3 @@ def load_recipe(path):
with open(path, "r") as fd:
content = yaml.load(fd.read(), Loader=yaml.SafeLoader)
return content


def get_schema_fullpath(schema_file, name=None):
"""Return the full path of a schema file

:param schema_file: the path to the schema file.
:type schema_file: str
:param name: the schema type. If not provided, derived from filename.
:type name: str, optional
"""
if not name:
name = schema_file.split("-v", 1)[0]
schema_file = os.path.join(here, schema_file)
return schema_file
8 changes: 5 additions & 3 deletions tests/menu/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class args:
func_build_subcmd(args, buildtest_configuration)


def test_discover_by_buildspecs():
def test_discover_by_buildspecs(tmpdir):

# test single buildspec file
buildspec = os.path.join(valid_buildspecs, "environment.yml")
Expand All @@ -88,9 +88,11 @@ def test_discover_by_buildspecs():
# invalid file extension must be of type .yml
assert not discover_by_buildspecs(os.path.join(root, "README.rst"))

print(
f"Searching for buildspecs in directory: {tmpdir} which should have no .yml files"
)
# when no Buildspec files found in a valid directory
# searching for all Buildspecs in current directory
assert not discover_by_buildspecs(os.path.dirname(os.path.abspath(__file__)))
assert not discover_by_buildspecs(tmpdir)

invalid_file = str(uuid.uuid4())
assert not discover_by_buildspecs(invalid_file)
Expand Down
37 changes: 37 additions & 0 deletions tests/menu/test_compilers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import pytest
from buildtest.menu.compilers import BuildtestCompilers
from buildtest.exceptions import BuildTestError

here = os.path.dirname(os.path.abspath(__file__))


class TestBuildtestCompilers:

bc = BuildtestCompilers()

def test_init(self):

assert hasattr(self.bc, "compilers")
assert isinstance(self.bc.names, list)

def test_print(self):
self.bc.print_yaml()
self.bc.print_json()
self.bc.print_compilers()

def test_invalid_moduletool(self):
settings_file = os.path.join(here, "test_compilers", "invalid_moduletool.yml")
print(f"Using settings file: {settings_file} for loading compilers")
bc = BuildtestCompilers(settings_file=settings_file)
with pytest.raises(BuildTestError):
bc.find_compilers()

def test_missing_compiler_find(self):
settings_file = os.path.join(
here, "test_compilers", "missing_compiler_find.yml"
)
print(f"Using settings file: {settings_file} for loading compilers")
bc = BuildtestCompilers(settings_file=settings_file)
with pytest.raises(BuildTestError):
bc.find_compilers()
26 changes: 26 additions & 0 deletions tests/menu/test_compilers/invalid_moduletool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
moduletool: N/A
load_default_buildspecs: True
executors:
local:
bash:
description: submit jobs on local machine using bash shell
shell: bash

sh:
description: submit jobs on local machine using sh shell
shell: sh

csh:
description: submit jobs on local machine using csh shell
shell: csh

python:
description: submit jobs on local machine using python shell
shell: python
compilers:
compiler:
gcc:
builtin_gcc:
cc: /usr/bin/gcc
fc: /usr/bin/gfortran
cxx: /usr/bin/g++
26 changes: 26 additions & 0 deletions tests/menu/test_compilers/missing_compiler_find.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
moduletool: lmod
load_default_buildspecs: True
executors:
local:
bash:
description: submit jobs on local machine using bash shell
shell: bash

sh:
description: submit jobs on local machine using sh shell
shell: sh

csh:
description: submit jobs on local machine using csh shell
shell: csh

python:
description: submit jobs on local machine using python shell
shell: python
compilers:
compiler:
gcc:
builtin_gcc:
cc: /usr/bin/gcc
fc: /usr/bin/gfortran
cxx: /usr/bin/g++
17 changes: 17 additions & 0 deletions tests/menu/test_inspect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from buildtest.menu.inspect import get_all_ids, func_inspect


def test_inspect_ids():

test_ids = get_all_ids()
# return should be a list of test ids
assert isinstance(test_ids, list)
print(test_ids)

# check if we have atleast one item and its test id is a string
assert isinstance(test_ids[0], str) and len(test_ids) > 0

class args:
test = test_ids[0]

func_inspect(args)
32 changes: 26 additions & 6 deletions tests/schemas/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import pytest
import random
import string

import tempfile

from buildtest.schemas.utils import load_schema, load_recipe

Expand All @@ -19,10 +17,32 @@ def test_load_schema_invalid_ext():
load_schema(os.path.join(root, "README.rst"))


@pytest.mark.utility
@pytest.mark.xfail(reason="Invalid File Path for loading schema", raises=SystemExit)
def test_load_schema_invalid_path():
fp = tempfile.NamedTemporaryFile()
assert os.path.exists(fp.name)
fp.close()

print(f"Loading Schema from file: {fp.name}")
# tempfile will remove file upon closing file stream so we expect load_schema to raise error when we have invalid file
load_schema(fp.name)


@pytest.mark.utility
@pytest.mark.xfail(reason="Invalid File Path when loading recipe", raises=SystemExit)
def test_load_recipe_invalid_path():

invalid_file = "".join(random.choice(string.ascii_letters) for i in range(10))
print(invalid_file, type(invalid_file))
load_recipe(invalid_file)
fp = tempfile.NamedTemporaryFile()
assert os.path.exists(fp.name)
fp.close()

print(f"Loading YAML recipe from file: {fp.name}")

load_recipe(fp.name)


@pytest.mark.utility
@pytest.mark.xfail(reason="YAML File must end in .yml extension", raises=SystemExit)
def test_load_recipe_extension():
load_recipe(os.path.join(root, "README.rst"))