Beispiel #1
0
    def test_get_configured_defaults_raises_on_invalid_keys(
        self, empty_config_mock, students_file
    ):
        invalid_key = "not_valid_key"
        config_contents = os.linesep.join(
            [
                "[{}]".format(_repobee.constants.CORE_SECTION_HDR),
                "base_url = {}".format(BASE_URL),
                "user = {}".format(USER),
                "org_name = {}".format(ORG_NAME),
                "template_org_name = {}".format(TEMPLATE_ORG_NAME),
                "students_file = {!s}".format(students_file),
                "{} = whatever".format(invalid_key),
            ]
        )
        empty_config_mock.write(config_contents)

        with pytest.raises(exception.FileError) as exc_info:
            config.get_configured_defaults(str(empty_config_mock))

        assert "config file at {} contains invalid default keys".format(
            empty_config_mock
        ) in str(exc_info.value)
        assert str(empty_config_mock) in str(exc_info.value)
        assert invalid_key in str(exc_info.value)
Beispiel #2
0
    def test_get_configured_defaults_raises_on_missing_header(
            self, empty_config_mock, students_file):
        config_contents = os.linesep.join([
            f"base_url = {BASE_URL}",
            f"user = {USER}",
            f"org_name = {ORG_NAME}",
            f"students_file = {str(students_file)}",
        ])
        empty_config_mock.write(config_contents)

        with pytest.raises(exception.FileError) as exc_info:
            config.get_configured_defaults(str(empty_config_mock))

        assert "does not contain the required [repobee] header" in str(
            exc_info.value)
Beispiel #3
0
    def test_get_configured_defaults_reads_full_config(
        self, config_mock, students_file, mock_getenv
    ):
        mock_getenv.side_effect = lambda name: None

        defaults = config.get_configured_defaults(str(config_mock))

        assert defaults["user"] == USER
        assert defaults["base_url"] == BASE_URL
        assert defaults["org_name"] == ORG_NAME
        assert defaults["students_file"] == str(students_file)
        assert defaults["template_org_name"] == TEMPLATE_ORG_NAME
        assert defaults["token"] == CONFIG_TOKEN
Beispiel #4
0
    def test_get_configured_defaults_raises_on_invalid_keys(
            self, empty_config_mock, students_file):
        invalid_key = "not_valid_key"
        config_contents = os.linesep.join([
            f"[{_repobee.constants.CORE_SECTION_HDR}]"
            f"base_url = {BASE_URL}",
            f"user = {USER}",
            f"org_name = {ORG_NAME}",
            f"template_org_name = {TEMPLATE_ORG_NAME}",
            f"students_file = {str(students_file)}",
            f"{invalid_key} = whatever",
        ])
        empty_config_mock.write(config_contents)

        with pytest.raises(exception.FileError) as exc_info:
            config.get_configured_defaults(str(empty_config_mock))

        assert (
            f"config file at {empty_config_mock} contains invalid default keys"
        ) in str(exc_info.value)
        assert str(empty_config_mock) in str(exc_info.value)
        assert invalid_key in str(exc_info.value)
Beispiel #5
0
 def get_default(arg_name):
     configured_defaults = config.get_configured_defaults(config_file)
     return configured_defaults.get(arg_name)
Beispiel #6
0
 def test_token_in_env_variable_overrides_configuration_file(
     self, config_mock
 ):
     defaults = config.get_configured_defaults(str(config_mock))
     assert defaults["token"] == constants.TOKEN
Beispiel #7
0
 def test_get_configured_defaults_empty_file(self, empty_config_mock):
     with pytest.raises(exception.FileError) as exc_info:
         config.get_configured_defaults(str(empty_config_mock))
     assert "does not contain the required [repobee] header" in str(
         exc_info.value
     )
Beispiel #8
0
 def test_get_configured_defaults_no_config_file(
     self, isfile_mock, unused_path
 ):
     defaults = config.get_configured_defaults(unused_path)
     assert defaults == dict(token=constants.TOKEN)
Beispiel #9
0
def _create_base_parsers(config_file):
    """Create the base parsers."""
    configured_defaults = config.get_configured_defaults(config_file)

    def default(arg_name):
        return (configured_defaults[arg_name]
                if arg_name in configured_defaults else None)

    def configured(arg_name):
        return arg_name in configured_defaults

    def api_requires(arg_name):
        return arg_name in plug.manager.hook.api_init_requires()

    # API args help sections
    user_help = "your username"
    org_name_help = "name of the target organization"
    base_url_help = (
        "Base url to a platform API. Must be HTTPS. For example, with "
        "github.com, the base url is https://api.github.com, and with "
        "GitHub enterprise, the url is https://<ENTERPRISE_HOST>/api/v3")
    token_help = "access token for the platform instance"

    # other configurable args help sections
    # these should not be checked against the api_requires function
    students_file_help = (
        "path to a list of student usernames or groups of students")
    template_org_help = (
        "name of the organization containing the template repos "
        "(defaults to the same value as `-o|--org-name`)")

    base_parser = RepobeeParser(is_core_command=True, add_help=False)
    base_parser.add_argument(
        "-u",
        "--user",
        help=user_help,
        type=str,
        required=not configured("user") and api_requires("user"),
        default=default("user"),
    )

    base_parser.add_argument(
        "-o",
        "--org-name",
        help=org_name_help,
        type=str,
        required=not configured("org_name") and api_requires("org_name"),
        default=default("org_name"),
    )
    base_parser.add_argument(
        "--bu",
        "--base-url",
        help=base_url_help,
        type=str,
        required=not configured("base_url") and api_requires("base_url"),
        default=default("base_url"),
        dest="base_url",
    )
    base_parser.add_argument(
        "-t",
        "--token",
        help=token_help,
        type=str,
        required=not configured("token") and api_requires("token"),
        default=default("token"),
    )

    _add_debug_args(base_parser)
    # base parser for when student lists are involved
    base_student_parser = RepobeeParser(is_core_command=True, add_help=False)
    students = base_student_parser.add_argument_group(
        "core").add_mutually_exclusive_group(
            required=not configured("students_file"))
    students.add_argument(
        "--sf",
        "--students-file",
        help=students_file_help,
        type=str,
        default=default("students_file"),
        dest="students_file",
    )
    students.add_argument(
        "-s",
        "--students",
        help="One or more whitespace separated student usernames.",
        type=str,
        nargs="+",
    )

    template_org_parser = RepobeeParser(is_core_command=True, add_help=False)
    template_org_parser.add_argument(
        "--to",
        "--template-org-name",
        help=template_org_help,
        default=default("template_org_name"),
        dest="template_org_name",
    )

    return (base_parser, base_student_parser, template_org_parser)