Skip to content

Commit 53206f9

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

File tree

2 files changed

+167
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)