MilesCranmer commited on
Commit
4726b4a
1 Parent(s): a0fd52f

Refactor CLI unittest with `click.testing`

Browse files
Files changed (1) hide show
  1. pysr/test/test_cli.py +41 -47
pysr/test/test_cli.py CHANGED
@@ -1,57 +1,51 @@
1
- import subprocess
2
  import unittest
3
-
4
-
5
- def run_command(command):
6
- """
7
- Retrieve output of a command string, decode and convert from CRLF to LF formatting
8
- """
9
- return (
10
- subprocess.run(command.split(" "), stdout=subprocess.PIPE)
11
- .stdout.decode("utf-8")
12
- .replace("\r\n", "\n")
13
- )
14
 
15
 
16
  class TestCli(unittest.TestCase):
17
- def test_help_on_all_commands(self):
18
- command_to_test = "python -m pysr --help"
19
- expected_lines = [
20
- "Usage: pysr [OPTIONS] COMMAND [ARGS]...",
21
- "",
22
- "Options:",
23
- " --help Show this message and exit.",
24
- "",
25
- "Commands:",
26
- " install Install Julia dependencies for PySR.",
27
- "",
28
- ]
29
 
30
- expected = "\n".join(expected_lines)
31
- actual = run_command(command_to_test)
32
- self.assertEqual(expected, actual)
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def test_help_on_install(self):
35
- command_to_test = "python -m pysr install --help"
36
- expected_lines = [
37
- "Usage: pysr install [OPTIONS]",
38
- "",
39
- " Install Julia dependencies for PySR.",
40
- "",
41
- "Options:",
42
- " -p, --project PROJECT_DIRECTORY",
43
- " Install in a specific Julia project (e.g., a",
44
- " local copy of SymbolicRegression.jl).",
45
- " -q, --quiet Disable logging.",
46
- " --precompile Force precompilation of Julia libraries.",
47
- " --no-precompile Disable precompilation.",
48
- " --help Show this message and exit.",
49
- "",
50
- ]
51
-
52
- expected = "\n".join(expected_lines)
53
- actual = run_command(command_to_test)
54
- self.assertEqual(expected, actual)
55
 
56
 
57
  def runtests():
 
 
1
  import unittest
2
+ from click import testing as click_testing
3
+ from .._cli.main import pysr
 
 
 
 
 
 
 
 
 
4
 
5
 
6
  class TestCli(unittest.TestCase):
7
+ # TODO: Include test for custom project here.
8
+ def setUp(self):
9
+ self.cli_runner = click_testing.CliRunner()
 
 
 
 
 
 
 
 
 
10
 
11
+ def test_help_on_all_commands(self):
12
+ expected = "\n".join(
13
+ [
14
+ "Usage: pysr [OPTIONS] COMMAND [ARGS]...",
15
+ "",
16
+ "Options:",
17
+ " --help Show this message and exit.",
18
+ "",
19
+ "Commands:",
20
+ " install Install Julia dependencies for PySR.",
21
+ "",
22
+ ]
23
+ )
24
+ result = self.cli_runner.invoke(pysr, ["--help"])
25
+ self.assertEqual(expected, result.output)
26
+ self.assertEqual(0, result.exit_code)
27
 
28
  def test_help_on_install(self):
29
+ expected = "\n".join(
30
+ [
31
+ "Usage: pysr install [OPTIONS]",
32
+ "",
33
+ " Install Julia dependencies for PySR.",
34
+ "",
35
+ "Options:",
36
+ " -p, --project PROJECT_DIRECTORY",
37
+ " Install in a specific Julia project (e.g., a",
38
+ " local copy of SymbolicRegression.jl).",
39
+ " -q, --quiet Disable logging.",
40
+ " --precompile Force precompilation of Julia libraries.",
41
+ " --no-precompile Disable precompilation.",
42
+ " --help Show this message and exit.",
43
+ "",
44
+ ]
45
+ )
46
+ result = self.cli_runner.invoke(pysr, ["install", "--help"])
47
+ self.assertEqual(expected, result.output)
48
+ self.assertEqual(0, result.exit_code)
49
 
50
 
51
  def runtests():