Skip to content

Commit 5f42a00

Browse files
Warn if "include directories" do not exist (#132)
* Warn if "include directories" do not exist Instead of raising a RunTime errror on a missing include directory, print a warning to the screen. * Fix key string and add test for desired case. Remove the "_" in the include directories string and add a test case to capture the print and ensure the proper messages are found. * Switch to using "warnings" Use the warning module for both the printing of the missing directory message and capturing the output during the test. * Additional warnings cleanup Use an asssertWarns instead of trying to capture the warning separately. Clean some outdated code. * Add warning type to warn Add a RunTimeWarning type to the warn command for ease of filtering and capturing later on. Update the test to capture the new warning type. Co-authored-by: Michka Popoff <[email protected]>
1 parent 82209fc commit 5f42a00

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

pygccxml/parser/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import copy
1313
import platform
1414
import subprocess
15+
import warnings
1516
# In py3, ConfigParser was renamed to the more-standard configparser.
1617
# But there's a py3 backport that installs "configparser" in py2, and I don't
1718
# want it because it has annoying deprecation warnings. So try the real py2
@@ -200,8 +201,12 @@ def __ensure_dir_exists(self, dir_path, meaning):
200201
if os.path.isdir(dir_path):
201202
return
202203
if os.path.exists(self.working_directory):
203-
raise RuntimeError(
204-
'%s("%s") does not exist!' % (meaning, dir_path))
204+
msg = '%s("%s") does not exist.' % (meaning, dir_path)
205+
if meaning == 'include directory':
206+
# Warn instead of failing.
207+
warnings.warn(msg, RuntimeWarning)
208+
else:
209+
raise RuntimeError(msg)
205210
else:
206211
raise RuntimeError(
207212
'%s("%s") should be "directory", not a file.' %

unittests/test_all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from . import test_hash
8585
from . import test_null_comparison
8686
from . import test_comments
87+
from . import test_warn_missing_include_dirs
8788
from . import test_overrides
8889

8990
testers = [
@@ -160,6 +161,7 @@
160161
test_hash,
161162
test_null_comparison,
162163
test_comments,
164+
test_warn_missing_include_dirs,
163165
test_overrides,
164166
]
165167

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2014-2017 Insight Software Consortium.
2+
# Copyright 2004-2009 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
import io
7+
import sys
8+
import os
9+
import unittest
10+
import warnings
11+
12+
sys.path.insert(1, os.path.join(os.curdir, '..'))
13+
sys.path.insert(1, "../pygccxml")
14+
15+
from pygccxml import parser # nopep8
16+
from pygccxml import utils # nopep8
17+
18+
19+
class Test(unittest.TestCase):
20+
21+
def test_config(self):
22+
"""
23+
Test that a missing include directory is printing a warning,
24+
not raising an error
25+
"""
26+
27+
# Some code to parse for the example
28+
code = "int a;"
29+
30+
# Find the location of the xml generator (castxml or gccxml)
31+
generator_path, name = utils.find_xml_generator()
32+
33+
# Path given as include director doesn't exist
34+
config = parser.xml_generator_configuration_t(
35+
xml_generator_path=generator_path,
36+
xml_generator=name,
37+
include_paths=["doesnt/exist", os.getcwd()])
38+
self.assertWarns(RuntimeWarning, parser.parse_string, code, config)
39+
40+
41+
def create_suite():
42+
suite = unittest.TestSuite()
43+
suite.addTest(unittest.makeSuite(Test))
44+
return suite
45+
46+
47+
def run_suite():
48+
unittest.TextTestRunner(verbosity=2).run(create_suite())
49+
50+
51+
if __name__ == "__main__":
52+
run_suite()

0 commit comments

Comments
 (0)