@@ -81,3 +81,53 @@ def test_load_toml_app_config_no_config_found(mock_platformdirs, mock_cwd) -> No
81
81
82
82
assert "Cannot find configuration file" in mock_stdout .getvalue ()
83
83
assert sys_exit .value .args [0 ] == 1
84
+
85
+
86
+ @unittest .mock .patch ("joft.utils.pathlib.Path.cwd" )
87
+ def test_load_toml_app_config_invalid_config_found (mock_cwd ) -> None :
88
+ """
89
+ Test that we will end with a non-zero error code when there is an invalid
90
+ config present and printing a message on the stdout.
91
+ """
92
+
93
+ invalid_config_file_contents = """[jira.server]
94
+ pat_token = "__pat_token__"
95
+ """
96
+
97
+ with tempfile .TemporaryDirectory () as tmpdir :
98
+ mock_cwd .return_value = tmpdir
99
+
100
+ config_file_path = os .path .join (tmpdir , "joft.config.toml" )
101
+ with open (config_file_path , "w" ) as fp :
102
+ fp .write (invalid_config_file_contents )
103
+
104
+ with unittest .mock .patch ("sys.stdout" , new = io .StringIO ()) as mock_stdout :
105
+ with pytest .raises (SystemExit ) as sys_exit :
106
+ joft .utils .load_toml_app_config ()
107
+
108
+ assert f"Configuration file { config_file_path } is invalid" in mock_stdout .getvalue ()
109
+ assert "KeyError - 'hostname'" in mock_stdout .getvalue ()
110
+ assert sys_exit .value .args [0 ] == 1
111
+
112
+
113
+ @pytest .mark .parametrize (
114
+ "config_file_content, raises" ,
115
+ [
116
+ ("[jira.server]\n hostname = 'foo'\n pat_token = 'bar'" , None ),
117
+ ("" , KeyError ),
118
+ ("[jira.server]\n hostname = 'foo'" , KeyError ),
119
+ ("[jira.server]\n pat_token = 'bar'" , KeyError ),
120
+ ("hostname = 'foo'\n pat_token = 'bar'" , KeyError ),
121
+ ],
122
+ )
123
+ def test_read_and_validate_config (config_file_content , raises , tmp_path ) -> None :
124
+ config_file_path = tmp_path / "joft.config.toml"
125
+ config_file_path .write_text (config_file_content )
126
+
127
+ if raises is None :
128
+ config = joft .utils .read_and_validate_config (config_file_path )
129
+ assert config ["jira" ]["server" ]["hostname" ] == "foo"
130
+ assert config ["jira" ]["server" ]["pat_token" ] == "bar"
131
+ else :
132
+ with pytest .raises (raises ):
133
+ config = joft .utils .read_and_validate_config (config_file_path )
0 commit comments