33from unittest import mock
44
55import pytest
6+ import tomlkit
67from typer .testing import CliRunner
78
89from taskbadger .cli import app
1213
1314
1415@pytest .fixture
15- def test_config ():
16- app_dir = Path (__file__ ).parent / "_test_config"
17- with mock .patch ("taskbadger.config._get_config_path" , return_value = app_dir ):
18- config = Config (organization_slug = "test_org" , project_slug = "test_project" , token = "test_token" )
19- write_config (config )
20- yield
21- os .remove (app_dir )
16+ def mock_config_location ():
17+ config_path = Path (__file__ ).parent / "_mock_config"
18+ with mock .patch ("taskbadger.config._get_config_path" , return_value = config_path ):
19+ yield config_path
20+ if config_path .exists ():
21+ os .remove (config_path )
22+
23+
24+ @pytest .fixture
25+ def mock_config (mock_config_location ):
26+ config = Config (organization_slug = "test_org" , project_slug = "test_project" , token = "test_token" )
27+ write_config (config )
2228
2329
2430def test_info_blank ():
@@ -50,7 +56,7 @@ def test_info_args_trump_env():
5056 _check_output (result , "org1" , "project1" , "-" )
5157
5258
53- def test_info_config (test_config ):
59+ def test_info_config (mock_config ):
5460 result = runner .invoke (app , ["info" ])
5561 _check_output (result , "test_org" , "test_project" , "test_token" )
5662
@@ -60,12 +66,12 @@ def test_info_config(test_config):
6066 "TASKBADGER_PROJECT" : "project2" ,
6167 "TASKBADGER_TOKEN" : "token2" ,
6268}, clear = True )
63- def test_info_config_env (test_config ):
69+ def test_info_config_env (mock_config ):
6470 result = runner .invoke (app , ["info" ])
6571 _check_output (result , "org2" , "project2" , "token2" )
6672
6773
68- def test_info_config_args (test_config ):
74+ def test_info_config_args (mock_config ):
6975 result = runner .invoke (app , ["-o" , "org1" , "-p" , "project1" , "info" ])
7076 _check_output (result , "org1" , "project1" , "test_token" )
7177
@@ -75,11 +81,26 @@ def test_info_config_args(test_config):
7581 "TASKBADGER_PROJECT" : "project2" ,
7682 "TASKBADGER_TOKEN" : "token2" ,
7783}, clear = True )
78- def test_info_config_env_args (test_config ):
84+ def test_info_config_env_args (mock_config ):
7985 result = runner .invoke (app , ["-o" , "org1" , "-p" , "project1" , "info" ])
8086 _check_output (result , "org1" , "project1" , "token2" )
8187
8288
89+ def test_configure (mock_config_location ):
90+ result = runner .invoke (app , ["configure" ], input = "an-org\n a-project\n a-token" )
91+ assert result .exit_code == 0
92+ assert mock_config_location .is_file ()
93+ with mock_config_location .open ("rt" , encoding = "utf-8" ) as fp :
94+ raw_config = tomlkit .load (fp )
95+ config_dict = raw_config .unwrap ()
96+ assert config_dict == {
97+ "defaults" : {
98+ "org" : "an-org" ,
99+ "project" : "a-project" ,
100+ },
101+ "auth" : {"token" : "a-token" }
102+ }
103+
83104def _check_output (result , org , project , token ):
84105 assert result .exit_code == 0
85106 assert f"Organization slug: { org } " in result .stdout
0 commit comments