Skip to content

Commit 9c54660

Browse files
author
Vladimir Kotal
authored
deal with read_config() for empty YAML file properly (#3619)
1 parent a31748b commit 9c54660

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tools/src/main/python/opengrok_tools/utils/readconfig.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def read_config(logger, inputfile):
6262
# Not a valid YAML file.
6363
logger.debug("got exception {}".format(sys.exc_info()[0]))
6464
else:
65+
if cfg is None:
66+
cfg = {}
67+
6568
return cfg
6669
except IOError as e:
6770
logger.error("cannot open '{}': {}".format(inputfile, e.strerror))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# See LICENSE.txt included in this distribution for the specific
11+
# language governing permissions and limitations under the License.
12+
#
13+
# When distributing Covered Code, include this CDDL HEADER in each
14+
# file and include the License file at LICENSE.txt.
15+
# If applicable, add the following below this CDDL HEADER, with the
16+
# fields enclosed by brackets "[]" replaced with your own identifying
17+
# information: Portions Copyright [yyyy] [name of copyright owner]
18+
#
19+
# CDDL HEADER END
20+
#
21+
22+
#
23+
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
24+
#
25+
import os
26+
import tempfile
27+
from mockito import mock
28+
import logging
29+
30+
from opengrok_tools.utils.readconfig import read_config
31+
32+
33+
def test_read_config_empty_yaml():
34+
tmpf = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
35+
tmpf.file.write('#foo\n')
36+
tmpf.close()
37+
res = read_config(mock(spec=logging.Logger), tmpf.name)
38+
os.remove(tmpf.name)
39+
assert res is not None
40+
assert type(res) == dict
41+
assert len(res) == 0

0 commit comments

Comments
 (0)