minioj 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
minioj-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MiniOJ contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
minioj-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: minioj
3
+ Version: 0.1.0
4
+ Summary: Run configured exercise stages and report feedback on GitHub pull requests.
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: requests>=2.28
10
+ Dynamic: license-file
11
+
12
+ # MiniOJ
13
+
14
+ MiniOJ reads an exercise configuration, runs its stages, and posts the captured
15
+ exit statuses, stdout, and stderr to a GitHub pull request.
16
+
17
+ ## Install and run
18
+
19
+ Python 3.10 or newer is required. From this checkout:
20
+
21
+ ```sh
22
+ python -m pip install -e .
23
+ minioj --repo owner/repository --pull-request 123 \
24
+ --comment '@minioj-bot run w1/ex1' --token "$GITHUB_TOKEN"
25
+ ```
26
+
27
+ `python -m minioj` and `python main.py` accept the same arguments. `--config`
28
+ defaults to `.minioj/`. When `--repo` is omitted, MiniOJ uses the first Git remote
29
+ in the current working directory.
30
+
31
+ ## Exercise configuration
32
+
33
+ Store each exercise at `.minioj/w<week>/ex<exercise>/conf.json`:
34
+
35
+ ```json
36
+ {
37
+ "name": "Week 1: Hello world",
38
+ "stages": [
39
+ {"name": "Compile", "command": "cc main.c -o solution"},
40
+ {"name": "Run", "command": "./solution", "cased": true}
41
+ ]
42
+ }
43
+ ```
44
+
45
+ An exercise name defaults to `w<week>/ex<exercise>`, and stage names default to
46
+ `Stage <index>` starting at zero. As in the original format, stage entries without
47
+ a string `command` are skipped. Only the JSON boolean `true` enables `cased`.
48
+
49
+ Commands support quoted arguments and run directly without a shell. Pipes,
50
+ redirection, and shell variable expansion require explicitly invoking a shell
51
+ (for example, `sh -c 'echo hello > output.txt'`). Run MiniOJ from the submission
52
+ directory: all stages inherit that working directory and run in config order,
53
+ including stages after a nonzero exit status.
54
+
55
+ A stage normally runs once with closed standard input. A `cased` stage runs once
56
+ for each `case<number>.in` file in the exercise directory, in numeric order. Each
57
+ file supplies stdin, and its matching `case<number>.out` supplies expected stdout
58
+ (for example, `case2.in` pairs with `case2.out`). Feedback numbers the cases from
59
+ zero and shows a unified diff in each case's `stdout` block: `-` lines are expected
60
+ output and `+` lines are actual output. Matching output displays `No differences.`
61
+ Whitespace and trailing newlines matter. If the expected file is missing,
62
+ feedback marks comparison as unavailable and shows the raw stdout instead.
63
+ Process exit status and stderr are reported independently of the comparison.
64
+ Each case ends with `Passed!` or `Failed...` outside its details block. Passing
65
+ requires a zero exit status and matching expected output; missing expected output
66
+ counts as a failure.
67
+
68
+ Configuration commands execute with the permissions of the MiniOJ process. Use
69
+ a separate sandbox when running submissions that you do not trust; this project
70
+ does not implement execution isolation or resource limits.
71
+
72
+ ## Project layout
73
+
74
+ | Module | Responsibility |
75
+ | --- | --- |
76
+ | `minioj/models.py` | Exercise IDs, exercise and stage definitions, command and stage results |
77
+ | `minioj/config.py` | JSON loading, defaults, and configuration errors |
78
+ | `minioj/runner.py` | Case discovery and subprocess execution |
79
+ | `minioj/feedback.py` | Markdown feedback and unified output diffs |
80
+ | `minioj/github.py` | Repository discovery and GitHub comments |
81
+ | `minioj/cli.py` | Arguments and orchestration |
82
+
83
+ The data models are immutable dataclasses. Config parsing produces `Exercise`
84
+ and `Stage` objects; execution produces `StageResult` objects containing
85
+ `CommandResult` objects. Feedback formatting consumes those results without
86
+ performing network or process I/O.
87
+
88
+ ## Tests
89
+
90
+ ```sh
91
+ python -m unittest discover -s tests -v
92
+ ```
93
+
94
+ Tests use temporary exercise directories and local subprocesses. GitHub requests
95
+ are mocked; running the test suite does not publish comments.
96
+
97
+ ## License
98
+
99
+ MIT. See [LICENSE](LICENSE).
minioj-0.1.0/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # MiniOJ
2
+
3
+ MiniOJ reads an exercise configuration, runs its stages, and posts the captured
4
+ exit statuses, stdout, and stderr to a GitHub pull request.
5
+
6
+ ## Install and run
7
+
8
+ Python 3.10 or newer is required. From this checkout:
9
+
10
+ ```sh
11
+ python -m pip install -e .
12
+ minioj --repo owner/repository --pull-request 123 \
13
+ --comment '@minioj-bot run w1/ex1' --token "$GITHUB_TOKEN"
14
+ ```
15
+
16
+ `python -m minioj` and `python main.py` accept the same arguments. `--config`
17
+ defaults to `.minioj/`. When `--repo` is omitted, MiniOJ uses the first Git remote
18
+ in the current working directory.
19
+
20
+ ## Exercise configuration
21
+
22
+ Store each exercise at `.minioj/w<week>/ex<exercise>/conf.json`:
23
+
24
+ ```json
25
+ {
26
+ "name": "Week 1: Hello world",
27
+ "stages": [
28
+ {"name": "Compile", "command": "cc main.c -o solution"},
29
+ {"name": "Run", "command": "./solution", "cased": true}
30
+ ]
31
+ }
32
+ ```
33
+
34
+ An exercise name defaults to `w<week>/ex<exercise>`, and stage names default to
35
+ `Stage <index>` starting at zero. As in the original format, stage entries without
36
+ a string `command` are skipped. Only the JSON boolean `true` enables `cased`.
37
+
38
+ Commands support quoted arguments and run directly without a shell. Pipes,
39
+ redirection, and shell variable expansion require explicitly invoking a shell
40
+ (for example, `sh -c 'echo hello > output.txt'`). Run MiniOJ from the submission
41
+ directory: all stages inherit that working directory and run in config order,
42
+ including stages after a nonzero exit status.
43
+
44
+ A stage normally runs once with closed standard input. A `cased` stage runs once
45
+ for each `case<number>.in` file in the exercise directory, in numeric order. Each
46
+ file supplies stdin, and its matching `case<number>.out` supplies expected stdout
47
+ (for example, `case2.in` pairs with `case2.out`). Feedback numbers the cases from
48
+ zero and shows a unified diff in each case's `stdout` block: `-` lines are expected
49
+ output and `+` lines are actual output. Matching output displays `No differences.`
50
+ Whitespace and trailing newlines matter. If the expected file is missing,
51
+ feedback marks comparison as unavailable and shows the raw stdout instead.
52
+ Process exit status and stderr are reported independently of the comparison.
53
+ Each case ends with `Passed!` or `Failed...` outside its details block. Passing
54
+ requires a zero exit status and matching expected output; missing expected output
55
+ counts as a failure.
56
+
57
+ Configuration commands execute with the permissions of the MiniOJ process. Use
58
+ a separate sandbox when running submissions that you do not trust; this project
59
+ does not implement execution isolation or resource limits.
60
+
61
+ ## Project layout
62
+
63
+ | Module | Responsibility |
64
+ | --- | --- |
65
+ | `minioj/models.py` | Exercise IDs, exercise and stage definitions, command and stage results |
66
+ | `minioj/config.py` | JSON loading, defaults, and configuration errors |
67
+ | `minioj/runner.py` | Case discovery and subprocess execution |
68
+ | `minioj/feedback.py` | Markdown feedback and unified output diffs |
69
+ | `minioj/github.py` | Repository discovery and GitHub comments |
70
+ | `minioj/cli.py` | Arguments and orchestration |
71
+
72
+ The data models are immutable dataclasses. Config parsing produces `Exercise`
73
+ and `Stage` objects; execution produces `StageResult` objects containing
74
+ `CommandResult` objects. Feedback formatting consumes those results without
75
+ performing network or process I/O.
76
+
77
+ ## Tests
78
+
79
+ ```sh
80
+ python -m unittest discover -s tests -v
81
+ ```
82
+
83
+ Tests use temporary exercise directories and local subprocesses. GitHub requests
84
+ are mocked; running the test suite does not publish comments.
85
+
86
+ ## License
87
+
88
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1 @@
1
+ """MiniOJ: run configured exercise stages and report results on GitHub."""
@@ -0,0 +1,5 @@
1
+ from .cli import main
2
+
3
+
4
+ if __name__ == "__main__":
5
+ raise SystemExit(main())
@@ -0,0 +1,68 @@
1
+ """Command-line parsing and orchestration for a single judge run."""
2
+
3
+ import subprocess
4
+ import sys
5
+ from argparse import ArgumentParser
6
+ from collections.abc import Sequence
7
+ from pathlib import Path
8
+
9
+ import requests
10
+
11
+ from .config import ConfigurationError, load_exercise
12
+ from .feedback import construct_feedback, construct_stage_feedback
13
+ from .github import GitHubClient, GitHubError, get_repo
14
+ from .models import ExerciseId
15
+ from .runner import run_exercise
16
+
17
+
18
+ def build_parser() -> ArgumentParser:
19
+ parser = ArgumentParser(description="Judge a program based on the configuration.")
20
+ parser.add_argument("-c", "--config", help="The config path.", default=".minioj/")
21
+ parser.add_argument("-r", "--repo", help="The repo where the triggering PR lives.")
22
+ parser.add_argument(
23
+ "-x",
24
+ "--comment",
25
+ help="The PR comment body that triggers MiniOJ.",
26
+ required=True,
27
+ )
28
+ parser.add_argument(
29
+ "-i",
30
+ "--pull-request",
31
+ type=int,
32
+ help="The ID of the PR that triggers MiniOJ.",
33
+ required=True,
34
+ )
35
+ parser.add_argument(
36
+ "-t", "--token", help="The token for the PR feedback comment.", required=True
37
+ )
38
+ return parser
39
+
40
+
41
+ def main(argv: Sequence[str] | None = None) -> int:
42
+ args = build_parser().parse_args(argv)
43
+ try:
44
+ client = GitHubClient(get_repo(args.repo), args.token)
45
+ exercise_id = ExerciseId.from_comment(args.comment)
46
+ try:
47
+ exercise = load_exercise(Path(args.config), exercise_id)
48
+ except ConfigurationError as error:
49
+ body = construct_feedback(
50
+ (error.title, str(error)),
51
+ append_title=str(error.exercise) if error.exercise is not None else "",
52
+ )
53
+ else:
54
+ results = run_exercise(exercise)
55
+ body = construct_feedback(
56
+ *construct_stage_feedback(*results), append_title=exercise.name
57
+ )
58
+ client.send_comment(args.pull_request, body)
59
+ except (
60
+ OSError,
61
+ ValueError,
62
+ subprocess.SubprocessError,
63
+ requests.RequestException,
64
+ GitHubError,
65
+ ) as error:
66
+ print(f"MiniOJ: {error}", file=sys.stderr)
67
+ return 1
68
+ return 0
@@ -0,0 +1,72 @@
1
+ """Read the existing .minioj/wN/exN/conf.json configuration format."""
2
+
3
+ import json
4
+ from pathlib import Path
5
+
6
+ from .models import Exercise, ExerciseId, Stage
7
+
8
+
9
+ class ConfigurationError(ValueError):
10
+ """A configuration problem that can be reported in PR feedback."""
11
+
12
+ def __init__(
13
+ self,
14
+ title: str,
15
+ message: str = "Contact admins to setup OJ config.",
16
+ exercise: ExerciseId | None = None,
17
+ ) -> None:
18
+ super().__init__(message)
19
+ self.title = title
20
+ self.exercise = exercise
21
+
22
+
23
+ def load_exercise(config_root: Path, exercise_id: ExerciseId) -> Exercise:
24
+ if not config_root.is_dir():
25
+ raise ConfigurationError("OJ config not found")
26
+
27
+ directory = config_root / str(exercise_id)
28
+ if not directory.is_dir():
29
+ raise ConfigurationError(
30
+ "Exercise not found",
31
+ "Check if you typed the right exercise.",
32
+ exercise_id,
33
+ )
34
+
35
+ config_file = directory / "conf.json"
36
+ if not config_file.is_file():
37
+ raise ConfigurationError("Exercise config not found", exercise=exercise_id)
38
+
39
+ try:
40
+ with config_file.open(encoding="utf-8") as config_stream:
41
+ config = json.load(config_stream)
42
+ except (OSError, UnicodeError, ValueError) as error:
43
+ raise ConfigurationError(
44
+ "Exercise config error", exercise=exercise_id
45
+ ) from error
46
+
47
+ if not isinstance(config, dict) or not isinstance(config.get("stages"), list):
48
+ raise ConfigurationError("Exercise config error", exercise=exercise_id)
49
+
50
+ name = config.get("name")
51
+ if not isinstance(name, str):
52
+ name = str(exercise_id)
53
+
54
+ stages = []
55
+ for index, raw_stage in enumerate(config["stages"]):
56
+ if not isinstance(raw_stage, dict):
57
+ continue
58
+ command = raw_stage.get("command")
59
+ if not isinstance(command, str):
60
+ continue
61
+ stage_name = raw_stage.get("name")
62
+ if not isinstance(stage_name, str):
63
+ stage_name = f"Stage {index}"
64
+ stages.append(
65
+ Stage(
66
+ name=stage_name,
67
+ command=command,
68
+ cased=raw_stage.get("cased") is True,
69
+ )
70
+ )
71
+
72
+ return Exercise(exercise_id, name, directory, tuple(stages))
@@ -0,0 +1,72 @@
1
+ """Render model objects as Markdown without performing I/O."""
2
+
3
+ from difflib import unified_diff
4
+ from pathlib import Path
5
+
6
+ from .models import CommandResult, StageResult
7
+
8
+
9
+ def construct_feedback(*sections: tuple[str, str], append_title: str = "") -> str:
10
+ body = "## MiniOJ feedback"
11
+ if append_title:
12
+ body += f" - {append_title}"
13
+ body += "\n\nMade with :heart: by FTC Team FFFabulous 19726.\n\n"
14
+ for title, section_body in sections:
15
+ body += f"### {title}\n\n{section_body}\n\n"
16
+ return body
17
+
18
+
19
+ def construct_output_diff(expected: str, actual: str, expected_name: str) -> str:
20
+ lines = unified_diff(
21
+ expected.splitlines(keepends=True),
22
+ actual.splitlines(keepends=True),
23
+ fromfile=expected_name,
24
+ tofile="stdout",
25
+ )
26
+ # difflib leaves unterminated lines as-is; separate them and mark the missing
27
+ # newline just as diff -u does, so a trailing-newline mismatch is visible.
28
+ return "".join(
29
+ line if line.endswith("\n") else f"{line}\n\\n"
30
+ for line in lines
31
+ )
32
+
33
+
34
+ def construct_result_feedback(result: CommandResult) -> str:
35
+ stdout = result.stdout
36
+ language = ""
37
+ note = ""
38
+ if result.case_name is not None:
39
+ expected_name = Path(result.case_name).with_suffix(".out").name
40
+ if result.expected_stdout is None:
41
+ note = (
42
+ f"Expected output missing: `{expected_name}`. "
43
+ "Comparison unavailable.\n\n"
44
+ )
45
+ else:
46
+ language = "diff"
47
+ stdout = construct_output_diff(
48
+ result.expected_stdout, result.stdout, expected_name
49
+ ) or "No differences."
50
+ return (
51
+ f"Status: {result.returncode}\n\n"
52
+ f"<details><summary>Details:</summary>\n\n"
53
+ f"`stdout`:\n\n{note}```{language}\n{stdout}\n```\n\n"
54
+ f"`stderr`: \n\n```\n{result.stderr}\n```\n\n"
55
+ f"</details>\n\n"
56
+ )
57
+
58
+
59
+ def construct_stage_feedback(*stages: StageResult) -> list[tuple[str, str]]:
60
+ sections = []
61
+ for result in stages:
62
+ body = ""
63
+ for index, command in enumerate(result.commands):
64
+ if result.stage.cased:
65
+ body += f"**Case {index}:**\n\n"
66
+ body += construct_result_feedback(command)
67
+ if result.stage.cased:
68
+ passed = command.returncode == 0 and command.output_matches is True
69
+ verdict = "Passed!" if passed else "Failed..."
70
+ body += f"{verdict}\n\n"
71
+ sections.append((result.stage.name, body))
72
+ return sections
@@ -0,0 +1,72 @@
1
+ """Resolve the target repository and publish feedback through GitHub."""
2
+
3
+ import re
4
+ import subprocess
5
+
6
+ import requests
7
+
8
+
9
+ class NoRemoteFoundError(ValueError):
10
+ """No repository could be inferred from Git remotes."""
11
+
12
+
13
+ class GitHubError(RuntimeError):
14
+ def __init__(self, status_code: int) -> None:
15
+ super().__init__(f"GitHub rejected the feedback comment (HTTP {status_code}).")
16
+ self.status_code = status_code
17
+
18
+
19
+ class NotAllowedError(GitHubError):
20
+ pass
21
+
22
+
23
+ class ResourceNotFoundError(GitHubError):
24
+ pass
25
+
26
+
27
+ def get_repo(repo_hint: str | None) -> str:
28
+ if repo_hint is not None:
29
+ return repo_hint
30
+ remotes = subprocess.run(
31
+ ["git", "remote"], capture_output=True, text=True, check=True
32
+ ).stdout.splitlines()
33
+ if not remotes:
34
+ raise NoRemoteFoundError("No Git remote found; supply --repo owner/repository.")
35
+ remote_url = subprocess.run(
36
+ ["git", "remote", "get-url", remotes[0]],
37
+ capture_output=True,
38
+ text=True,
39
+ check=True,
40
+ ).stdout.strip()
41
+ match = re.search(r"[:/]([^/:]+/[^/]+?)(?:\.git)?/?$", remote_url)
42
+ if match is None:
43
+ raise NoRemoteFoundError(
44
+ "Cannot determine the repository from Git; supply --repo owner/repository."
45
+ )
46
+ return match[1]
47
+
48
+
49
+ class GitHubClient:
50
+ def __init__(self, repo: str, token: str) -> None:
51
+ self.repo = repo
52
+ self._token = token
53
+
54
+ def send_comment(self, pull_request: int, body: str) -> None:
55
+ url = f"https://api.github.com/repos/{self.repo}/issues/{pull_request}/comments"
56
+ response = requests.post(
57
+ url,
58
+ headers={
59
+ "Accept": "application/vnd.github+json",
60
+ "Authorization": f"Bearer {self._token}",
61
+ },
62
+ json={"body": body},
63
+ timeout=30,
64
+ )
65
+ if response.status_code in (403, 422):
66
+ raise NotAllowedError(response.status_code)
67
+ if response.status_code in (404, 410):
68
+ raise ResourceNotFoundError(response.status_code)
69
+ response.raise_for_status()
70
+ if response.status_code != 201:
71
+ raise GitHubError(response.status_code)
72
+ print(f"Sent comment to {url}")
@@ -0,0 +1,66 @@
1
+ """Data shared by configuration, execution, and feedback."""
2
+
3
+ import re
4
+ from dataclasses import dataclass
5
+ from pathlib import Path
6
+
7
+
8
+ class ExerciseNotFoundError(ValueError):
9
+ """The triggering comment does not identify an exercise."""
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class ExerciseId:
14
+ week: int
15
+ exercise: int
16
+
17
+ def __str__(self) -> str:
18
+ return f"w{self.week}/ex{self.exercise}"
19
+
20
+ @classmethod
21
+ def from_comment(cls, comment: str) -> "ExerciseId":
22
+ for line in comment.lower().splitlines():
23
+ if "@minioj-bot" not in line:
24
+ continue
25
+ match = re.search(r"run w([0-9]+)/ex([0-9]+)", line)
26
+ if match is not None:
27
+ return cls(week=int(match[1]), exercise=int(match[2]))
28
+ raise ExerciseNotFoundError(
29
+ "No exercise found; mention @minioj-bot and use 'run w1/ex1'."
30
+ )
31
+
32
+
33
+ @dataclass(frozen=True)
34
+ class Stage:
35
+ name: str
36
+ command: str
37
+ cased: bool = False
38
+
39
+
40
+ @dataclass(frozen=True)
41
+ class Exercise:
42
+ id: ExerciseId
43
+ name: str
44
+ directory: Path
45
+ stages: tuple[Stage, ...]
46
+
47
+
48
+ @dataclass(frozen=True)
49
+ class CommandResult:
50
+ returncode: int
51
+ stdout: str
52
+ stderr: str
53
+ case_name: str | None = None
54
+ expected_stdout: str | None = None
55
+
56
+ @property
57
+ def output_matches(self) -> bool | None:
58
+ if self.expected_stdout is None:
59
+ return None
60
+ return self.stdout == self.expected_stdout
61
+
62
+
63
+ @dataclass(frozen=True)
64
+ class StageResult:
65
+ stage: Stage
66
+ commands: tuple[CommandResult, ...]
@@ -0,0 +1,68 @@
1
+ """Execute stages in order, optionally once per exercise input file."""
2
+
3
+ import re
4
+ import shlex
5
+ import subprocess
6
+ from pathlib import Path
7
+
8
+ from .models import CommandResult, Exercise, Stage, StageResult
9
+
10
+
11
+ def find_case_files(directory: Path) -> tuple[Path, ...]:
12
+ cases = [
13
+ candidate
14
+ for candidate in directory.iterdir()
15
+ if candidate.is_file() and re.fullmatch(r"case[0-9]+\.in", candidate.name)
16
+ ]
17
+ return tuple(sorted(cases, key=lambda case: (int(case.stem[4:]), case.name)))
18
+
19
+
20
+ def run_stage(stage: Stage, directory: Path) -> StageResult:
21
+ # Commands use shell-style quoting for arguments, but do not start a shell.
22
+ command = shlex.split(stage.command)
23
+ if not command:
24
+ raise ValueError(f"Stage {stage.name!r} has an empty command.")
25
+
26
+ results = []
27
+ if stage.cased:
28
+ for case_file in find_case_files(directory):
29
+ expected_file = case_file.with_suffix(".out")
30
+ expected_stdout = (
31
+ expected_file.read_text(encoding="utf-8")
32
+ if expected_file.is_file()
33
+ else None
34
+ )
35
+ with case_file.open(encoding="utf-8") as case_input:
36
+ process = subprocess.run(
37
+ command,
38
+ stdin=case_input,
39
+ capture_output=True,
40
+ text=True,
41
+ check=False,
42
+ )
43
+ results.append(
44
+ CommandResult(
45
+ process.returncode,
46
+ process.stdout,
47
+ process.stderr,
48
+ case_file.name,
49
+ expected_stdout,
50
+ )
51
+ )
52
+ else:
53
+ process = subprocess.run(
54
+ command,
55
+ stdin=subprocess.DEVNULL,
56
+ capture_output=True,
57
+ text=True,
58
+ check=False,
59
+ )
60
+ results.append(
61
+ CommandResult(process.returncode, process.stdout, process.stderr)
62
+ )
63
+
64
+ return StageResult(stage, tuple(results))
65
+
66
+
67
+ def run_exercise(exercise: Exercise) -> tuple[StageResult, ...]:
68
+ return tuple(run_stage(stage, exercise.directory) for stage in exercise.stages)
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: minioj
3
+ Version: 0.1.0
4
+ Summary: Run configured exercise stages and report feedback on GitHub pull requests.
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: requests>=2.28
10
+ Dynamic: license-file
11
+
12
+ # MiniOJ
13
+
14
+ MiniOJ reads an exercise configuration, runs its stages, and posts the captured
15
+ exit statuses, stdout, and stderr to a GitHub pull request.
16
+
17
+ ## Install and run
18
+
19
+ Python 3.10 or newer is required. From this checkout:
20
+
21
+ ```sh
22
+ python -m pip install -e .
23
+ minioj --repo owner/repository --pull-request 123 \
24
+ --comment '@minioj-bot run w1/ex1' --token "$GITHUB_TOKEN"
25
+ ```
26
+
27
+ `python -m minioj` and `python main.py` accept the same arguments. `--config`
28
+ defaults to `.minioj/`. When `--repo` is omitted, MiniOJ uses the first Git remote
29
+ in the current working directory.
30
+
31
+ ## Exercise configuration
32
+
33
+ Store each exercise at `.minioj/w<week>/ex<exercise>/conf.json`:
34
+
35
+ ```json
36
+ {
37
+ "name": "Week 1: Hello world",
38
+ "stages": [
39
+ {"name": "Compile", "command": "cc main.c -o solution"},
40
+ {"name": "Run", "command": "./solution", "cased": true}
41
+ ]
42
+ }
43
+ ```
44
+
45
+ An exercise name defaults to `w<week>/ex<exercise>`, and stage names default to
46
+ `Stage <index>` starting at zero. As in the original format, stage entries without
47
+ a string `command` are skipped. Only the JSON boolean `true` enables `cased`.
48
+
49
+ Commands support quoted arguments and run directly without a shell. Pipes,
50
+ redirection, and shell variable expansion require explicitly invoking a shell
51
+ (for example, `sh -c 'echo hello > output.txt'`). Run MiniOJ from the submission
52
+ directory: all stages inherit that working directory and run in config order,
53
+ including stages after a nonzero exit status.
54
+
55
+ A stage normally runs once with closed standard input. A `cased` stage runs once
56
+ for each `case<number>.in` file in the exercise directory, in numeric order. Each
57
+ file supplies stdin, and its matching `case<number>.out` supplies expected stdout
58
+ (for example, `case2.in` pairs with `case2.out`). Feedback numbers the cases from
59
+ zero and shows a unified diff in each case's `stdout` block: `-` lines are expected
60
+ output and `+` lines are actual output. Matching output displays `No differences.`
61
+ Whitespace and trailing newlines matter. If the expected file is missing,
62
+ feedback marks comparison as unavailable and shows the raw stdout instead.
63
+ Process exit status and stderr are reported independently of the comparison.
64
+ Each case ends with `Passed!` or `Failed...` outside its details block. Passing
65
+ requires a zero exit status and matching expected output; missing expected output
66
+ counts as a failure.
67
+
68
+ Configuration commands execute with the permissions of the MiniOJ process. Use
69
+ a separate sandbox when running submissions that you do not trust; this project
70
+ does not implement execution isolation or resource limits.
71
+
72
+ ## Project layout
73
+
74
+ | Module | Responsibility |
75
+ | --- | --- |
76
+ | `minioj/models.py` | Exercise IDs, exercise and stage definitions, command and stage results |
77
+ | `minioj/config.py` | JSON loading, defaults, and configuration errors |
78
+ | `minioj/runner.py` | Case discovery and subprocess execution |
79
+ | `minioj/feedback.py` | Markdown feedback and unified output diffs |
80
+ | `minioj/github.py` | Repository discovery and GitHub comments |
81
+ | `minioj/cli.py` | Arguments and orchestration |
82
+
83
+ The data models are immutable dataclasses. Config parsing produces `Exercise`
84
+ and `Stage` objects; execution produces `StageResult` objects containing
85
+ `CommandResult` objects. Feedback formatting consumes those results without
86
+ performing network or process I/O.
87
+
88
+ ## Tests
89
+
90
+ ```sh
91
+ python -m unittest discover -s tests -v
92
+ ```
93
+
94
+ Tests use temporary exercise directories and local subprocesses. GitHub requests
95
+ are mocked; running the test suite does not publish comments.
96
+
97
+ ## License
98
+
99
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,22 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ minioj/__init__.py
5
+ minioj/__main__.py
6
+ minioj/cli.py
7
+ minioj/config.py
8
+ minioj/feedback.py
9
+ minioj/github.py
10
+ minioj/models.py
11
+ minioj/runner.py
12
+ minioj.egg-info/PKG-INFO
13
+ minioj.egg-info/SOURCES.txt
14
+ minioj.egg-info/dependency_links.txt
15
+ minioj.egg-info/entry_points.txt
16
+ minioj.egg-info/requires.txt
17
+ minioj.egg-info/top_level.txt
18
+ tests/test_cli.py
19
+ tests/test_config.py
20
+ tests/test_feedback.py
21
+ tests/test_github.py
22
+ tests/test_runner.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ minioj = minioj.cli:main
@@ -0,0 +1 @@
1
+ requests>=2.28
@@ -0,0 +1 @@
1
+ minioj
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0.3"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "minioj"
7
+ version = "0.1.0"
8
+ description = "Run configured exercise stages and report feedback on GitHub pull requests."
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.10"
13
+ dependencies = ["requests>=2.28"]
14
+
15
+ [project.scripts]
16
+ minioj = "minioj.cli:main"
17
+
18
+ [tool.setuptools.packages.find]
19
+ include = ["minioj*"]
minioj-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,101 @@
1
+ from contextlib import redirect_stderr, redirect_stdout
2
+ from io import StringIO
3
+ import json
4
+ from pathlib import Path
5
+ import shlex
6
+ import subprocess
7
+ import sys
8
+ from tempfile import TemporaryDirectory
9
+ import unittest
10
+ from unittest.mock import patch
11
+
12
+ import requests
13
+
14
+ from minioj.cli import main
15
+
16
+
17
+ class CliTests(unittest.TestCase):
18
+ def setUp(self):
19
+ temporary = TemporaryDirectory()
20
+ self.addCleanup(temporary.cleanup)
21
+ self.root = Path(temporary.name) / ".minioj"
22
+ self.args = [
23
+ "--repo", "owner/repo", "--pull-request", "42", "--token", "test-token",
24
+ "--comment", "@minioj-bot run w1/ex2", "--config", str(self.root),
25
+ ]
26
+
27
+ @patch("minioj.github.requests.post")
28
+ def test_real_config_execution_and_feedback_with_mocked_github(self, post):
29
+ directory = self.root / "w1" / "ex2"
30
+ directory.mkdir(parents=True)
31
+ command = shlex.join([sys.executable, "-c", "import sys; print(sys.stdin.read().upper())"])
32
+ config = {"name": "Integration", "stages": [
33
+ {"name": "Once", "command": command},
34
+ {"name": "Cases", "command": command, "cased": True},
35
+ ]}
36
+ (directory / "conf.json").write_text(json.dumps(config), encoding="utf-8")
37
+ (directory / "case2.in").write_text("second", encoding="utf-8")
38
+ (directory / "case1.in").write_text("first", encoding="utf-8")
39
+ (directory / "case2.out").write_text("SECOND\n", encoding="utf-8")
40
+ (directory / "case1.out").write_text("expected\n", encoding="utf-8")
41
+ response = requests.Response()
42
+ response.status_code = 201
43
+ post.return_value = response
44
+ with redirect_stdout(StringIO()):
45
+ self.assertEqual(main(self.args), 0)
46
+ post.assert_called_once()
47
+ body = post.call_args.kwargs["json"]["body"]
48
+ self.assertTrue(body.startswith("## MiniOJ feedback - Integration"))
49
+ self.assertIn("### Once", body)
50
+ self.assertIn("### Cases", body)
51
+ self.assertEqual(body.count("Status: 0"), 3)
52
+ self.assertIn("```diff\n--- case1.out\n+++ stdout\n@@ -1 +1 @@", body)
53
+ self.assertIn("-expected\n+FIRST\n", body)
54
+ self.assertIn("**Case 1:**", body)
55
+ self.assertIn("```diff\nNo differences.\n```", body)
56
+ self.assertEqual(body.count("</details>\n\nPassed!\n\n"), 1)
57
+ self.assertEqual(body.count("</details>\n\nFailed...\n\n"), 1)
58
+
59
+ @patch("minioj.cli.GitHubClient.send_comment")
60
+ def test_missing_root_posts_configuration_feedback(self, send):
61
+ self.assertEqual(main(self.args), 0)
62
+ send.assert_called_once()
63
+ pull_request, body = send.call_args.args
64
+ self.assertEqual(pull_request, 42)
65
+ self.assertTrue(body.startswith("## MiniOJ feedback\n"))
66
+ self.assertIn("### OJ config not found", body)
67
+
68
+ @patch("minioj.cli.GitHubClient.send_comment")
69
+ def test_invalid_json_posts_exercise_config_feedback(self, send):
70
+ directory = self.root / "w1" / "ex2"
71
+ directory.mkdir(parents=True)
72
+ (directory / "conf.json").write_text("{", encoding="utf-8")
73
+ self.assertEqual(main(self.args), 0)
74
+ body = send.call_args.args[1]
75
+ self.assertTrue(body.startswith("## MiniOJ feedback - w1/ex2"))
76
+ self.assertIn("### Exercise config error", body)
77
+
78
+ @patch("minioj.cli.GitHubClient.send_comment")
79
+ def test_invalid_comment_returns_failure_without_posting(self, send):
80
+ self.args[self.args.index("--comment") + 1] = "@minioj-bot help"
81
+ with redirect_stderr(StringIO()) as error:
82
+ self.assertEqual(main(self.args), 1)
83
+ self.assertIn("No exercise found", error.getvalue())
84
+ send.assert_not_called()
85
+
86
+ @patch("minioj.cli.GitHubClient.send_comment", side_effect=requests.Timeout("timed out"))
87
+ def test_network_failure_returns_nonzero(self, send):
88
+ with redirect_stderr(StringIO()) as error:
89
+ self.assertEqual(main(self.args), 1)
90
+ self.assertIn("timed out", error.getvalue())
91
+
92
+ def test_checkout_entry_points_accept_help(self):
93
+ root = Path(__file__).resolve().parents[1]
94
+ for arguments in ([str(root / "main.py"), "--help"], ["-m", "minioj", "--help"]):
95
+ with self.subTest(arguments=arguments):
96
+ result = subprocess.run(
97
+ [sys.executable, *arguments], cwd=root,
98
+ capture_output=True, text=True, check=True,
99
+ )
100
+ self.assertIn("--pull-request", result.stdout)
101
+ self.assertIn("--config", result.stdout)
@@ -0,0 +1,91 @@
1
+ import json
2
+ from pathlib import Path
3
+ from tempfile import TemporaryDirectory
4
+ import unittest
5
+
6
+ from minioj.config import ConfigurationError, load_exercise
7
+ from minioj.models import ExerciseId, ExerciseNotFoundError, Stage
8
+
9
+
10
+ class ExerciseIdTests(unittest.TestCase):
11
+ def test_parse_first_matching_line_case_insensitively(self):
12
+ exercise = ExerciseId.from_comment(
13
+ "run w9/ex9\n@minioj-bot help\n@MiniOJ-Bot RUN W02/EX03\n"
14
+ "@minioj-bot run w4/ex5"
15
+ )
16
+ self.assertEqual(exercise, ExerciseId(2, 3))
17
+ self.assertEqual(str(exercise), "w2/ex3")
18
+
19
+ def test_require_mention_and_command_on_same_line(self):
20
+ for comment in ("run w1/ex2", "@minioj-bot\nrun w1/ex2", "@minioj-bot help"):
21
+ with self.subTest(comment=comment):
22
+ with self.assertRaises(ExerciseNotFoundError):
23
+ ExerciseId.from_comment(comment)
24
+
25
+
26
+ class ConfigurationTests(unittest.TestCase):
27
+ def setUp(self):
28
+ temporary = TemporaryDirectory()
29
+ self.addCleanup(temporary.cleanup)
30
+ self.root = Path(temporary.name) / ".minioj"
31
+ self.directory = self.root / "w1" / "ex2"
32
+ self.directory.mkdir(parents=True)
33
+ self.config_file = self.directory / "conf.json"
34
+ self.exercise_id = ExerciseId(1, 2)
35
+
36
+ def write_config(self, config):
37
+ self.config_file.write_text(json.dumps(config), encoding="utf-8")
38
+
39
+ def test_load_named_exercise_and_stages(self):
40
+ self.write_config({
41
+ "name": "Hello",
42
+ "stages": [
43
+ {"name": "Compile", "command": "cc main.c -o solution"},
44
+ {"name": "Run", "command": "./solution", "cased": True},
45
+ ],
46
+ })
47
+ exercise = load_exercise(self.root, self.exercise_id)
48
+ self.assertEqual(exercise.id, self.exercise_id)
49
+ self.assertEqual(exercise.name, "Hello")
50
+ self.assertEqual(exercise.directory, self.directory)
51
+ self.assertEqual(exercise.stages, (
52
+ Stage("Compile", "cc main.c -o solution"),
53
+ Stage("Run", "./solution", cased=True),
54
+ ))
55
+
56
+ def test_defaults_and_skipped_entries_preserve_original_indexes(self):
57
+ self.write_config({
58
+ "name": 7,
59
+ "stages": [None, {}, {"command": 1}, {"command": "echo", "name": 7}],
60
+ })
61
+ exercise = load_exercise(self.root, self.exercise_id)
62
+ self.assertEqual(exercise.name, "w1/ex2")
63
+ self.assertEqual(exercise.stages, (Stage("Stage 3", "echo"),))
64
+
65
+ def test_only_boolean_true_enables_cases(self):
66
+ for value in (False, None, 1, "true", []):
67
+ with self.subTest(value=value):
68
+ self.write_config({"stages": [{"command": "echo", "cased": value}]})
69
+ exercise = load_exercise(self.root, self.exercise_id)
70
+ self.assertFalse(exercise.stages[0].cased)
71
+
72
+ def test_missing_paths_have_distinct_feedback(self):
73
+ for root, exercise_id, title, context in (
74
+ (self.root / "missing", self.exercise_id, "OJ config not found", None),
75
+ (self.root, ExerciseId(9, 9), "Exercise not found", ExerciseId(9, 9)),
76
+ (self.root, self.exercise_id, "Exercise config not found", self.exercise_id),
77
+ ):
78
+ with self.subTest(title=title):
79
+ with self.assertRaises(ConfigurationError) as raised:
80
+ load_exercise(root, exercise_id)
81
+ self.assertEqual(raised.exception.title, title)
82
+ self.assertEqual(raised.exception.exercise, context)
83
+
84
+ def test_malformed_json_and_invalid_structure_are_config_errors(self):
85
+ for contents in ("{", "[]", "null", "{}", '{"stages": {}}'):
86
+ with self.subTest(contents=contents):
87
+ self.config_file.write_text(contents, encoding="utf-8")
88
+ with self.assertRaises(ConfigurationError) as raised:
89
+ load_exercise(self.root, self.exercise_id)
90
+ self.assertEqual(raised.exception.title, "Exercise config error")
91
+ self.assertEqual(raised.exception.exercise, self.exercise_id)
@@ -0,0 +1,87 @@
1
+ import unittest
2
+
3
+ from minioj.feedback import (
4
+ construct_feedback,
5
+ construct_output_diff,
6
+ construct_result_feedback,
7
+ construct_stage_feedback,
8
+ )
9
+ from minioj.models import CommandResult, Stage, StageResult
10
+
11
+
12
+ class FeedbackTests(unittest.TestCase):
13
+ def test_markdown_matches_existing_single_run_format(self):
14
+ result = StageResult(Stage("Compile", "cc"), (CommandResult(1, "out", "err"),))
15
+ body = construct_feedback(*construct_stage_feedback(result), append_title="Example")
16
+ self.assertEqual(body, (
17
+ "## MiniOJ feedback - Example\n\n"
18
+ "Made with :heart: by FTC Team FFFabulous 19726.\n\n"
19
+ "### Compile\n\n"
20
+ "Status: 1\n\n"
21
+ "<details><summary>Details:</summary>\n\n"
22
+ "`stdout`:\n\n```\nout\n```\n\n"
23
+ "`stderr`: \n\n```\nerr\n```\n\n"
24
+ "</details>\n\n\n\n"
25
+ ))
26
+
27
+ def test_cased_results_have_independent_numbered_sections(self):
28
+ result = StageResult(Stage("Run", "./solution", True), (
29
+ CommandResult(0, "first\n", "", "case1.in", "expected first\n"),
30
+ CommandResult(2, "second\n", "error", "case2.in", "expected second\n"),
31
+ CommandResult(0, "same\n", "", "case3.in", "same\n"),
32
+ CommandResult(1, "same\n", "error", "case4.in", "same\n"),
33
+ CommandResult(0, "output", "", "case5.in"),
34
+ ))
35
+ sections = construct_stage_feedback(result)
36
+ self.assertEqual(sections[0][0], "Run")
37
+ body = sections[0][1]
38
+ self.assertIn("**Case 0:**\n\nStatus: 0", body)
39
+ self.assertIn("**Case 1:**\n\nStatus: 2", body)
40
+ self.assertIn("```diff\n--- case1.out\n+++ stdout\n@@ -1 +1 @@", body)
41
+ self.assertIn("```diff\n--- case2.out\n+++ stdout\n@@ -1 +1 @@", body)
42
+ self.assertIn("-expected first\n+first\n", body)
43
+ self.assertIn("`stderr`: \n\n```\nerror\n```", body)
44
+ self.assertLess(body.index("first"), body.index("second"))
45
+ cases = body.split("**Case ")[1:]
46
+ self.assertEqual(len(cases), 5)
47
+ for case, verdict in zip(cases, (
48
+ "Failed...", "Failed...", "Passed!", "Failed...", "Failed...",
49
+ )):
50
+ with self.subTest(case=case.splitlines()[0]):
51
+ self.assertTrue(case.endswith(f"</details>\n\n{verdict}\n\n"))
52
+
53
+ def test_matching_outputs_including_empty_output_show_no_differences(self):
54
+ for stdout in ("", "same\n"):
55
+ with self.subTest(stdout=stdout):
56
+ result = CommandResult(0, stdout, "", "case1.in", stdout)
57
+ body = construct_result_feedback(result)
58
+ self.assertIn("`stdout`:\n\n```diff\nNo differences.\n```", body)
59
+ self.assertIs(result.output_matches, True)
60
+
61
+ def test_missing_expected_output_is_explicit_and_retains_raw_stdout(self):
62
+ body = construct_result_feedback(CommandResult(0, "actual", "", "case1.in"))
63
+ self.assertIn("Expected output missing: `case1.out`.", body)
64
+ self.assertIn("Comparison unavailable.", body)
65
+ self.assertIn("```\nactual\n```", body)
66
+ self.assertNotIn("No differences.", body)
67
+
68
+ def test_unified_diff_shows_context_whitespace_and_missing_newlines(self):
69
+ diff = construct_output_diff("before\nexpected\nafter\n", "before\nactual\nafter\n", "case1.out")
70
+ self.assertEqual(diff, (
71
+ "--- case1.out\n+++ stdout\n@@ -1,3 +1,3 @@\n"
72
+ " before\n-expected\n+actual\n after\n"
73
+ ))
74
+ for expected, actual, tail in (
75
+ ("same\n", "same", "-same\n+same\n\\n"),
76
+ ("same", "same\n", "-same\n\\n+same\n"),
77
+ ("same \n", "same\n", "-same \n+same\n"),
78
+ ):
79
+ with self.subTest(expected=expected, actual=actual):
80
+ diff = construct_output_diff(expected, actual, "case1.out")
81
+ self.assertEqual(diff, "--- case1.out\n+++ stdout\n@@ -1 +1 @@\n" + tail)
82
+
83
+ def test_empty_stages_and_error_feedback(self):
84
+ self.assertEqual(construct_stage_feedback(), [])
85
+ body = construct_feedback(("OJ config not found", "Contact admins."))
86
+ self.assertTrue(body.startswith("## MiniOJ feedback\n"))
87
+ self.assertIn("### OJ config not found\n\nContact admins.", body)
@@ -0,0 +1,89 @@
1
+ from contextlib import redirect_stdout
2
+ from io import StringIO
3
+ import subprocess
4
+ import unittest
5
+ from unittest.mock import patch
6
+
7
+ import requests
8
+
9
+ from minioj.github import (
10
+ GitHubClient,
11
+ GitHubError,
12
+ NoRemoteFoundError,
13
+ NotAllowedError,
14
+ ResourceNotFoundError,
15
+ get_repo,
16
+ )
17
+
18
+
19
+ class RepositoryTests(unittest.TestCase):
20
+ @patch("minioj.github.subprocess.run")
21
+ def test_explicit_repo_does_not_invoke_git(self, run):
22
+ self.assertEqual(get_repo("owner/repo"), "owner/repo")
23
+ run.assert_not_called()
24
+
25
+ @patch("minioj.github.subprocess.run")
26
+ def test_https_and_ssh_remotes_with_optional_suffix(self, run):
27
+ for url in (
28
+ "https://github.com/owner/repo.name_with-dashes.git",
29
+ "git@github.com:owner/repo.name_with-dashes.git",
30
+ "ssh://git@github.com/owner/repo.name_with-dashes.git",
31
+ "https://github.com/owner/repo.name_with-dashes",
32
+ "https://github.com/owner/repo.name_with-dashes/",
33
+ ):
34
+ with self.subTest(url=url):
35
+ run.side_effect = [
36
+ subprocess.CompletedProcess([], 0, "origin\nupstream\n"),
37
+ subprocess.CompletedProcess([], 0, url + "\n"),
38
+ ]
39
+ self.assertEqual(get_repo(None), "owner/repo.name_with-dashes")
40
+ self.assertEqual(run.call_args.args[0], ["git", "remote", "get-url", "origin"])
41
+
42
+ @patch("minioj.github.subprocess.run")
43
+ def test_missing_or_unparseable_remote(self, run):
44
+ for outputs in (("",), ("origin\n", "not-a-remote")):
45
+ with self.subTest(outputs=outputs):
46
+ run.side_effect = [subprocess.CompletedProcess([], 0, out) for out in outputs]
47
+ with self.assertRaises(NoRemoteFoundError):
48
+ get_repo(None)
49
+
50
+
51
+ class GitHubClientTests(unittest.TestCase):
52
+ @patch("minioj.github.requests.post")
53
+ def test_success_posts_json_and_authentication_with_timeout(self, post):
54
+ response = requests.Response()
55
+ response.status_code = 201
56
+ post.return_value = response
57
+ with redirect_stdout(StringIO()) as output:
58
+ GitHubClient("owner/repo", "test-token").send_comment(42, "feedback")
59
+ post.assert_called_once_with(
60
+ "https://api.github.com/repos/owner/repo/issues/42/comments",
61
+ headers={"Accept": "application/vnd.github+json", "Authorization": "Bearer test-token"},
62
+ json={"body": "feedback"},
63
+ timeout=30,
64
+ )
65
+ self.assertIn("Sent comment", output.getvalue())
66
+
67
+ @patch("minioj.github.requests.post")
68
+ def test_known_statuses_raise_specific_errors(self, post):
69
+ for status, expected in (
70
+ (403, NotAllowedError), (422, NotAllowedError),
71
+ (404, ResourceNotFoundError), (410, ResourceNotFoundError),
72
+ ):
73
+ with self.subTest(status=status):
74
+ response = requests.Response()
75
+ response.status_code = status
76
+ post.return_value = response
77
+ with self.assertRaises(expected) as raised:
78
+ GitHubClient("owner/repo", "test-token").send_comment(42, "feedback")
79
+ self.assertEqual(raised.exception.status_code, status)
80
+
81
+ @patch("minioj.github.requests.post")
82
+ def test_other_failures_and_unexpected_success_are_not_silent(self, post):
83
+ for status, expected in ((401, requests.HTTPError), (500, requests.HTTPError), (200, GitHubError)):
84
+ with self.subTest(status=status):
85
+ response = requests.Response()
86
+ response.status_code = status
87
+ post.return_value = response
88
+ with self.assertRaises(expected):
89
+ GitHubClient("owner/repo", "test-token").send_comment(42, "feedback")
@@ -0,0 +1,123 @@
1
+ from pathlib import Path
2
+ import shlex
3
+ import sys
4
+ from tempfile import TemporaryDirectory
5
+ import unittest
6
+
7
+ from minioj.models import Exercise, ExerciseId, Stage
8
+ from minioj.runner import find_case_files, run_exercise, run_stage
9
+
10
+
11
+ def python_command(source, *args):
12
+ return shlex.join([sys.executable, "-c", source, *args])
13
+
14
+
15
+ class RunnerTests(unittest.TestCase):
16
+ def setUp(self):
17
+ temporary = TemporaryDirectory()
18
+ self.addCleanup(temporary.cleanup)
19
+ self.directory = Path(temporary.name)
20
+
21
+ def test_arguments_closed_stdin_and_nonzero_output_are_captured(self):
22
+ command = python_command(
23
+ "import sys; print(sys.argv[1]); print(repr(sys.stdin.read())); "
24
+ "print('failure detail', file=sys.stderr); sys.exit(7)",
25
+ "quoted argument with spaces",
26
+ )
27
+ stage = Stage("Run", command)
28
+ result = run_stage(stage, self.directory)
29
+ self.assertEqual(result.stage, stage)
30
+ self.assertEqual(len(result.commands), 1)
31
+ process = result.commands[0]
32
+ self.assertEqual(process.returncode, 7)
33
+ self.assertEqual(process.stdout, "quoted argument with spaces\n''\n")
34
+ self.assertEqual(process.stderr, "failure detail\n")
35
+ self.assertIsNone(process.case_name)
36
+ self.assertIsNone(process.output_matches)
37
+
38
+ def test_cases_are_exact_files_in_numeric_order_and_supply_stdin(self):
39
+ for name in ("case10.in", "case2.in", "case1.in", "case2.in.bak", "Case3.in"):
40
+ (self.directory / name).write_text(name, encoding="utf-8")
41
+ (self.directory / "case4.in").mkdir()
42
+ (self.directory / "case.in").touch()
43
+ names = [case.name for case in find_case_files(self.directory)]
44
+ self.assertEqual(names, ["case1.in", "case2.in", "case10.in"])
45
+ for name in names:
46
+ (self.directory / name).with_suffix(".out").write_text(
47
+ f"{name}\n", encoding="utf-8"
48
+ )
49
+
50
+ result = run_stage(
51
+ Stage("Cases", python_command("import sys; print(sys.stdin.read())"), True),
52
+ self.directory,
53
+ )
54
+ self.assertEqual([case.case_name for case in result.commands], names)
55
+ self.assertEqual([case.stdout for case in result.commands], [f"{n}\n" for n in names])
56
+ self.assertTrue(all(case.returncode == 0 for case in result.commands))
57
+ self.assertTrue(all(case.output_matches is True for case in result.commands))
58
+
59
+ def test_cased_outputs_compare_with_exact_matching_output_file(self):
60
+ cases = (
61
+ ("case01", "actual\n", "expected\n", False),
62
+ ("case2", "", "", True),
63
+ ("case10", "line", "line\n", False),
64
+ )
65
+ for name, actual, expected, _ in cases:
66
+ (self.directory / f"{name}.in").write_text(actual, encoding="utf-8")
67
+ (self.directory / f"{name}.out").write_text(expected, encoding="utf-8")
68
+ (self.directory / "case99.out").write_text("orphan", encoding="utf-8")
69
+ result = run_stage(
70
+ Stage("Cases", python_command(
71
+ "import sys; sys.stdout.write(sys.stdin.read())"
72
+ ), True),
73
+ self.directory,
74
+ )
75
+ self.assertEqual(len(result.commands), len(cases))
76
+ for command, (name, actual, expected, matches) in zip(result.commands, cases):
77
+ with self.subTest(name=name):
78
+ self.assertEqual(command.case_name, f"{name}.in")
79
+ self.assertEqual(command.stdout, actual)
80
+ self.assertEqual(command.expected_stdout, expected)
81
+ self.assertIs(command.output_matches, matches)
82
+ self.assertEqual(command.returncode, 0)
83
+
84
+ def test_missing_output_file_does_not_use_backup_or_differently_numbered_file(self):
85
+ (self.directory / "case1.in").write_text("actual", encoding="utf-8")
86
+ for name in ("case1.out.bak", "case1.OUT", "case01.out", "case2.out"):
87
+ (self.directory / name).write_text("actual", encoding="utf-8")
88
+ result = run_stage(
89
+ Stage("Cases", python_command(
90
+ "import sys; sys.stdout.write(sys.stdin.read())"
91
+ ), True),
92
+ self.directory,
93
+ )
94
+ self.assertEqual(result.commands[0].stdout, "actual")
95
+ self.assertIsNone(result.commands[0].expected_stdout)
96
+ self.assertIsNone(result.commands[0].output_matches)
97
+
98
+ def test_cased_stage_without_inputs_does_not_run(self):
99
+ marker = self.directory / "unexpected"
100
+ command = python_command("import pathlib, sys; pathlib.Path(sys.argv[1]).touch()", str(marker))
101
+ result = run_stage(Stage("Cases", command, True), self.directory)
102
+ self.assertEqual(result.commands, ())
103
+ self.assertFalse(marker.exists())
104
+
105
+ def test_stages_share_working_directory_and_continue_after_nonzero_exit(self):
106
+ marker = self.directory / "stage-output"
107
+ first = Stage("First", python_command(
108
+ "import pathlib, sys; pathlib.Path(sys.argv[1]).write_text('ready'); sys.exit(3)",
109
+ str(marker),
110
+ ))
111
+ second = Stage("Second", python_command(
112
+ "import pathlib, sys; print(pathlib.Path(sys.argv[1]).read_text()); print(pathlib.Path.cwd())",
113
+ str(marker),
114
+ ))
115
+ exercise = Exercise(ExerciseId(1, 1), "Example", self.directory, (first, second))
116
+ results = run_exercise(exercise)
117
+ self.assertEqual([result.stage.name for result in results], ["First", "Second"])
118
+ self.assertEqual([result.commands[0].returncode for result in results], [3, 0])
119
+ self.assertEqual(results[1].commands[0].stdout, f"ready\n{Path.cwd()}\n")
120
+
121
+ def test_empty_command_has_clear_error(self):
122
+ with self.assertRaisesRegex(ValueError, "empty command"):
123
+ run_stage(Stage("Empty", " "), self.directory)