Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reporting terminal dimensions #381

Merged
merged 5 commits into from
Sep 20, 2023
Merged

Fix reporting terminal dimensions #381

merged 5 commits into from
Sep 20, 2023

Conversation

MisterDA
Copy link
Collaborator

@MisterDA MisterDA commented Mar 27, 2023

My intent was to use a bigger default than 80 columns when the CI is detected. Sadly, this would seem to require too big of a refactoring to communicate the default size to the Pp module, which is also why I haven't made the number of columns configurable programmatically, and exposed it only via an environment variable to fix reproducibility issues for the Unix platform.
Oddly enough, communicating the bounds to the Format module with Format.set_margin hides the first [OK], [FAIL], etc tag. I've haven't dug why.
I've tested that this on macOS, Windows FreeBSD, and Linux.

@MisterDA MisterDA force-pushed the terminal-dimensions branch from dace603 to d25192a Compare March 27, 2023 15:47
@MisterDA MisterDA marked this pull request as draft March 27, 2023 15:53
@MisterDA MisterDA marked this pull request as ready for review March 27, 2023 18:17
@MisterDA MisterDA force-pushed the terminal-dimensions branch 2 times, most recently from f3c9b07 to 1eb8a29 Compare March 28, 2023 14:59
@MisterDA
Copy link
Collaborator Author

@OlivierNicole has done a nice review for this PR, thanks.
I think it can be merged as-is. Does a anyone have an opinion?

- simplify macro dance;
- enable size detection on macOS;
- use CAML_NAME_SPACE before OCaml 5.0.
This allows the library to report the correct size even when test are
run buffered with `dune runtest`, as it changes the standard output of
the executable. The correct size was reported when the test was run
unbuffered (e.g., with `dune exec -- test.exe`).
In some platforms, incorrect reporting would lead to a division by
zero.
Even though we had the C stubs, it was disabled in OCaml code.

CAMLprim value ocaml_alcotest_get_terminal_dimensions(value unit)
{
CAMLparam1(unit);
CAMLlocal2(result, pair);
struct winsize ws;
int z = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
if (z == 0)
int fd = open("/dev/tty", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case someone else is looking at this and doesn't know why STDOUT_FILENO was replaced by "/dev/tty" (like I was), the difference is when the output of alcotest is piped (say through a grep): stdout wouldn't have a size so the formatting would be different (and probably be confusing to users?), while the tty always return the same size (... I don't have strong opinion regarding this change, in general it's weird for piped programs to respect terminal size but it seems fine for alcotest)

Copy link
Member

@dinosaure dinosaure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM even if I don't really like the allocation on the C side (return -1 is a possibility for the None case but it's not related to this PR).

@dinosaure dinosaure merged commit d30c362 into main Sep 20, 2023
@dinosaure dinosaure deleted the terminal-dimensions branch September 20, 2023 09:34
samoht added a commit to samoht/opam-repository that referenced this pull request Jul 25, 2024
CHANGES:

- Add `match_raises`, a generalized version of `check_raises`
  (mirage/alcotest#88, mirage/alcotest#386, @JoanThibault)

- Update JaneStreet core and async to v0.16 (mirage/alcotest#390 @tmcgilchrist)

- Fix division by zero when size of the terminal is incorrectly
  reported as zero. (fix mirage/alcotest#356, mirage/alcotest#381, @MisterDA)

- Enable terminal size reporting on macOS and Windows. Also report the
  terminal size even when the test is run buffered by Dune.
  (mirage/alcotest#381, mirage/alcotest#396, @MisterDA)

- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
  var. (mirage/alcotest#322, mirage/alcotest#381, @MisterDA)

- Be able to allocate and use user's formatters for stdout/stderr
  (mirage/alcotest#399, @dinosaure)

- Stop detecting ocamlci specifically, since there's nothing specific
  about it. Simply use the `CI` env var to detect CIs. Improve CI
  detection.
  (mirage/alcotest#397, @MisterDA)
Halbaroth pushed a commit to Halbaroth/opam-repository that referenced this pull request Jul 26, 2024
CHANGES:

- Add `match_raises`, a generalized version of `check_raises`
  (mirage/alcotest#88, mirage/alcotest#386, @JoanThibault)

- Update JaneStreet core and async to v0.16 (mirage/alcotest#390 @tmcgilchrist)

- Fix division by zero when size of the terminal is incorrectly
  reported as zero. (fix mirage/alcotest#356, mirage/alcotest#381, @MisterDA)

- Enable terminal size reporting on macOS and Windows. Also report the
  terminal size even when the test is run buffered by Dune.
  (mirage/alcotest#381, mirage/alcotest#396, @MisterDA)

- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
  var. (mirage/alcotest#322, mirage/alcotest#381, @MisterDA)

- Be able to allocate and use user's formatters for stdout/stderr
  (mirage/alcotest#399, @dinosaure)

- Stop detecting ocamlci specifically, since there's nothing specific
  about it. Simply use the `CI` env var to detect CIs. Improve CI
  detection.
  (mirage/alcotest#397, @MisterDA)
avsm pushed a commit to avsm/opam-repository that referenced this pull request Sep 5, 2024
CHANGES:

- Add `match_raises`, a generalized version of `check_raises`
  (mirage/alcotest#88, mirage/alcotest#386, @JoanThibault)

- Update JaneStreet core and async to v0.16 (mirage/alcotest#390 @tmcgilchrist)

- Fix division by zero when size of the terminal is incorrectly
  reported as zero. (fix mirage/alcotest#356, mirage/alcotest#381, @MisterDA)

- Enable terminal size reporting on macOS and Windows. Also report the
  terminal size even when the test is run buffered by Dune.
  (mirage/alcotest#381, mirage/alcotest#396, @MisterDA)

- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
  var. (mirage/alcotest#322, mirage/alcotest#381, @MisterDA)

- Be able to allocate and use user's formatters for stdout/stderr
  (mirage/alcotest#399, @dinosaure)

- Stop detecting ocamlci specifically, since there's nothing specific
  about it. Simply use the `CI` env var to detect CIs. Improve CI
  detection.
  (mirage/alcotest#397, @MisterDA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random crashes when running tests in a non-standard terminal
3 participants