def test_config():
    config = {
        "languages": {
            "java": {
                "upstream_templates_dir": "Java",
                "commands": {
                    "pre": [{
                        "commandline": ["some", "cmd"],
                        "description": "Some command"
                    }]
                }
            }
        },
        "user_agent_client_name": "MyClient"
    }

    c = Config.from_dict(config)
    # check a value that was explicitly given
    assert c.user_agent_client_name == "MyClient"
    # check a value that should have a default
    assert c.codegen_exec == "openapi-generator"

    with pytest.raises(KeyError):
        c.unknown

    java = c.get_language_config("java")
    assert type(java) == LanguageConfig
    assert java.upstream_templates_dir == "Java"

    pre = java.get_stage_commands("pre")[0]
    assert type(pre) == LanguageCommand
    assert pre.description == "Some command"
    assert pre.commandline == ["some", "cmd"]
Example #2
0
def test_command_get_generated_lang_dir():
    args = flexmock(generated_code_dir="/some/path")
    config = Config.from_dict(
        {"languages": {
            "java": {
                "github_repo_name": "repo-name"
            }
        }})
    mc = MyCommand(config=config, args=args)
    assert mc.get_generated_lang_dir("java") == "/some/path/repo-name"
Example #3
0
def test_command_get_generated_lang_version_dir():
    args = flexmock(generated_code_dir="/some/path")
    config = Config.from_dict({
        "languages": {
            "java": {
                "github_repo_name": "repo-name",
                "version_path_template": "{{spec_version}}"
            }
        }
    })
    mc = MyCommand(config=config, args=args)
    assert mc.get_generated_lang_version_dir("java",
                                             "v1") == "/some/path/repo-name/v1"
Example #4
0
def cli():
    toplog = logging.getLogger(__name__.split(".")[0])
    set_log(toplog)
    args = get_cli_parser().parse_args()
    if args.verbose:
        set_log_level(toplog, logging.DEBUG)

    command_class = all_commands[args.action]
    command = command_class({}, args)
    if args.action == "init":
        sys.exit(command.run())

    with change_cwd(args.spec_repo_dir):
        command.config = Config.from_file(os.path.join(args.config_dir, constants.DEFAULT_CONFIG_FILE))
        sys.exit(command.run())
Example #5
0
def run_command_with_config(command_class, click_ctx, **kwargs):
    click_ctx.obj.update(kwargs)
    cmd = command_class({}, click_ctx.obj)

    with change_cwd(click_ctx.obj.get("spec_repo_dir")):
        configfile = os.path.join(
            os.path.join(constants.SPEC_REPO_CONFIG_DIR,
                         constants.DEFAULT_CONFIG_FILE))
        try:
            cmd.config = Config.from_file(configfile)
        except OSError:
            check_for_legacy_config(click_ctx, configfile)
        try:
            click_ctx.exit(cmd.run())
        except errors.ApigentoolsError as e:
            log.error("Apigentools error: %s", e)
        except subprocess.CalledProcessError as e:
            log.error("Failed running subprocess: %s", e.cmd)
            log.error(fmt_cmd_out_for_log(e, False))
            click_ctx.exit(1)
                "v1": [],
                "v2": []
            },
            "library_version": "1.0.0",
        },
        "test-lang2": {
            "spec_versions": ["v1"],
            "library_version": "1.0.0"
        },
    },
    "spec_sections": {
        "v1": ["x.yaml"],
        "v2": ["y.yaml"]
    },
}
SPEC_CONFIG_OBJ = Config.from_dict(SPEC_CONFIG)


@pytest.fixture
def setup_spec():
    with tempfile.NamedTemporaryFile("w+") as fp:
        fp.write(json.dumps(SPEC_CONFIG))
        fp.flush()
        yield fp.name


def test_config(setup_spec, capsys):
    # Setup the arguments (The CLI tooling would do this, but we're testing the command
    # after that gets setup
    args = {"spec_dir": setup_spec, "full_spec_file": "full_spec.yaml"}
Example #7
0
def test_config_from_file():
    c = Config.from_file(os.path.join(FIXTURE_DIR,"good_config_json.json"))
    assert c.user_agent_client_name == "MyClient"
    assert c.codegen_exec == "openapi-generator"
Example #8
0
def test_config_from_file():
    c = Config.from_file(os.path.join(FIXTURE_DIR, "good_config_yaml.yaml"))
    check_config(c)
Example #9
0
def test_config_from_dict():
    c = Config.from_dict(config_sample)
    check_config(c)