Skip to content

Commit 34e1495

Browse files
committed
[tests] Add unit tests for the rootFolder variables
1 parent 8ed376f commit 34e1495

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from meshroom.core import desc
2+
3+
class PrintAttributes(desc.Node):
4+
documentation="Test node printing the values of its attributes."
5+
inputs = [
6+
desc.File(
7+
name="relativePathInput",
8+
label="Relative Input File",
9+
description="Relative path to the input file.",
10+
value="${NODE_SOURCECODE_FOLDER}" + "/input.txt",
11+
),
12+
]
13+
14+
outputs = [
15+
desc.File(
16+
name="output",
17+
label="Output",
18+
description="Path to the output file.",
19+
value="${NODE_CACHE_FOLDER}" + "file.out",
20+
),
21+
]
22+
23+
def processChunk(self, chunk):
24+
print(chunk.node.relativePathInput.value)
25+
print(chunk.node.output.value)

tests/test_nodeRootFolders.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# coding:utf-8
2+
3+
from inspect import getfile
4+
import os
5+
from pathlib import Path
6+
from tempfile import mktemp
7+
8+
from meshroom.core.graph import Graph, loadGraph
9+
from meshroom.core import desc, loadAllNodes, registerNodeType, unregisterNodeType
10+
from meshroom.core.node import Node
11+
import meshroom.core
12+
13+
class TestNode(desc.Node):
14+
inputs = []
15+
outputs = []
16+
17+
class PrintAttributes(desc.Node):
18+
# Local "PrintAttributes" node, with a different source code folder as the one in nodes/test
19+
documentation="Test node printing the values of its attributes."
20+
inputs = [
21+
desc.File(
22+
name="relativePathInput",
23+
label="Relative Input File",
24+
description="Relative path to the input file.",
25+
value="${NODE_SOURCECODE_FOLDER}" + "/input.txt",
26+
),
27+
]
28+
29+
outputs = [
30+
desc.File(
31+
name="output",
32+
label="Output",
33+
description="Path to the output file.",
34+
value="${NODE_CACHE_FOLDER}" + "file.out",
35+
),
36+
]
37+
38+
def processChunk(self, chunk):
39+
print(chunk.node.relativePathInput.value)
40+
print(chunk.node.output.value)
41+
42+
43+
def test_registerSameNodeWithDifferentLocations():
44+
"""
45+
Check that the nodes with the same description but registered at different locations have different evaluations
46+
for the NODE_SOURCECODE_FOLDER value.
47+
"""
48+
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
49+
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()
50+
51+
# Node loaded from "nodes/test"
52+
n1 = Node("PrintAttributes")
53+
sourceFolderNode1 = n1.attribute("relativePathInput").getEvalValue()
54+
assert sourceFolderNode1 == Path(getfile(meshroom.core.nodesDesc["PrintAttributes"])).parent.resolve().as_posix() + "/input.txt"
55+
assert Path(sourceFolderNode1).parent.resolve().as_posix() == n1.rootFolder["NODE_SOURCECODE_FOLDER"]
56+
57+
# Unregister that node and replace it with the one from this file
58+
unregisterNodeType(PrintAttributes)
59+
assert "PrintAttributes" not in meshroom.core.nodesDesc.keys()
60+
61+
registerNodeType(PrintAttributes)
62+
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()
63+
64+
n2 = Node("PrintAttributes")
65+
sourceFolderNode2 = n2.attribute("relativePathInput").getEvalValue()
66+
assert sourceFolderNode2 == Path(getfile(meshroom.core.nodesDesc["PrintAttributes"])).parent.resolve().as_posix() + "/input.txt"
67+
assert Path(sourceFolderNode2).parent.resolve().as_posix() == n2.rootFolder["NODE_SOURCECODE_FOLDER"]
68+
69+
assert sourceFolderNode1 is not sourceFolderNode2
70+
unregisterNodeType(PrintAttributes)
71+
72+
73+
def test_reloadGraphWithDifferentNodeLocations():
74+
"""
75+
Save a Graph with a node description registered at a specific location, unregister that node type, and register the
76+
same description from a different location.
77+
"""
78+
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
79+
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()
80+
81+
graph = Graph('')
82+
node = graph.addNewNode("PrintAttributes")
83+
name = node.name
84+
85+
# Save graph in a file
86+
filename = mktemp()
87+
graph.save(filename)
88+
89+
sourceCodeFolderNode = node.attribute("relativePathInput").getEvalValue()
90+
assert sourceCodeFolderNode == node.rootFolder["NODE_SOURCECODE_FOLDER"] + "/input.txt"
91+
92+
cacheFolderNode = node.attribute("output").value # output attribute, already evaluated upon the node's creation
93+
node._buildCmdVars()
94+
assert desc.Node.internalFolder == node.rootFolder["NODE_CACHE_FOLDER"]
95+
assert cacheFolderNode == desc.Node.internalFolder.format(**node._cmdVars) + "file.out"
96+
97+
# Delete the current graph
98+
del graph
99+
100+
# Unregister that node and replace it with the one from this file
101+
unregisterNodeType(meshroom.core.nodesDesc["PrintAttributes"])
102+
assert "PrintAttributes" not in meshroom.core.nodesDesc.keys()
103+
104+
registerNodeType(PrintAttributes)
105+
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()
106+
107+
# Reload the graph
108+
graph = loadGraph(filename)
109+
assert graph
110+
node = graph.node(name)
111+
assert node.nodeType == "PrintAttributes"
112+
113+
# Check that the relative path is different for the input
114+
assert node.attribute("relativePathInput").getEvalValue() != sourceCodeFolderNode
115+
116+
# Check that it is the same for the cache
117+
assert node.attribute("output").value == cacheFolderNode
118+
119+
os.remove(filename)
120+
unregisterNodeType(PrintAttributes)
121+
122+
123+
def test_updateRootFolders():
124+
"""
125+
Check that root folders can be added and removed.
126+
"""
127+
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
128+
assert "PrintAttributes" in meshroom.core.nodesDesc
129+
130+
node = Node("PrintAttributes")
131+
assert len(node.rootFolder) == 2
132+
133+
# Add a new element in the list of root folders
134+
node.rootFolder.update({"NODE_TEST_FOLDER": "/tmp/"})
135+
assert len(node.rootFolder) == 3
136+
137+
attr = node.attribute("relativePathInput")
138+
139+
sourceCodeFolder = attr.getEvalValue()
140+
attr.value = "${NODE_TEST_FOLDER}" + "input.txt"
141+
assert attr.getEvalValue() == "/tmp/input.txt"
142+
assert attr.getEvalValue() != sourceCodeFolder
143+
144+
# Remove the extra element in the list of root folders
145+
node.rootFolder.pop("NODE_TEST_FOLDER", None)
146+
assert len(node.rootFolder) == 2
147+
148+
assert attr.getEvalValue() != "/tmp/input.txt"
149+
assert attr.getEvalValue() == attr.value
150+
151+
attr.value = attr.defaultValue()
152+
assert attr.getEvalValue() == sourceCodeFolder

0 commit comments

Comments
 (0)