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

raco test: set current-test-invocation-directory #4988

Merged

Conversation

benknoble
Copy link
Contributor

Running "raco test foo/bar/test.rkt" will cause Racket to change directory to "foo/bar" before invoking "test.rkt", and error messages are printed relative to (current-directory) like

raco test: "foo/bar/test.rkt"
--------------------
fail
FAILURE
name:       check-equal?
location:   test.rkt:5:0
actual:     1
expected:   2
--------------------
1/1 test failures

For an editor to consume this, it has to post-process the output to correlate "raco test: path" with "location: partial path" (which is difficult when running tests in parallel). This is not ideal; output with

location:   foo/bar/test.rkt:5:0

is immediately useable by editors for jumping to the error.

Expose the original directory in which "raco test" was launched so that rackunit (or other test frameworks) can appropriately adjust filepaths when reporting errors.

Checklist
  • Bugfix
  • Feature
  • tests included
  • documentation

pkgs/compiler-lib/raco/testing.rkt Outdated Show resolved Hide resolved
pkgs/racket-doc/scribblings/raco/test.scrbl Outdated Show resolved Hide resolved
Running "raco test foo/bar/test.rkt" will cause Racket to change
directory to "foo/bar" before invoking "test.rkt", and error messages
are printed relative to (current-directory) like

    raco test: "foo/bar/test.rkt"
    --------------------
    fail
    FAILURE
    name:       check-equal?
    location:   test.rkt:5:0
    actual:     1
    expected:   2
    --------------------
    1/1 test failures

For an editor to consume this, it has to post-process the output to
correlate "raco test: path" with "location: partial path" (which is
difficult when running tests in parallel). This is not ideal; output
with

    location:   foo/bar/test.rkt:5:0

is immediately useable by editors for jumping to the error.

Expose the original directory in which "raco test" was launched so
that rackunit (or other test frameworks) can appropriately adjust
filepaths when reporting errors.

Also add a name to the test-log-enabled? parameter while we're here.
@benknoble
Copy link
Contributor Author

I believe the new version addresses @usaoc's comments. Should the out contract use path-for-some-system? since that's what the guard actually produces, or is path? sufficient documentation here?

range-diff:

1:  216c2bd1f5 ! 1:  df01b0d1dc raco test: set current-test-invocation-directory
    @@ Commit message
         that rackunit (or other test frameworks) can appropriately adjust
         filepaths when reporting errors.
     
    +    Also add a name to the test-log-enabled? parameter while we're here.
    +
      ## pkgs/compiler-lib/compiler/commands/test.rkt ##
     @@ pkgs/compiler-lib/compiler/commands/test.rkt: (define-values (sum _ deps)
        "Save stdout and stderr to file, overwrite if it exists."
    @@ pkgs/compiler-lib/raco/testing.rkt
     +;; records the original "raco test" directory while raco test changes directory
     +;; to invoke tests
     +(define current-test-invocation-directory
    -+  (make-parameter #f))
    ++  (make-parameter
    ++   #f
    ++   (λ (path)
    ++     (cond
    ++       [(path-string? path) (path->directory-path (simplify-path path))]
    ++       [(not path) path]
    ++       [else (raise-argument-error 'current-test-invocation-directory
    ++                                   "(or/c #f path-string?)"
    ++                                   path)]))
    ++   'current-test-invocation-directory))
      
      (define test-log-enabled?
    -   (make-parameter #t (lambda (v) (and v #t))))
    +-  (make-parameter #t (lambda (v) (and v #t))))
    ++  (make-parameter #t (lambda (v) (and v #t)) 'test-log-enabled?))
    + 
    + (define TOTAL 0)
    + (define FAILED 0)
     
      ## pkgs/racket-doc/scribblings/raco/test.scrbl ##
     @@ pkgs/racket-doc/scribblings/raco/test.scrbl: with @exec{raco test} should also use this library to log test results.
       counted in the test log, such as when testing a custom check's failure
       behavior.}
      
    -+@defparam[current-test-invocation-directory test-invocation-directory (or/c #f path-string?) #:value #f]{
    ++@defparam*[current-test-invocation-directory
    ++            path
    ++            (or/c #f path-string?)
    ++            (or/c #f path?)
    ++            #:value #f]{
     +Contains the directory from which tests were invoked by, @emph{e.g.}, @exec{raco
     +test}. This may differ from @racket[current-directory] when the test runner
     +changes directory before invoking a specific test file and should be set by test

@benknoble benknoble force-pushed the expose-current-test-invocation-directory branch from 216c2bd to df01b0d Compare May 10, 2024 18:07
@usaoc
Copy link
Contributor

usaoc commented May 11, 2024

I think a path or string will always end up as a path for the current platform, unless you explicitly produce a path for a different platform, so path? is correct here.

@rfindler
Copy link
Member

rfindler commented May 11, 2024 via email

@benknoble
Copy link
Contributor Author

Sorry if I am missing something, but on the input side would path-string? be acceptable?

Yes; indeed, the documentation and code (should) both agree that the parameter accepts (or/c #f path-string?). The output coerces to (or/c #f path?).

@mflatt mflatt merged commit ea3e810 into racket:master May 13, 2024
7 checks passed
@benknoble benknoble deleted the expose-current-test-invocation-directory branch May 13, 2024 19:00
@benknoble
Copy link
Contributor Author

@mflatt it looks like I've missed a case somewhere: when I do raco test -j 8 foo/bar/test.rkt, I still get the new output. But with raco test -j 8 foo/bar/test{1,2}.rkt, I get the old output (meaning the new parameter hasn't been set, I think).

Any pointers?

@mflatt
Copy link
Member

mflatt commented May 19, 2024

@benknoble With -j 8, parallelism can be implemented with a thread, place, or process. For places, maybe line 121 of test.rkt is the right point to set the parameter in the new place, and maybe the value to be used for the parameter needs to be passed to that place. For processes, probably the value of the parameter needs to be passed as a command-line argument to be received by the process submodule.

@benknoble
Copy link
Contributor Author

Thanks. I started thinking along those lines; the code references are helpful.

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.

None yet

4 participants