def test_read_config(path, contents, fallback): def test_case(assertion, msg, mock=None, read_data=None): with patch("__builtin__.open", mock_open(mock, read_data)): assert assertion(), msg test_case(lambda: sh.read_config(path, fallback) == contents, "Should read valid YAML", read_data=yaml.dump(contents, default_flow_style=False)) with pytest.raises(IOError) if fallback is None else empty(): test_case(lambda: sh.read_config(path, fallback) == fallback, "Should use fallback if file is missing", MagicMock(side_effect=IOError(errno.ENOENT, ""))) with pytest.raises(ParserError): test_case( lambda: sh.read_config(path, fallback) is None, "Should raise error if data is invalid", read_data="[garbage}", )
def test_read_config(path, contents, fallback): def test_case(assertion, msg, mock=None, read_data=None): with patch("__builtin__.open", mock_open(mock, read_data)): assert assertion(), msg test_case( lambda: sh.read_config(path, fallback) == contents, "Should read valid YAML", read_data=yaml.dump(contents, default_flow_style=False) ) with pytest.raises(IOError) if fallback is None else empty(): test_case( lambda: sh.read_config(path, fallback) == fallback, "Should use fallback if file is missing", MagicMock(side_effect=IOError(errno.ENOENT, "")) ) with pytest.raises(ParserError): test_case( lambda: sh.read_config(path, fallback) is None, "Should raise error if data is invalid", read_data="[garbage}", )
def test_write_config(tmpdir, path, contents): fullpath = str(tmpdir.join(path)) sh.write_config(fullpath, contents) assert sh.read_config(fullpath) == contents