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

Support loading the test suite from the file system #358

Open
mjambon opened this issue Sep 17, 2022 · 0 comments
Open

Support loading the test suite from the file system #358

mjambon opened this issue Sep 17, 2022 · 0 comments

Comments

@mjambon
Copy link
Contributor

mjambon commented Sep 17, 2022

For testing our application, we have essentially a test folder named foo that contains input files. Each input file gives its name to a test case. If foo/ doesn't exist, it's an error and we can't run the tests. However, we want to be able to run --help to inspect command-line options manually from any location in the file system, without foo/ being present.

In order to build the test suite, we must inspect folder foo, resulting in the test suite that we pass to Alcotest.run. It goes like this:

let main () =
  Alcotest.run "semgrep-core" (tests ())

let () = main ()

If we run the test program /path/to/test.exe --help, tests () will be evaluated. It will look for foo/ and will fail instead of showing the help page.

Our workaround is something close to this:

(*
   This allows running the test program with '--help' from any folder
   without getting an error due to not being able to load test data.
*)
let tests_with_delayed_error () =
  try tests ()
  with e ->
     ["Error", ["cannot load test data - not a real test", `Quick, (fun () -> raise e)]]

let main () =
  Alcotest.run "semgrep-core" (tests_with_delayed_error ())

let () = main ()

While this solution works, it still tries to load the test data when it doesn't have to. It's also not an obvious workaround. If Alcotest.run allowed delaying the evaluation of the test suite, it would be easier.

I propose the following alternate run function:

(** Same as [run] but delay the construction of the test suite until it's really needed. *)
val run_dynamic_suite : 
  ( ?argv:string array -> string -> (unit -> unit test list) -> return ) with_options

The change is the test list argument which is now (unit -> unit test list). A similar run_with_args alternative would have to be provided too.

@mjambon mjambon changed the title Support loading the test suite from the filesystem Support loading the test suite from the file system Sep 17, 2022
mjambon added a commit to semgrep/semgrep that referenced this issue Sep 17, 2022
mjambon added a commit to semgrep/semgrep that referenced this issue Sep 19, 2022
* Fix incorrect stripping of '$' from the regexp '\$' (literal dollar sign)

* Add a workaround to allow querying the test program for '--help'
without requiring test data. The issue was raised at
mirage/alcotest#358

* Update changelog

* Add link to alcotest issue

* Clarify comment

* Rephrase
brandonspark pushed a commit to semgrep/semgrep that referenced this issue Sep 22, 2022
* Fix incorrect stripping of '$' from the regexp '\$' (literal dollar sign)

* Add a workaround to allow querying the test program for '--help'
without requiring test data. The issue was raised at
mirage/alcotest#358

* Update changelog

* Add link to alcotest issue

* Clarify comment

* Rephrase
brandonspark pushed a commit to semgrep/semgrep that referenced this issue Sep 22, 2022
* Fix incorrect stripping of '$' from the regexp '\$' (literal dollar sign)

* Add a workaround to allow querying the test program for '--help'
without requiring test data. The issue was raised at
mirage/alcotest#358

* Update changelog

* Add link to alcotest issue

* Clarify comment

* Rephrase
brandonspark pushed a commit to semgrep/semgrep that referenced this issue Sep 22, 2022
* Fix incorrect stripping of '$' from the regexp '\$' (literal dollar sign)

* Add a workaround to allow querying the test program for '--help'
without requiring test data. The issue was raised at
mirage/alcotest#358

* Update changelog

* Add link to alcotest issue

* Clarify comment

* Rephrase
brandonspark pushed a commit to semgrep/semgrep that referenced this issue Sep 23, 2022
* Fix incorrect stripping of '$' from the regexp '\$' (literal dollar sign)

* Add a workaround to allow querying the test program for '--help'
without requiring test data. The issue was raised at
mirage/alcotest#358

* Update changelog

* Add link to alcotest issue

* Clarify comment

* Rephrase
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

No branches or pull requests

1 participant