Skip to content

Commit a9e06a4

Browse files
Improve robustness of unit tests
- Remove dependencies of MaterialXTest on the current working directory, allowing it to be launched from any location. - Use getDefaultDataSearchPath to access search paths in MaterialXTest, aligning its behavior with that of other applications. - Merge the concepts of library and source search paths in render testing.
1 parent 99ebdf5 commit a9e06a4

25 files changed

Lines changed: 195 additions & 237 deletions

File tree

source/MaterialXRenderOsl/OslRenderer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ void OslRenderer::renderOSL(const FilePath& dirPath, const string& shaderName, c
143143
" does not include proper tokens for rendering");
144144
}
145145

146+
// Set the working directory for rendering.
147+
FileSearchPath searchPath = getDefaultDataSearchPath();
148+
FilePath rootPath = searchPath.isEmpty() ? FilePath() : searchPath[0];
149+
FilePath origWorkingPath = FilePath::getCurrentPath();
150+
rootPath.setCurrentPath();
151+
146152
// Write scene file
147153
const string sceneFileName("scene_template.xml");
148154
std::ofstream shaderFileStream;
@@ -178,6 +184,9 @@ void OslRenderer::renderOSL(const FilePath& dirPath, const string& shaderName, c
178184
}
179185
}
180186

187+
// Restore the working directory after rendering.
188+
origWorkingPath.setCurrentPath();
189+
181190
// Report errors on a non-zero return value.
182191
if (returnValue)
183192
{

source/MaterialXTest/Main.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ namespace mx = MaterialX;
1313
int main(int argc, char* const argv[])
1414
{
1515
Catch::Session session;
16-
17-
#ifndef _DEBUG
1816
session.configData().showDurations = Catch::ShowDurations::Always;
19-
#endif
17+
2018
#ifdef CATCH_PLATFORM_WINDOWS
2119
BOOL inDebugger = IsDebuggerPresent();
2220
if (inDebugger)
@@ -29,18 +27,6 @@ int main(int argc, char* const argv[])
2927
}
3028
#endif
3129

32-
// If the current path has no valid resources folder, as can occur when launching the
33-
// test suite from an IDE, then align the current path with the module path.
34-
mx::FilePath resourcesPath = mx::FilePath::getCurrentPath() / "resources";
35-
if (!resourcesPath.exists())
36-
{
37-
resourcesPath = mx::FilePath::getModulePath().getParentPath() / "resources";
38-
if (resourcesPath.exists())
39-
{
40-
resourcesPath.getParentPath().setCurrentPath();
41-
}
42-
}
43-
4430
int returnCode = session.applyCommandLine(argc, argv);
4531
if (returnCode != 0)
4632
{

source/MaterialXTest/MaterialXCore/Document.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,24 @@ TEST_CASE("Document", "[document]")
111111

112112
TEST_CASE("Version", "[document]")
113113
{
114-
mx::DocumentPtr doc = mx::createDocument();
115-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
116-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_ng.mtlx"), doc);
117-
mx::FileSearchPath searchPath("resources/Materials/TestSuite/stdlib/upgrade/");
114+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
115+
mx::DocumentPtr stdlib = mx::createDocument();
116+
mx::loadLibraries({ "libraries" }, searchPath, stdlib);
117+
searchPath.append(searchPath.find("resources/Materials/TestSuite/stdlib/upgrade"));
118118

119119
// 1.36 to 1.37
120120
{
121+
mx::DocumentPtr doc = mx::createDocument();
121122
mx::readFromXmlFile(doc, "1_36_to_1_37.mtlx", searchPath);
123+
doc->importLibrary(stdlib);
122124
REQUIRE(doc->validate());
123125

124126
mx::XmlWriteOptions writeOptions;
125127
writeOptions.writeXIncludeEnable = true;
126-
mx::writeToXmlFile(doc, "1_36_to_1_37_updated.mtlx", &writeOptions);
128+
std::string xmlString = mx::writeToXmlString(doc, &writeOptions);
127129

128130
mx::DocumentPtr doc2 = mx::createDocument();
129-
mx::readFromXmlFile(doc2, "1_36_to_1_37_updated.mtlx");
131+
mx::readFromXmlString(doc2, xmlString);
130132
REQUIRE(doc2->validate());
131133

132134
// Check conversion to desired types occurred
@@ -160,18 +162,17 @@ TEST_CASE("Version", "[document]")
160162

161163
// 1.37 to 1.38
162164
{
163-
doc = mx::createDocument();
164-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
165-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_ng.mtlx"), doc);
165+
mx::DocumentPtr doc = mx::createDocument();
166166
mx::readFromXmlFile(doc, "1_37_to_1_38.mtlx", searchPath);
167+
doc->importLibrary(stdlib);
167168
REQUIRE(doc->validate());
168169

169170
mx::XmlWriteOptions writeOptions;
170171
writeOptions.writeXIncludeEnable = false;
171-
mx::writeToXmlFile(doc, "1_37_to_1_38_updated.mtlx", &writeOptions);
172+
std::string xmlString = mx::writeToXmlString(doc, &writeOptions);
172173

173174
mx::DocumentPtr doc2 = mx::createDocument();
174-
mx::readFromXmlFile(doc2, "1_37_to_1_38_updated.mtlx");
175+
mx::readFromXmlString(doc2, xmlString);
175176
REQUIRE(doc2->validate());
176177

177178
// atan2 test

source/MaterialXTest/MaterialXCore/Material.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ TEST_CASE("Material", "[material]")
6161

6262
TEST_CASE("Material Discovery", "[material]")
6363
{
64+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
6465
mx::DocumentPtr doc = mx::createDocument();
65-
66-
const mx::FilePath currentPath = mx::FilePath::getCurrentPath();
67-
mx::FileSearchPath searchPath(currentPath / mx::FilePath("resources/Materials/TestSuite"));
68-
mx::FilePath filename = "stdlib/materials/material_node_discovery.mtlx";
69-
mx::readFromXmlFile(doc, filename, searchPath);
66+
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/materials/material_node_discovery.mtlx", searchPath);
7067

7168
// 1. Find all materials referenced by material assignments
7269
// which are found in connected nodegraphs

source/MaterialXTest/MaterialXCore/Node.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ TEST_CASE("Node", "[node]")
139139
TEST_CASE("Flatten", "[nodegraph]")
140140
{
141141
// Read an example containing graph-based custom nodes.
142+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
142143
mx::DocumentPtr doc = mx::createDocument();
143-
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/shader/surface.mtlx");
144+
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/shader/surface.mtlx", searchPath);
144145
REQUIRE(doc->validate());
145146

146147
// Count root-level, nested, and custom nodes.
@@ -174,8 +175,8 @@ TEST_CASE("Flatten", "[nodegraph]")
174175

175176
TEST_CASE("Inheritance", "[nodedef]")
176177
{
178+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
177179
mx::DocumentPtr doc = mx::createDocument();
178-
mx::FileSearchPath searchPath(mx::FilePath::getCurrentPath());
179180
mx::loadLibraries({ "libraries" }, searchPath, doc);
180181
REQUIRE(doc->validate());
181182
auto nodedef = doc->getNodeDef("ND_standard_surface_surfaceshader");
@@ -559,13 +560,12 @@ TEST_CASE("Organization", "[nodegraph]")
559560

560561
TEST_CASE("Tokens", "[nodegraph]")
561562
{
562-
mx::DocumentPtr doc = mx::createDocument();
563-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
564-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_ng.mtlx"), doc);
565-
mx::FileSearchPath searchPath("resources/Materials/TestSuite/stdlib/texture/");
563+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
564+
mx::DocumentPtr stdlib = mx::createDocument();
565+
mx::loadLibraries({ "libraries" }, searchPath, stdlib);
566566

567-
mx::readFromXmlFile(doc, "tokenGraph.mtlx", searchPath);
568-
doc->validate();
567+
mx::DocumentPtr doc = mx::createDocument();
568+
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/texture/tokenGraph.mtlx", searchPath);
569569

570570
mx::StringVec graphNames = { "Tokenized_Image_2k_png", "Tokenized_Image_4k_jpg" };
571571
mx::StringVec resolutionStrings = { "2k", "4k" };
@@ -597,13 +597,13 @@ TEST_CASE("Tokens", "[nodegraph]")
597597

598598
TEST_CASE("Node Definition Creation", "[nodedef]")
599599
{
600-
mx::DocumentPtr doc = mx::createDocument();
601-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
602-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_ng.mtlx"), doc);
603-
mx::FileSearchPath searchPath("resources/Materials/TestSuite/stdlib/definition/");
600+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
601+
mx::DocumentPtr stdlib = mx::createDocument();
602+
mx::loadLibraries({ "libraries" }, searchPath, stdlib);
604603

605-
mx::readFromXmlFile(doc, "definition_from_nodegraph.mtlx", searchPath);
606-
REQUIRE(doc->validate());
604+
mx::DocumentPtr doc = mx::createDocument();
605+
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/definition/definition_from_nodegraph.mtlx", searchPath);
606+
doc->importLibrary(stdlib);
607607

608608
mx::NodeGraphPtr graph = doc->getNodeGraph("test_colorcorrect");
609609
REQUIRE(graph);
@@ -721,7 +721,5 @@ TEST_CASE("Node Definition Creation", "[nodedef]")
721721
}
722722
REQUIRE(findDefault);
723723
}
724-
725724
REQUIRE(doc->validate());
726-
mx::writeToXmlFile(doc, "definition_from_nodegraph_out.mtlx");
727725
}

source/MaterialXTest/MaterialXCore/Traversal.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,9 @@ TEST_CASE("IntraGraph Traversal", "[traversal]")
176176

177177
TEST_CASE("InterGraph Traversal", "[traversal]")
178178
{
179+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
179180
mx::DocumentPtr doc = mx::createDocument();
180-
mx::FilePath currentPath = mx::FilePath::getCurrentPath();
181-
mx::FileSearchPath searchPath(currentPath);
182-
mx::loadLibraries({ "libraries" }, searchPath, doc);
183-
184-
mx::FilePath testFile = currentPath / mx::FilePath("resources/Materials/TestSuite/stdlib/nodegraph_inputs/nodegraph_nodegraph.mtlx");
185-
mx::readFromXmlFile(doc, testFile, searchPath);
186-
REQUIRE(doc->validate());
181+
mx::readFromXmlFile(doc, "resources/Materials/TestSuite/stdlib/nodegraph_inputs/nodegraph_nodegraph.mtlx", searchPath);
187182

188183
for (mx::NodeGraphPtr graph : doc->getNodeGraphs())
189184
{

source/MaterialXTest/MaterialXCore/Unit.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ const float EPSILON = 1e-4f;
1717

1818
TEST_CASE("UnitAttribute", "[unit]")
1919
{
20+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
2021
mx::DocumentPtr doc = mx::createDocument();
21-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
22+
mx::loadLibraries({ "libraries" }, searchPath, doc);
23+
2224
std::vector<mx::UnitTypeDefPtr> unitTypeDefs = doc->getUnitTypeDefs();
2325
REQUIRE(!unitTypeDefs.empty());
2426

@@ -60,8 +62,9 @@ TEST_CASE("UnitAttribute", "[unit]")
6062

6163
TEST_CASE("UnitEvaluation", "[unit]")
6264
{
65+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
6366
mx::DocumentPtr doc = mx::createDocument();
64-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
67+
mx::loadLibraries({ "libraries" }, searchPath, doc);
6568

6669
//
6770
// Test distance converter
@@ -117,11 +120,11 @@ TEST_CASE("UnitEvaluation", "[unit]")
117120

118121
TEST_CASE("UnitDocument", "[unit]")
119122
{
120-
mx::FilePath libraryPath("libraries/stdlib");
121-
mx::FilePath examplesPath("resources/Materials/TestSuite/stdlib/units");
122-
std::string searchPath = libraryPath.asString() +
123-
mx::PATH_LIST_SEPARATOR +
124-
examplesPath.asString();
123+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
124+
mx::DocumentPtr stdlib = mx::createDocument();
125+
mx::loadLibraries({ "libraries" }, searchPath, stdlib);
126+
mx::FilePath examplesPath = searchPath.find("resources/Materials/TestSuite/stdlib/units");
127+
searchPath.append(examplesPath);
125128

126129
static const std::string DISTANCE_DEFAULT("meter");
127130

@@ -130,7 +133,7 @@ TEST_CASE("UnitDocument", "[unit]")
130133
{
131134
mx::DocumentPtr doc = mx::createDocument();
132135
mx::readFromXmlFile(doc, filename, searchPath);
133-
mx::loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/stdlib/stdlib_defs.mtlx"), doc);
136+
doc->importLibrary(stdlib);
134137

135138
mx::UnitTypeDefPtr distanceTypeDef = doc->getUnitTypeDef("distance");
136139
REQUIRE(distanceTypeDef);
@@ -149,11 +152,14 @@ TEST_CASE("UnitDocument", "[unit]")
149152
mx::NodePtr pNode = elem->asA<mx::Node>();
150153
if (pNode)
151154
{
152-
if (pNode->getInputCount()) {
153-
for (mx::InputPtr input : pNode->getInputs()) {
155+
if (pNode->getInputCount())
156+
{
157+
for (mx::InputPtr input : pNode->getInputs())
158+
{
154159
const std::string type = input->getType();
155160
const mx::ValuePtr value = input->getValue();
156-
if (input->hasUnit() && value) {
161+
if (input->hasUnit() && value)
162+
{
157163
if (type == "float")
158164
{
159165
float originalval = value->asA<float>();

source/MaterialXTest/MaterialXFormat/File.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,27 @@ TEST_CASE("Syntactic operations", "[file]")
3232

3333
TEST_CASE("File system operations", "[file]")
3434
{
35-
mx::StringVec filenames =
35+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
36+
mx::FilePathVec examplePaths =
3637
{
3738
"libraries/stdlib/stdlib_defs.mtlx",
3839
"resources/Materials/Examples/StandardSurface/standard_surface_brass_tiled.mtlx",
3940
"resources/Materials/Examples/StandardSurface/standard_surface_marble_solid.mtlx",
4041
};
41-
42-
for (const std::string& filename : filenames)
42+
for (const mx::FilePath& path : examplePaths)
4343
{
44-
mx::FilePath path(filename);
45-
REQUIRE(path.exists());
46-
REQUIRE(mx::FileSearchPath().find(path).exists());
44+
REQUIRE(searchPath.find(path).exists());
4745
}
4846

49-
mx::FilePath currentPath = mx::FilePath::getCurrentPath();
50-
mx::FilePath modulePath = mx::FilePath::getModulePath();
51-
bool expectedPaths = currentPath == modulePath ||
52-
currentPath == modulePath.getParentPath();
53-
REQUIRE(expectedPaths);
47+
REQUIRE(mx::FilePath::getCurrentPath().exists());
48+
REQUIRE(mx::FilePath::getModulePath().exists());
5449
}
5550

5651
TEST_CASE("File search path operations", "[file]")
5752
{
58-
mx::FileSearchPath searchPath = "libraries/stdlib" +
59-
mx::PATH_LIST_SEPARATOR +
60-
"resources/Materials/Examples/StandardSurface";
53+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
54+
searchPath.append(searchPath.find("libraries/stdlib"));
55+
searchPath.append(searchPath.find("resources/Materials/Examples/StandardSurface"));
6156

6257
mx::FilePathVec filenames =
6358
{
@@ -102,10 +97,8 @@ TEST_CASE("Flatten filenames", "[file]")
10297
image2->setInputValue("file", "brass_color.jpg", mx::FILENAME_TYPE_STRING);
10398

10499
// 2. Test resolving to absolute paths
105-
mx::FilePath rootPath(mx::FilePath::getCurrentPath());
106-
107-
mx::FileSearchPath searchPath;
108-
searchPath.append(rootPath);
100+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
101+
mx::FilePath rootPath = searchPath.isEmpty() ? mx::FilePath() : searchPath[0];
109102

110103
mx::flattenFilenames(doc1, searchPath);
111104
REQUIRE(nodeGraph->getFilePrefix() == mx::EMPTY_STRING);

source/MaterialXTest/MaterialXFormat/XmlIo.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace mx = MaterialX;
1414

1515
TEST_CASE("Load content", "[xmlio]")
1616
{
17-
mx::FilePath libraryPath("libraries/stdlib");
18-
mx::FilePath examplesPath("resources/Materials/Examples/StandardSurface");
19-
mx::FileSearchPath searchPath = libraryPath.asString() +
20-
mx::PATH_LIST_SEPARATOR +
21-
examplesPath.asString();
17+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
18+
mx::FilePath libraryPath = searchPath.find("libraries/stdlib");
19+
mx::FilePath examplesPath = searchPath.find("resources/Materials/Examples/StandardSurface");
20+
searchPath.append(libraryPath);
21+
searchPath.append(examplesPath);
2222

2323
// Read the standard library.
2424
std::vector<mx::DocumentPtr> libs;
@@ -242,7 +242,8 @@ TEST_CASE("Load content", "[xmlio]")
242242

243243
TEST_CASE("Comments and newlines", "[xmlio]")
244244
{
245-
mx::FilePath testPath("resources/Materials/Examples/StandardSurface/standard_surface_chess_set.mtlx");
245+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
246+
mx::FilePath testPath = searchPath.find("resources/Materials/Examples/StandardSurface/standard_surface_chess_set.mtlx");
246247

247248
// Read the example file into an XML string buffer.
248249
std::string origXml = mx::readFile(testPath);

0 commit comments

Comments
 (0)