44
55from click .testing import CliRunner
66from Crypto .PublicKey import RSA
7+ from transcriptic import commands
78from transcriptic .cli import cli
89
910
@@ -40,7 +41,9 @@ def temp_ssh_key(tmpdir_factory):
4041
4142@pytest .fixture
4243def cli_test_runner (temp_tx_dotfile ):
43- runner = CliRunner (env = {"TRANSCRIPTIC_CONFIG" : str (temp_tx_dotfile )})
44+ runner = CliRunner (
45+ env = {"TRANSCRIPTIC_CONFIG" : str (temp_tx_dotfile )}, mix_stderr = False
46+ )
4447 yield runner
4548
4649
@@ -58,7 +61,7 @@ def test_login_nonexistent_key_path(cli_test_runner):
5861 ["login" , "--rsa-key" , "/temp/path/invalid_key.pem" ],
5962 input = "\n " .join (["email@foo.com" , "barpw" ]),
6063 )
61- assert "Invalid value for '--rsa-key'" in result .output
64+ assert "Invalid value for '--rsa-key'" in result .stderr
6265
6366
6467def test_login_random_file_for_key (cli_test_runner , tmpdir_factory ):
@@ -72,8 +75,9 @@ def test_login_random_file_for_key(cli_test_runner, tmpdir_factory):
7275 )
7376 assert (
7477 "Error loading RSA key: Could not parse the specified RSA Key, "
75- "ensure it is a PRIVATE key in PEM format" in result .output
78+ "ensure it is a PRIVATE key in PEM format" in result .stderr
7679 )
80+ assert result .exit_code == 1
7781
7882
7983def test_login_public_key (cli_test_runner , temp_ssh_key ):
@@ -84,3 +88,88 @@ def test_login_public_key(cli_test_runner, temp_ssh_key):
8488 input = "\n " .join (["email@foo.com" , "barpw" ]),
8589 )
8690 assert "Error connecting to host: This is not a private key" in result .output
91+ assert result .exit_code == 1
92+
93+
94+ def test_projects_exception (cli_test_runner , monkeypatch ):
95+ def mocked_exception (api , i , json_flag , names_only ):
96+ raise RuntimeError ("Some runtime error" )
97+
98+ monkeypatch .setattr (commands , "projects" , mocked_exception )
99+
100+ result = cli_test_runner .invoke (
101+ cli ,
102+ ["projects" ],
103+ )
104+ assert result .stderr == (
105+ "There was an error listing the projects in your "
106+ "organization. Make sure your login details are correct.\n "
107+ )
108+
109+
110+ def test_projects_names_only (cli_test_runner , monkeypatch ):
111+ mocked_return = {"p123" : "Foo" }
112+ monkeypatch .setattr (
113+ commands , "projects" , lambda api , i , json_flag , names_only : mocked_return
114+ )
115+
116+ result = cli_test_runner .invoke (
117+ cli ,
118+ ["projects" , "--names" ],
119+ )
120+ assert result .output == f"{ mocked_return } \n "
121+
122+
123+ def test_projects_json_flag (cli_test_runner , monkeypatch ):
124+ mocked_return = [{"archived_at" : "some datetime" , "id" : "p123" , "name" : "Foo" }]
125+ monkeypatch .setattr (
126+ commands , "projects" , lambda api , i , json_flag , names_only : mocked_return
127+ )
128+
129+ result = cli_test_runner .invoke (
130+ cli ,
131+ ["projects" , "--json" ],
132+ )
133+ assert result .output == f"{ json .dumps (mocked_return )} \n "
134+
135+
136+ def test_projects_default (cli_test_runner , monkeypatch ):
137+ mocked_return = {"p123" : "Foo (archived)" }
138+ monkeypatch .setattr (
139+ commands , "projects" , lambda api , i , json_flag , names_only : mocked_return
140+ )
141+
142+ result = cli_test_runner .invoke (
143+ cli ,
144+ ["projects" ],
145+ )
146+ assert result .output == (
147+ "\n "
148+ " PROJECTS:\n "
149+ " \n "
150+ " PROJECT NAME | PROJECT ID \n "
151+ "--------------------------------------------------------------------------------\n "
152+ "Foo (archived) | p123 \n "
153+ "--------------------------------------------------------------------------------\n "
154+ )
155+
156+
157+ def test_submit_exception_handling (cli_test_runner , monkeypatch ):
158+ runtime_error = "Some runtime error message"
159+
160+ def mocked_exception (api , file , project , title , test , pm ):
161+ raise RuntimeError (runtime_error )
162+
163+ monkeypatch .setattr (commands , "submit" , mocked_exception )
164+ result = cli_test_runner .invoke (cli , ["submit" , "--project" , "some project" ])
165+ assert result .stderr == f"{ runtime_error } \n "
166+ assert result .exit_code == 1
167+
168+
169+ def test_submit_success (cli_test_runner , monkeypatch ):
170+ mock_url = "http://mock-api/mock/p123/runs/r123"
171+ monkeypatch .setattr (
172+ commands , "submit" , lambda api , file , project , title , test , pm : mock_url
173+ )
174+ result = cli_test_runner .invoke (cli , ["submit" , "--project" , "some project" ])
175+ assert result .output == f"Run created: { mock_url } \n "
0 commit comments