Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 3d58c6f

Browse files
committed
Merge pull request #328 from dawagner/xmlgeneration-bugfix
Xml Generation bugfix There were some coding errors in domainGenerator.py. This branch fixes them and add more tests to ensure it doesn't happen again.
2 parents 06c7cc2 + 1962672 commit 3d58c6f

File tree

5 files changed

+94
-40
lines changed

5 files changed

+94
-40
lines changed

test/xml-generator/test.py

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! python2
1+
#! python2.7
22
# Copyright (c) 2015, Intel Corporation
33
# All rights reserved.
44
#
@@ -31,37 +31,89 @@
3131
import os
3232
import subprocess
3333
import difflib
34+
import unittest
35+
import copy
36+
37+
class PfConfig:
38+
def __init__(self, toplevel, criteria, schemas):
39+
self.toplevel = toplevel
40+
self.criteria = criteria
41+
self.schemas = schemas
42+
43+
class TestVector:
44+
def __init__(self, initialSettings=None, edds=[], domains=[]):
45+
self.initialSettings = initialSettings
46+
self.edds = edds
47+
self.domains = domains
48+
49+
class Tester(object):
50+
def __init__(self, pfConfig, testVector):
51+
self.command = [sys.executable, "domainGenerator.py",
52+
"--validate",
53+
"--verbose",
54+
"--toplevel-config", pfConfig.toplevel,
55+
"--criteria", pfConfig.criteria,
56+
"--schemas-dir", pfConfig.schemas]
57+
58+
if testVector.initialSettings:
59+
self.command += ["--initial-settings", testVector.initialSettings]
60+
if testVector.edds:
61+
self.command += ["--add-edds"] + testVector.edds
62+
if testVector.domains:
63+
self.command += ["--add-domains"] + testVector.domains
64+
65+
def check(self, reference=None, expectedErrors=0):
66+
process = subprocess.Popen(self.command, stdout=subprocess.PIPE)
67+
actual = process.stdout.read().splitlines()
68+
69+
if process.wait() != expectedErrors:
70+
raise AssertionError("Expected {} errors, found {}".format(
71+
expectedErrors,
72+
process.returncode))
73+
74+
if not reference:
75+
# The caller only wants to check the number of errors
76+
return True
77+
78+
# The generation has succeeded as expected - let's compare with the reference.
79+
unified = difflib.unified_diff(reference,
80+
actual,
81+
fromfile="reference.xml",
82+
tofile="-",
83+
lineterm="")
84+
diffs = list(unified)
85+
if diffs:
86+
for d in diffs:
87+
print(d)
88+
return AssertionError("The result and the reference don't match.")
89+
return True
90+
3491

3592
basedir = os.path.dirname(sys.argv[0])
3693

