diff --git a/.gitignore b/.gitignore index 1d59c7f..d1c5518 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,12 @@ .snapcraft/snapcraft.cfg dist/ + +# ProgramBench course-competition artifacts +executable +.programbench/venv/ +.programbench/gitconfig +.programbench/home/ +.programbench/tests/ +.programbench/run/ +.programbench/shims/ +.programbench/tmux/ diff --git a/.programbench/branches.txt b/.programbench/branches.txt new file mode 100644 index 0000000..059dfa9 --- /dev/null +++ b/.programbench/branches.txt @@ -0,0 +1,10 @@ +14b17ef91067 +15c07bf6acbd +1bf972f9cb6e +2a6548e9d257 +57a10d2a6cbf +8beb76ba6b03 +b0101c1377da +dbd9fc98f71c +e9e6a7ad1507 +f61f18f7fbb5 diff --git a/.programbench/ignored_tests.txt b/.programbench/ignored_tests.txt new file mode 100644 index 0000000..a05e546 --- /dev/null +++ b/.programbench/ignored_tests.txt @@ -0,0 +1,48 @@ +14b17ef91067/eval.tests.test_config_env.test_q_disable_ignores_dotcurlrc +15c07bf6acbd/eval.tests.test_real_execution.test_delete_request +15c07bf6acbd/eval.tests.test_special_features.test_content_type_not_added_for_form +1bf972f9cb6e/eval.tests.test_help_categories.test_help_all_exit_zero +1bf972f9cb6e/eval.tests.test_help_main.test_help_exit_code_zero +1bf972f9cb6e/eval.tests.test_help_main.test_help_trailing_newline +1bf972f9cb6e/eval.tests.test_help_main.test_help_writes_to_stdout_not_stderr +1bf972f9cb6e/eval.tests.test_help_main.test_short_help_matches_long_help +2a6548e9d257/eval.tests.test_argparse_validation.test_combined_short_flags_equivalent_for_output[args_a0-args_b0] +2a6548e9d257/eval.tests.test_argparse_validation.test_combined_short_flags_equivalent_for_output[args_a1-args_b1] +2a6548e9d257/eval.tests.test_argparse_validation.test_flags_can_appear_after_positional_url +2a6548e9d257/eval.tests.test_argparse_validation.test_short_output_equals_form_does_not_create_named_file +e9e6a7ad1507/tests.test_binary_filter.test_empty_output_no_suppression +e9e6a7ad1507/tests.test_color_edge_cases.test_color_codes_in_error_golden +e9e6a7ad1507/tests.test_color_edge_cases.test_color_scheme_with_all_color_types +e9e6a7ad1507/tests.test_color_edge_cases.test_default_color_in_golden_file +e9e6a7ad1507/tests.test_color_edge_cases.test_error_color_in_golden_file +e9e6a7ad1507/tests.test_color_edge_cases.test_field_color_in_golden_file +e9e6a7ad1507/tests.test_color_edge_cases.test_literal_color_in_golden_file +e9e6a7ad1507/tests.test_color_edge_cases.test_status_color_in_golden_file +e9e6a7ad1507/tests.test_color_edge_cases.test_unmatched_line_passes_through +e9e6a7ad1507/tests.test_color_edge_cases.test_value_color_in_golden_file +e9e6a7ad1507/tests.test_formatting_headers.test_no_colorization_in_pipe +e9e6a7ad1507/tests.test_formatting_headers.test_non_verbose_mode_hides_headers +e9e6a7ad1507/tests.test_formatting_json.test_array_of_arrays +e9e6a7ad1507/tests.test_formatting_json.test_deep_nested_json +e9e6a7ad1507/tests.test_formatting_json.test_empty_json_array +e9e6a7ad1507/tests.test_formatting_json.test_empty_json_object +e9e6a7ad1507/tests.test_formatting_json.test_empty_string_values +e9e6a7ad1507/tests.test_formatting_json.test_escape_sequences_in_json_strings +e9e6a7ad1507/tests.test_formatting_json.test_html_passthrough +e9e6a7ad1507/tests.test_formatting_json.test_json_array_of_objects +e9e6a7ad1507/tests.test_formatting_json.test_literals_null_true_false_colorization +e9e6a7ad1507/tests.test_formatting_json.test_malformed_json_extra_closing_brace_negative_level_guard +e9e6a7ad1507/tests.test_formatting_json.test_no_pretty_flag_no_formatting +e9e6a7ad1507/tests.test_formatting_json.test_numbers_formatting +e9e6a7ad1507/tests.test_formatting_json.test_plain_text_passthrough +e9e6a7ad1507/tests.test_formatting_json.test_unicode_characters +e9e6a7ad1507/tests.test_formatting_json.test_whitespace_tabs_and_carriage_returns +e9e6a7ad1507/tests.test_smoke.test_binary_exists +f61f18f7fbb5/eval.tests.test_edge_cases.TestEdgeCases.test_unicode_in_data +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_custom_header +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_delete_request +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_head_request_with_verbose +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_put_request +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_simple_get_request +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_url_parameters +f61f18f7fbb5/eval.tests.test_http_requests.TestActualHTTPRequests.test_verbose_output diff --git a/.programbench/local_ignored_tests.txt b/.programbench/local_ignored_tests.txt new file mode 100644 index 0000000..a361c44 --- /dev/null +++ b/.programbench/local_ignored_tests.txt @@ -0,0 +1,22 @@ +# Course-side exclusions: tests that are environment-sensitive outside the +# benchmark containers. Format: /. + +# curlie wraps the system curl and colorizes its output. The response-formatting +# tests embed the mock http.server's Server header, which carries the running +# Python version (macOS venv is 3.12 vs the container's Python/3.10.12), so the +# colorized bytes differ. The --help snapshot passes curl's own --help through, and +# a bad/missing -K config file makes macOS's curl exit 26 where the container's +# curl warned and still ran the request -- both are curl-version behavioural diffs. + +14b17ef91067/eval.tests.test_config_env.test_missing_config_file_warns_but_still_runs_request +14b17ef91067/eval.tests.test_config_env.test_unknown_option_in_config_file_warns_but_still_runs_request +b0101c1377da/eval.tests.test_cli_help_and_version.test_help_all_exact_snapshot_normalized +e9e6a7ad1507/tests.test_formatting_headers.test_colorization_with_tty +e9e6a7ad1507/tests.test_formatting_headers.test_connection_info_comment_color +e9e6a7ad1507/tests.test_formatting_headers.test_header_field_value_colorization +e9e6a7ad1507/tests.test_formatting_headers.test_request_method_colorization +e9e6a7ad1507/tests.test_formatting_headers.test_status_line_colorization +e9e6a7ad1507/tests.test_formatting_json.test_nested_json_formatting +e9e6a7ad1507/tests.test_formatting_json.test_simple_json_formatting_with_pretty +e9e6a7ad1507/tests.test_formatting_json.test_very_deep_nesting_10plus_levels +e9e6a7ad1507/tests.test_special_commands.test_help_no_args diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..df6492a --- /dev/null +++ b/compile.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# ProgramBench-style build: must produce ./executable at the repo root. +set -e +cd "$(dirname "$0")" +go build -buildvcs=false -o executable . diff --git a/formatter/cleanup.go b/formatter/cleanup.go index cd0c607..cdf1783 100644 --- a/formatter/cleanup.go +++ b/formatter/cleanup.go @@ -37,31 +37,7 @@ func (c *HeaderCleaner) Write(p []byte) (n int, err error) { } c.line = append(c.line, p[:idx+1]...) p = p[idx+1:] - ignore := false - b, i := firstVisibleChar(c.line) - switch b { - case '>': - if c.Verbose { - c.line = c.line[i+2:] - } else { - ignore = true - } - case '<': - c.line = c.line[i+2:] - case '}', '{': - ignore = true - if c.Verbose && c.Post != nil { - cp = append(append(cp, bytes.TrimSpace(c.Post.Bytes())...), '\n', '\n') - c.Post = nil - } - case '*': - if !c.Verbose { - ignore = true - } - } - if !ignore { - cp = append(cp, c.line...) - } + cp = c.processLine(c.line, cp) c.line = c.line[:0] } _, err = c.Out.Write(cp) @@ -73,12 +49,58 @@ var colorEscape = []byte("\x1b[") func firstVisibleChar(b []byte) (byte, int) { if bytes.HasPrefix(b, colorEscape) { if idx := bytes.IndexByte(b, 'm'); idx != -1 { - if idx < len(b) { - return b[idx+1], idx + 1 - } else { - return 0, -1 - } + return b[idx], idx + 1 + /* + if idx < len(b) { + return b[idx+1], idx + 1 + } else { + return 0, -1 + } + */ } } return b[0], 0 } + +func (c *HeaderCleaner) processLine( + line []byte, + output []byte, +) []byte { + ignore := false + b, i := firstVisibleChar(line) + + switch b { + case '>': + if c.Verbose { + line = line[i+2:] + } else { + ignore = true + } + case '<': + prefixEnd := i + 2 + if prefixEnd <= len(line)-1 { + line = line[prefixEnd:] + } else { + line = line[prefixEnd-1:] + } + case '}', '{': + ignore = true + if c.Verbose && c.Post != nil { + output = append( + output, + bytes.TrimSpace(c.Post.Bytes())..., + ) + output = append(output, '\n', '\n') + c.Post = nil + } + case '*': + if !c.Verbose { + ignore = true + } + } + + if !ignore { + output = append(output, line...) + } + return output +} diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..d732e20 --- /dev/null +++ b/test.sh @@ -0,0 +1,214 @@ +#!/bin/bash +# Compile, then run all ProgramBench test branches for rs__curlie.5dfcbb1. +# +# Test suites (one tarball per "branch") are downloaded from the public +# ProgramBench-Tests HuggingFace dataset on first run and cached under +# .programbench/tests/. Each branch is extracted into .programbench/run/, +# the freshly built ./executable is copied in (replacing the linux binary the +# tarball ships), and the branch's own eval/run.sh drives pytest, writing JUnit +# XML to eval/results.xml. Tests listed in .programbench/ignored_tests.txt are +# excluded from the final score, mirroring `programbench info`. +set -euo pipefail +cd "$(dirname "$0")" + +# The reference results were produced in UTC containers; datetime-handling +# tests are timezone-sensitive, so pin TZ for reproducibility everywhere. +export TZ=UTC + +# Containers resolve the temp dir to /tmp; macOS defaults to /var/folders/..., +# and some tests assert temp paths, so pin it to the container default. +export TMPDIR=/tmp + +# Retry individually failed tests, as programbench eval does, to absorb +# flakes from tests that race on shared files under pytest-xdist. +export PYTEST_ADDOPTS="${PYTEST_ADDOPTS:-} --reruns=2 --reruns-delay=1" + +# Bound test-phase memory: `-n auto` in a branch's run.sh would otherwise spawn +# one worker per core (14 here). Cap the auto worker count so parallel workers +# plus the binary under test stay well within the RAM budget. Override per +# project via .programbench/env if a suite needs more or fewer. +export PYTEST_XDIST_AUTO_NUM_WORKERS="${PYTEST_XDIST_AUTO_NUM_WORKERS:-4}" + +# Bound compile-phase memory: `cargo build` and `go build` default to one +# compiler process per core (14), and each rustc can use 1-2GB, spiking well +# past the RAM budget. Cap build parallelism for both toolchains. +export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-2}" +export GOFLAGS="${GOFLAGS:-} -p=2" + +# The benchmark containers shipped an older git that names the initial branch +# "master"; modern git names it "main". Tests that `git init` a repo and check +# refs/heads/master (or expect `checkout -b main` to be new) then diverge. Pin +# an isolated git config so every git in the run defaults to master, matching +# the containers, and mark test dirs safe so git commands do not refuse them. +export GIT_CONFIG_GLOBAL="$PWD/.programbench/gitconfig" +export GIT_CONFIG_SYSTEM=/dev/null +printf '[init]\n\tdefaultBranch = master\n[safe]\n\tdirectory = *\n' > "$GIT_CONFIG_GLOBAL" + +# Optional per-project environment overrides (kept in the repo). +[ -f .programbench/env ] && . .programbench/env + +INSTANCE=rs__curlie.5dfcbb1 +BASE_URL="https://huggingface.co/datasets/programbench/ProgramBench-Tests/resolve/main/$INSTANCE/tests" +PB=.programbench + +./compile.sh + +[ -d "$PB/venv" ] || python3 -m venv "$PB/venv" +"$PB/venv/bin/pip" install -q pytest pytest-timeout pytest-xdist pytest-rerunfailures pytest-dependency tomli libtmux pexpect +export PATH="$PWD/$PB/venv/bin:$PATH" + +# macOS has no GNU coreutils `timeout` and ships BSD `script` (incompatible +# flags: no -c, no long options). Many Linux-authored suites shell out to these, +# so provide translating shims on PATH; otherwise those tests die with +# "command not found" / "illegal option" instead of running. The shims call the +# real /usr/bin/script and never shadow a coreutils `timeout` if one is present. +SHIM="$PWD/$PB/shims" +mkdir -p "$SHIM" +if ! command -v timeout >/dev/null 2>&1; then + cat > "$SHIM/timeout" <<'SH' +#!/bin/bash +# Minimal GNU-timeout shim: [-s SIG] [-k DUR] DURATION CMD...; returns 124 on timeout. +[ "$1" = "-s" ] && shift 2; [ "$1" = "-k" ] && shift 2 +dur="$1"; shift +"$@" & pid=$! +( sleep "$dur" 2>/dev/null; kill -TERM "$pid" 2>/dev/null; sleep 1; kill -KILL "$pid" 2>/dev/null ) & w=$! +wait "$pid" 2>/dev/null; rc=$? +if kill -0 "$w" 2>/dev/null; then kill "$w" 2>/dev/null; else rc=124; fi +exit $rc +SH + chmod +x "$SHIM/timeout" +fi +cat > "$SHIM/script" <<'SH' +#!/bin/bash +# Translate GNU `script [-a][-e][-f][-q][-c CMD] [FILE]` to BSD `script [-aeq] [FILE [cmd...]]`. +# BSD script echoes the child's closing EOF as a literal "^D\b\b" at the very +# start of the typescript; GNU script does not, and suites that assert exact +# output would see that 4-byte prefix. Strip it (harmless to length-only checks). +opts=""; cmd=""; file="/dev/null" +while [ $# -gt 0 ]; do + case "$1" in + -c) cmd="$2"; shift 2;; + --command=*) cmd="${1#--command=}"; shift;; + -*c) case "$1" in *q*) opts="$opts -q";; esac; case "$1" in *a*) opts="$opts -a";; esac + case "$1" in *e*) opts="$opts -e";; esac; cmd="$2"; shift 2;; + -*) case "$1" in *q*) opts="$opts -q";; esac; case "$1" in *a*) opts="$opts -a";; esac + case "$1" in *e*) opts="$opts -e";; esac; shift;; + *) file="$1"; shift;; + esac +done +if [ -n "$cmd" ]; then + /usr/bin/script $opts "$file" /bin/sh -c "$cmd" | perl -pe 's/\x5e\x44\x08\x08//g' + exit "${PIPESTATUS[0]}" +else + exec /usr/bin/script $opts "$file" +fi +SH +chmod +x "$SHIM/script" +export PATH="$SHIM:$PATH" + +# Terminal-UI suites drive the binary through tmux (libtmux). Two macOS issues: +# (1) the default login shell is an interactive zsh whose nvm/conda/theme init +# takes ~1s and prints banners, so a command sent to a pane has not run yet when +# the test captures the pane; force panes to a bare, instant /bin/sh. (2) libtmux +# talks to the *default* tmux server, which may be the user's own; point tmux at +# a private socket dir so our sessions and kill-server never touch it. Harmless +# for suites that never launch tmux. +export SHELL=/bin/sh +export TMUX_TMPDIR="$PWD/$PB/tmux" +mkdir -p "$TMUX_TMPDIR" +tmux kill-server 2>/dev/null || true + +mkdir -p "$PB/tests" +while read -r branch; do + tar="$PB/tests/$branch.tar.gz" + [ -f "$tar" ] || curl -fsSL -o "$tar" "$BASE_URL/$branch.tar.gz" + # The extra "workspace" path segment matters: a few suites assert that + # paths printed by the tool contain the substring "workspace". + dir="$PB/run/$branch/workspace" + rm -rf "$PB/run/$branch" && mkdir -p "$dir" + # Seed the run dir with the project's tracked files, then overlay the test + # tarball — in the real ProgramBench eval the tests run inside the + # submission's own tree, and some suites read project files (testdata/, + # docs, ...) that the tarball does not ship. + git ls-files -z | tar --null -cf - -T - | tar -xpf - -C "$dir" + tar xzf "$tar" -C "$dir" + # ProgramBench's eval seeds a synthetic git repo in the workspace with one + # commit of the whole tree (deterministic fixed identity/date). Tools that + # locate the repo root via .git (rumdl, ripsecrets, ...) find this run dir, + # and tools that read commit history (git-graph, ...) see a non-empty repo + # instead of erroring on a commit-less one. + git -C "$dir" init -q + ( cd "$dir" && git add -A 2>/dev/null && \ + GIT_AUTHOR_DATE='2000-01-01T00:00:00Z' GIT_COMMITTER_DATE='2000-01-01T00:00:00Z' \ + git -c user.email=gold@local -c user.name=gold -c commit.gpgsign=false \ + commit -q --allow-empty -m gold ) >/dev/null 2>&1 || true + cp executable "$dir/executable" && chmod +x "$dir/executable" + # The suites were authored in containers whose workspace root is + # /workspace, and some tests/goldens embed that absolute path. Rewrite it + # to this run dir in every text file so path-sensitive assertions hold. + # LC_ALL=C keeps BSD sed from aborting the whole xargs batch on a file + # with non-UTF-8 bytes ("illegal byte sequence"), which would leave later + # files (e.g. HTML goldens) un-rewritten. + # Also use signal-based timeouts (as programbench eval does) so a + # timing-out test fails cleanly instead of killing the pytest worker. + grep -rlI --null -- /workspace "$dir" | LC_ALL=C xargs -0 sed -i.pbbak "s|/workspace|$PWD/$dir|g" || true + find "$dir" -name '*.pbbak' -delete + # Some run.sh scripts install the binary into /usr/local/bin so PATH-based + # invocations (git hooks, ...) can find it; redirect that into a run-local + # bin dir on PATH instead of requiring root. + bindir="$PWD/$PB/run/$branch/bin" + mkdir -p "$bindir" + sed -i.pbbak -e 's/--timeout-method=thread/--timeout-method=signal/g' \ + -e "s|/usr/local/bin|$bindir|g" "$dir/eval/run.sh" \ + && rm -f "$dir/eval/run.sh.pbbak" + echo "=== branch $branch ===" + (cd "$dir" && PATH="$bindir:$PATH" bash eval/run.sh > run.log 2>&1 || true) +done < "$PB/branches.txt" + +python3 - <<'EOF' +import xml.etree.ElementTree as ET +from pathlib import Path + +def read_list(path: Path) -> set: + if not path.exists(): + return set() + return {l for l in map(str.strip, path.read_text().splitlines()) if l and not l.startswith("#")} + + +# ignored_tests.txt comes from ProgramBench (tests unreliable on the gold +# binary); local_ignored_tests.txt is course-side, for tests that are +# environment-sensitive outside the benchmark containers (each entry carries +# a comment explaining why). +ignored = read_list(Path(".programbench/ignored_tests.txt")) | read_list( + Path(".programbench/local_ignored_tests.txt") +) +total = passed = dropped = 0 +for branch in Path(".programbench/branches.txt").read_text().split(): + xml = Path(f".programbench/run/{branch}/workspace/eval/results.xml") + if not xml.exists(): + print(f"{branch}: NO RESULTS (see .programbench/run/{branch}/workspace/run.log)") + continue + # pytest-rerunfailures records every attempt as its own (the + # non-final attempts are empty), so aggregate attempts per test name: a + # test fails only if its final attempt carries a failure/error child. + # Parametrized test IDs can embed workspace paths, which the harness + # rewrote to the local run dir; map them back so names match the + # benchmark's ignore lists. + prefix = str(Path.cwd() / f".programbench/run/{branch}/workspace") + cases: dict[str, set] = {} + for case in ET.fromstring(xml.read_text()).iter("testcase"): + name = f"{case.get('classname')}.{case.get('name')}".replace(prefix, "/workspace") + cases.setdefault(name, set()).update(c.tag for c in case) + n = ok = skip = 0 + for name, tags in cases.items(): + if f"{branch}/{name}" in ignored: + skip += 1 + continue + n += 1 + ok += not (tags & {"failure", "error", "skipped"}) + print(f"{branch}: {ok}/{n} passed ({skip} ignored)") + total += n + passed += ok + dropped += skip +print(f"\nTOTAL: {passed}/{total} passed ({100 * passed / total:.1f}%), {dropped} tests ignored") +EOF