MilesCranmer commited on
Commit
3555cfd
1 Parent(s): f335ea1

WIP move `python -m pysr.test` call to click interface

Browse files
pysr/_cli/main.py CHANGED
@@ -2,6 +2,14 @@ import warnings
2
 
3
  import click
4
 
 
 
 
 
 
 
 
 
5
 
6
  @click.group("pysr")
7
  @click.pass_context
@@ -38,3 +46,34 @@ def _install(julia_project, quiet, precompile):
38
  warnings.warn(
39
  "This command is deprecated. Julia dependencies are now installed at first import."
40
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import click
4
 
5
+ from ..test import (
6
+ get_runtests_cli,
7
+ runtests,
8
+ runtests_env,
9
+ runtests_jax,
10
+ runtests_torch,
11
+ )
12
+
13
 
14
  @click.group("pysr")
15
  @click.pass_context
 
46
  warnings.warn(
47
  "This command is deprecated. Julia dependencies are now installed at first import."
48
  )
49
+
50
+
51
+ TEST_OPTIONS = {"main", "env", "jax", "torch", "cli"}
52
+
53
+
54
+ @pysr.command("test", help="Run PySR test suite.")
55
+ @click.argument("tests", nargs=-1, help="Choose from " + ", ".join(TEST_OPTIONS) + ".")
56
+ def _tests(tests):
57
+ if len(tests) == 0:
58
+ raise click.UsageError(
59
+ "At least one test must be specified. "
60
+ + "The following are available: "
61
+ + ", ".join(TEST_OPTIONS)
62
+ + "."
63
+ )
64
+ else:
65
+ for test in tests:
66
+ if test in TEST_OPTIONS:
67
+ if test == "main":
68
+ runtests()
69
+ elif test == "env":
70
+ runtests_env()
71
+ elif test == "jax":
72
+ runtests_jax()
73
+ elif test == "torch":
74
+ runtests_torch()
75
+ elif test == "cli":
76
+ runtests_cli = get_runtests_cli()
77
+ runtests_cli()
78
+ else:
79
+ warnings.warn(f"Invalid test {test}. Skipping.")
pysr/julia_helpers.py CHANGED
@@ -38,11 +38,6 @@ from juliacall import convert as jl_convert
38
  jl.seval("using Serialization: Serialization")
39
  jl.seval("using PythonCall: PythonCall")
40
 
41
- juliainfo = None
42
- julia_initialized = False
43
- julia_kwargs_at_initialization = None
44
- julia_activated_env = None
45
-
46
 
47
  def _escape_filename(filename):
48
  """Turn a path into a string with correctly escaped backslashes."""
 
38
  jl.seval("using Serialization: Serialization")
39
  jl.seval("using PythonCall: PythonCall")
40
 
 
 
 
 
 
41
 
42
  def _escape_filename(filename):
43
  """Turn a path into a string with correctly escaped backslashes."""
pysr/test/__init__.py CHANGED
@@ -1,7 +1,13 @@
1
  from .test import runtests
2
- from .test_cli import runtests as runtests_cli
3
  from .test_env import runtests as runtests_env
4
  from .test_jax import runtests as runtests_jax
5
  from .test_torch import runtests as runtests_torch
6
 
7
- __all__ = ["runtests", "runtests_env", "runtests_jax", "runtests_torch", "runtests_cli"]
 
 
 
 
 
 
 
1
  from .test import runtests
2
+ from .test_cli import get_runtests as get_runtests_cli
3
  from .test_env import runtests as runtests_env
4
  from .test_jax import runtests as runtests_jax
5
  from .test_torch import runtests as runtests_torch
6
 
7
+ __all__ = [
8
+ "runtests",
9
+ "runtests_env",
10
+ "runtests_jax",
11
+ "runtests_torch",
12
+ "get_runtests_cli",
13
+ ]
pysr/test/__main__.py CHANGED
@@ -1,6 +1,5 @@
1
  """CLI for running PySR's test suite."""
2
  import argparse
3
- import os
4
 
5
  from . import *
6
 
@@ -11,33 +10,6 @@ if __name__ == "__main__":
11
  parser.add_argument(
12
  "test",
13
  nargs="*",
14
- help="Test to run. One or more of 'main', 'env', 'jax', 'torch', 'cli'.",
 
15
  )
16
-
17
- # Parse args:
18
- args = parser.parse_args()
19
- tests = args.test
20
-
21
- if len(tests) == 0:
22
- # Raise help message:
23
- parser.print_help()
24
- raise SystemExit(1)
25
-
26
- # Run tests:
27
- for test in tests:
28
- if test in {"main", "env", "jax", "torch", "cli"}:
29
- cur_dir = os.path.dirname(os.path.abspath(__file__))
30
- print(f"Running test from {cur_dir}")
31
- if test == "main":
32
- runtests()
33
- elif test == "env":
34
- runtests_env()
35
- elif test == "jax":
36
- runtests_jax()
37
- elif test == "torch":
38
- runtests_torch()
39
- elif test == "cli":
40
- runtests_cli()
41
- else:
42
- parser.print_help()
43
- raise SystemExit(1)
 
1
  """CLI for running PySR's test suite."""
2
  import argparse
 
3
 
4
  from . import *
5
 
 
10
  parser.add_argument(
11
  "test",
12
  nargs="*",
13
+ help="DEPRECATED. Use `python -m pysr test [tests...]` instead."
14
+ # help="Test to run. One or more of 'main', 'env', 'jax', 'torch', 'cli'.",
15
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pysr/test/test_cli.py CHANGED
@@ -2,58 +2,62 @@ import unittest
2
 
3
  from click import testing as click_testing
4
 
5
- from .._cli.main import pysr
6
-
7
-
8
- class TestCli(unittest.TestCase):
9
- # TODO: Include test for custom project here.
10
- def setUp(self):
11
- self.cli_runner = click_testing.CliRunner()
12
-
13
- def test_help_on_all_commands(self):
14
- expected = "\n".join(
15
- [
16
- "Usage: pysr [OPTIONS] COMMAND [ARGS]...",
17
- "",
18
- "Options:",
19
- " --help Show this message and exit.",
20
- "",
21
- "Commands:",
22
- " install Install Julia dependencies for PySR.",
23
- "",
24
- ]
25
- )
26
- result = self.cli_runner.invoke(pysr, ["--help"])
27
- self.assertEqual(expected, result.output)
28
- self.assertEqual(0, result.exit_code)
29
-
30
- def test_help_on_install(self):
31
- expected = "\n".join(
32
- [
33
- "Usage: pysr install [OPTIONS]",
34
- "",
35
- " Install Julia dependencies for PySR.",
36
- "",
37
- "Options:",
38
- " -p, --project PROJECT_DIRECTORY",
39
- " Install in a specific Julia project (e.g., a",
40
- " local copy of SymbolicRegression.jl).",
41
- " -q, --quiet Disable logging.",
42
- " --precompile Force precompilation of Julia libraries.",
43
- " --no-precompile Disable precompilation.",
44
- " --help Show this message and exit.",
45
- "",
46
- ]
47
- )
48
- result = self.cli_runner.invoke(pysr, ["install", "--help"])
49
- self.assertEqual(expected, result.output)
50
- self.assertEqual(0, result.exit_code)
51
-
52
-
53
- def runtests():
54
- """Run all tests in cliTest.py."""
55
- loader = unittest.TestLoader()
56
- suite = unittest.TestSuite()
57
- suite.addTests(loader.loadTestsFromTestCase(TestCli))
58
- runner = unittest.TextTestRunner()
59
- return runner.run(suite)
 
 
 
 
 
2
 
3
  from click import testing as click_testing
4
 
5
+
6
+ def get_runtests():
7
+ # Lazy load to avoid circular imports.
8
+
9
+ from .._cli.main import pysr
10
+
11
+ class TestCli(unittest.TestCase):
12
+ # TODO: Include test for custom project here.
13
+ def setUp(self):
14
+ self.cli_runner = click_testing.CliRunner()
15
+
16
+ def test_help_on_all_commands(self):
17
+ expected = "\n".join(
18
+ [
19
+ "Usage: pysr [OPTIONS] COMMAND [ARGS]...",
20
+ "",
21
+ "Options:",
22
+ " --help Show this message and exit.",
23
+ "",
24
+ "Commands:",
25
+ " install Install Julia dependencies for PySR.",
26
+ "",
27
+ ]
28
+ )
29
+ result = self.cli_runner.invoke(pysr, ["--help"])
30
+ self.assertEqual(expected, result.output)
31
+ self.assertEqual(0, result.exit_code)
32
+
33
+ def test_help_on_install(self):
34
+ expected = "\n".join(
35
+ [
36
+ "Usage: pysr install [OPTIONS]",
37
+ "",
38
+ " Install Julia dependencies for PySR.",
39
+ "",
40
+ "Options:",
41
+ " -p, --project PROJECT_DIRECTORY",
42
+ " Install in a specific Julia project (e.g., a",
43
+ " local copy of SymbolicRegression.jl).",
44
+ " -q, --quiet Disable logging.",
45
+ " --precompile Force precompilation of Julia libraries.",
46
+ " --no-precompile Disable precompilation.",
47
+ " --help Show this message and exit.",
48
+ "",
49
+ ]
50
+ )
51
+ result = self.cli_runner.invoke(pysr, ["install", "--help"])
52
+ self.assertEqual(expected, result.output)
53
+ self.assertEqual(0, result.exit_code)
54
+
55
+ def runtests():
56
+ """Run all tests in cliTest.py."""
57
+ loader = unittest.TestLoader()
58
+ suite = unittest.TestSuite()
59
+ suite.addTests(loader.loadTestsFromTestCase(TestCli))
60
+ runner = unittest.TextTestRunner()
61
+ return runner.run(suite)
62
+
63
+ return runtests