37-
configDir = os.path.join(basedir, "PFConfig")
38-
vectorDir = os.path.join(basedir, "testVector")
39-
40-
command = [sys.executable, "domainGenerator.py",
41-
"--validate",
42-
"--verbose",
43-
"--toplevel-config", os.path.join(configDir, "configuration.xml"),
44-
"--criteria", os.path.join(configDir, "criteria.txt"),
45-
"--initial-settings", os.path.join(vectorDir, "initialSettings.xml"),
46-
"--add-edds", os.path.join(vectorDir, "first.pfw"), os.path.join(vectorDir, "second.pfw"),
47-
"--add-domains", os.path.join(vectorDir, "third.xml"), os.path.join(vectorDir, "fourth.xml"),
48-
"--schemas-dir", os.path.join(basedir, "../../schemas")]
49-
50-
reference = open(os.path.join(vectorDir, "reference.xml")).read().splitlines()
51-
52-
process = subprocess.Popen(command, stdout=subprocess.PIPE)
53-
actual = process.stdout.read().splitlines()
54-
55-
unified = difflib.unified_diff(reference,
56-
actual,
57-
fromfile="reference.xml",
58-
tofile="-",
59-
lineterm="")
60-
diffs = list(unified)
61-
if diffs:
62-
for d in diffs:
63-
print(d)
64-
sys.exit(1)
65-
if process.wait() != 1:
66-
print("Error: Expected 1 error, found {}".format(process.returncode))
67-
sys.exit(1)
94+
config_dir = os.path.join(basedir, "PFConfig")
95+
vector_dir = os.path.join(basedir, "testVector")
96+
class TestCase(unittest.TestCase):
97+
nominal_reference = open(os.path.join(vector_dir, "reference.xml")).read().splitlines()
98+
nominal_pfconfig = PfConfig(os.path.join(config_dir, "configuration.xml"),
99+
os.path.join(config_dir, "criteria.txt"),
100+
os.path.join(basedir, "../../schemas"))
101+
nominal_vector = TestVector(os.path.join(vector_dir, "initialSettings.xml"),
102+
[os.path.join(vector_dir, "first.pfw"),
103+
os.path.join(vector_dir, "second.pfw")],
104+
[os.path.join(vector_dir, "third.xml"),
105+
os.path.join(vector_dir, "fourth.xml")])
106+
107+
def test_nominal(self):
108+
tester = Tester(self.nominal_pfconfig, self.nominal_vector)
109+
self.assertTrue(tester.check(self.nominal_reference))
110+
111+
def test_nonfatalError(self):
112+
vector = copy.copy(self.nominal_vector)
113+
vector.edds.append(os.path.join(vector_dir, "duplicate.pfw"))
114+
115+
tester = Tester(self.nominal_pfconfig, vector)
116+
self.assertTrue(tester.check(self.nominal_reference, expectedErrors=1))
117+
118+
if __name__ == "__main__":
119+
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Should trigger a non fatal error
2+
domain: EddGroup.First

test/xml-generator/testVector/first.pfw

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,3 @@ domainGroup: EddGroup
3131
component: /Test/test/block/1
3232
q2.5 = 0
3333
string = some other string
34-
# Should trigger a non fatal error
35-
domain: First

tools/xmlGenerator/domainGenerator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def parseArgs():
5858
help="Initial XML settings file (containing a \
5959
<ConfigurableDomains> tag",
6060
nargs='?',
61+
default=None,
6162
metavar="XML_SETTINGS_FILE")
6263
argparser.add_argument('--add-domains',
6364
help="List of single domain files (each containing a single \
@@ -184,7 +185,6 @@ def main():
184185
# EDD files (aka ".pfw" files)
185186
#
186187
parsed_edds = parseEdd(args.edd_files, args.verbose)
187-
error_nb = 0
188188

189189
# We need to modify the toplevel configuration file to account for differences
190190
# between development setup and target (installation) setup, in particular, the
@@ -209,7 +209,9 @@ def main():
209209
args.schemas_dir],
210210
stdout=sys.stdout, stdin=subprocess.PIPE, stderr=sys.stderr)
211211

212-
initial_settings = os.path.realpath(args.initial_settings)
212+
initial_settings = None
213+
if args.initial_settings:
214+
initial_settings = os.path.realpath(args.initial_settings)
213215

214216
for command in generateDomainCommands(logging, all_criteria, initial_settings,
215217
args.xml_domain_files, parsed_edds):
@@ -219,8 +221,8 @@ def main():
219221
# Closing the connector's input triggers the domain generation
220222
connector.stdin.close()
221223
connector.wait()
222-
fake_toplevel_config.delete()
223-
return connector.return_code
224+
os.remove(fake_toplevel_config.name)
225+
return connector.returncode
224226

225227
# If this file is directly executed
226228
if __name__ == "__main__":

tools/xmlGenerator/domainGeneratorConnector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ void XmlGenerator::exportDomains(std::ostream &output)
209209
static const char *usage =
210210
R"(Usage: domainGeneratorConnector <top-level config> <verbose> <validate> <path>
211211
212-
<verbose> '1': verbose, else: terse
213-
<validate> '1' validate, else: don't validate
212+
<verbose> 'verbose': verbose, else: terse
213+
<validate> 'validate': validate, else: don't validate
214214
<path> path to the schemas' directory
215215
216216
All arguments are mandatory. If no validation is required,

0 commit comments

Comments
 (0)