コード例 #1
0
def validate(data, set_dir):
    def is_file(path):
        return path.is_file()

    def to_fixture(data):
        return Fixture(**data)

    def to_step(data):
        return Step(**data)

    def to_test(data):
        return Test(**data)

    def absolute_path(path):
        absolute = Path(os.path.expanduser(path))
        if not absolute.is_absolute():
            absolute = (set_dir / path).resolve()
        return absolute

    def to_command(raw_command):
        return shlex.split(raw_command.replace('@.', str(set_dir)))

    fixture = schema.Schema(
        schema.And(
            {
                'enter': schema.And(str, len),
                'exit': schema.And(str, len)
            }, schema.Use(to_fixture)))
    fixtures = schema.Schema({schema.And(str, len): fixture})
    step = schema.Schema(
        schema.And(
            {
                'command':
                schema.And(schema.Const(schema.And(str, len)),
                           schema.Use(to_command)),
                schema.Optional('input', default=None):
                schema.And(schema.Use(absolute_path), is_file)
            }, schema.Use(to_step)))
    test = schema.Schema(
        schema.And(
            {
                schema.Optional('tags', default=None): [str],
                schema.Optional('condition', default=None): str,
                schema.Optional('fixture', default=None): str,
                'steps': [step]
            }, schema.Use(to_test)))
    tests = schema.Schema({schema.And(str, len): test})
    sch = schema.Schema({
        schema.Optional('config-file', default=None):
        schema.Use(absolute_path),
        schema.Optional('fixtures', default=None):
        fixtures,
        'tests':
        tests
    })
    return sch.validate(data)
コード例 #2
0
def validate(data):
    def is_file(path):
        return path.is_file()

    def to_fixture(data):
        return Fixture(**data)

    def to_step(data):
        return Step(**data)

    def guard_to_condition(guard):
        return Condition(guard["guard"])

    def to_test(data):
        data["config_file"] = data.pop("config-file")
        return Test(**data)

    def absolute_path(path):
        absolute = Path(os.path.expanduser(path))
        if not absolute.is_absolute():
            absolute = (SET_DIR / path).resolve()
        return absolute

    def replace_path(raw_command):
        return raw_command.replace("@.", str(SET_DIR))

    def to_command(raw_command):
        return shlex.split(replace_path(raw_command))

    def to_result(raw_result):
        return Result[raw_result.upper()]

    fixture = schema.Schema(
        schema.And(
            {
                "enter": schema.And(str, len),
                "exit": schema.And(str, len)
            },
            schema.Use(to_fixture),
        ))
    fixtures = schema.Schema({schema.And(str, len): fixture})
    step = schema.Schema(
        schema.And(
            {
                "command":
                schema.And(schema.Const(schema.And(str, len)),
                           schema.Use(to_command)),
                schema.Optional("input", default=None):
                schema.And(schema.Use(absolute_path), is_file),
                schema.Optional("transformation", default=None):
                schema.Use(replace_path),
                schema.Optional("expected_result", default=Result.SUCCESS):
                schema.Use(to_result),
            },
            schema.Use(to_step),
        ))
    guard = schema.Schema(
        schema.And({"guard": str}, schema.Use(guard_to_condition)))
    test = schema.Schema(
        schema.And(
            {
                schema.Optional("tags", default=None): [str],
                schema.Optional("condition", default=None):
                schema.Use(Condition),
                schema.Optional("config-file", default=None):
                schema.Use(absolute_path),
                schema.Optional("fixture", default=None):
                str,
                "steps": [schema.Or(step, guard)],
            },
            schema.Use(to_test),
        ))
    tests = schema.Schema({schema.And(str, len): test})
    sch = schema.Schema({
        schema.Optional("config-file", default=None):
        schema.Use(absolute_path),
        schema.Optional("fixtures", default=None):
        fixtures,
        "tests":
        tests,
    })
    return sch.validate(data)