Repository: pytest-dev/pytest
File: testing/test_parseopt.py (Python)

Code before:
        with pytest.raises(ValueError):
            group.addoption("-x", action="store_true")
        assert len(group.options) == 0
        group._addoption("-x", action="store_true")
        assert len(group.options) == 1

    def test_parser_addoption(self, parser: parseopt.Parser) -> None:
        group = parser.getgroup("custom options")
        assert len(group.options) == 0
        group.addoption("--option1", action="store_true")
        assert len(group.options) == 1

    def test_parse(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", dest="hello", action="store")
        args = parser.parse(["--hello", "world"])
        assert args.hello == "world"
        assert not getattr(args, parseopt.FILE_OR_DIR)

    def test_parse2(self, parser: parseopt.Parser) -> None:
        args = parser.parse([Path(".")])
        assert getattr(args, parseopt.FILE_OR_DIR)[0] == "."

    @pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
    def test_parse_from_file(self, parser: parseopt.Parser, tmp_path: Path) -> None:
        tests = [".", "some.py::Test::test_method[param0]", "other/test_file.py"]
        args_file = tmp_path / "tests.txt"
        args_file.write_text("\n".join(tests), encoding="utf-8")
        args = parser.parse([f"@{args_file.absolute()}"])
        assert getattr(args, parseopt.FILE_OR_DIR) == tests

    def test_parse_known_args(self, parser: parseopt.Parser) -> None:
        parser.parse_known_args([Path(".")])
        parser.addoption("--hello", action="store_true")
        ns = parser.parse_known_args(["x", "--y", "--hello", "this"])
        assert ns.hello
        assert ns.file_or_dir == ["x"]

    def test_parse_known_and_unknown_args(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", action="store_true")
        ns, unknown = parser.parse_known_and_unknown_args(
            ["x", "--y", "--hello", "this"]
        )
        assert ns.hello
        assert ns.file_or_dir == ["x"]
        assert unknown == ["--y", "this"]

    def test_parse_will_set_default(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", dest="hello", default="x", action="store")
        option = parser.parse([])
        assert option.hello == "x"
        del option.hello

Review comment (suggestion):
```suggestion
    # Warning ignore because of:
    # https://github.com/python/cpython/issues/85308
    # Can be removed once Python<3.12 support is dropped.
    @pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
```

Code after:
        with pytest.raises(ValueError):
            group.addoption("-x", action="store_true")
        assert len(group.options) == 0
        group._addoption("-x", action="store_true")
        assert len(group.options) == 1

    def test_parser_addoption(self, parser: parseopt.Parser) -> None:
        group = parser.getgroup("custom options")
        assert len(group.options) == 0
        group.addoption("--option1", action="store_true")
        assert len(group.options) == 1

    def test_parse(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", dest="hello", action="store")
        args = parser.parse(["--hello", "world"])
        assert args.hello == "world"
        assert not getattr(args, parseopt.FILE_OR_DIR)

    def test_parse2(self, parser: parseopt.Parser) -> None:
        args = parser.parse([Path(".")])
        assert getattr(args, parseopt.FILE_OR_DIR)[0] == "."

    # Warning ignore because of:
    # https://github.com/python/cpython/issues/85308
    # Can be removed once Python<3.12 support is dropped.
    @pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
    def test_parse_from_file(self, parser: parseopt.Parser, tmp_path: Path) -> None:
        tests = [".", "some.py::Test::test_method[param0]", "other/test_file.py"]
        args_file = tmp_path / "tests.txt"
        args_file.write_text("\n".join(tests), encoding="utf-8")
        args = parser.parse([f"@{args_file.absolute()}"])
        assert getattr(args, parseopt.FILE_OR_DIR) == tests

    def test_parse_known_args(self, parser: parseopt.Parser) -> None:
        parser.parse_known_args([Path(".")])
        parser.addoption("--hello", action="store_true")
        ns = parser.parse_known_args(["x", "--y", "--hello", "this"])
        assert ns.hello
        assert ns.file_or_dir == ["x"]

    def test_parse_known_and_unknown_args(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", action="store_true")
        ns, unknown = parser.parse_known_and_unknown_args(
            ["x", "--y", "--hello", "this"]
        )
        assert ns.hello
        assert ns.file_or_dir == ["x"]
        assert unknown == ["--y", "this"]

    def test_parse_will_set_default(self, parser: parseopt.Parser) -> None:
        parser.addoption("--hello", dest="hello", default="x", action="store")