Skip to content

Commit

Permalink
[tests] Add unit tests for the rootFolder variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cbentejac committed Dec 27, 2024
1 parent 8ed376f commit 34e1495
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/nodes/test/printAttributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from meshroom.core import desc

class PrintAttributes(desc.Node):
documentation="Test node printing the values of its attributes."
inputs = [
desc.File(
name="relativePathInput",
label="Relative Input File",
description="Relative path to the input file.",
value="${NODE_SOURCECODE_FOLDER}" + "/input.txt",
),
]

outputs = [
desc.File(
name="output",
label="Output",
description="Path to the output file.",
value="${NODE_CACHE_FOLDER}" + "file.out",
),
]

def processChunk(self, chunk):
print(chunk.node.relativePathInput.value)
print(chunk.node.output.value)

Check warning on line 25 in tests/nodes/test/printAttributes.py

View check run for this annotation

Codecov / codecov/patch

tests/nodes/test/printAttributes.py#L24-L25

Added lines #L24 - L25 were not covered by tests
152 changes: 152 additions & 0 deletions tests/test_nodeRootFolders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# coding:utf-8

from inspect import getfile
import os
from pathlib import Path
from tempfile import mktemp

from meshroom.core.graph import Graph, loadGraph
from meshroom.core import desc, loadAllNodes, registerNodeType, unregisterNodeType
from meshroom.core.node import Node
import meshroom.core

class TestNode(desc.Node):
inputs = []
outputs = []

class PrintAttributes(desc.Node):
# Local "PrintAttributes" node, with a different source code folder as the one in nodes/test
documentation="Test node printing the values of its attributes."
inputs = [
desc.File(
name="relativePathInput",
label="Relative Input File",
description="Relative path to the input file.",
value="${NODE_SOURCECODE_FOLDER}" + "/input.txt",
),
]

outputs = [
desc.File(
name="output",
label="Output",
description="Path to the output file.",
value="${NODE_CACHE_FOLDER}" + "file.out",
),
]

def processChunk(self, chunk):
print(chunk.node.relativePathInput.value)
print(chunk.node.output.value)

Check warning on line 40 in tests/test_nodeRootFolders.py

View check run for this annotation

Codecov / codecov/patch

tests/test_nodeRootFolders.py#L39-L40

Added lines #L39 - L40 were not covered by tests


def test_registerSameNodeWithDifferentLocations():
"""
Check that the nodes with the same description but registered at different locations have different evaluations
for the NODE_SOURCECODE_FOLDER value.
"""
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()

# Node loaded from "nodes/test"
n1 = Node("PrintAttributes")
sourceFolderNode1 = n1.attribute("relativePathInput").getEvalValue()
assert sourceFolderNode1 == Path(getfile(meshroom.core.nodesDesc["PrintAttributes"])).parent.resolve().as_posix() + "/input.txt"
assert Path(sourceFolderNode1).parent.resolve().as_posix() == n1.rootFolder["NODE_SOURCECODE_FOLDER"]

# Unregister that node and replace it with the one from this file
unregisterNodeType(PrintAttributes)
assert "PrintAttributes" not in meshroom.core.nodesDesc.keys()

registerNodeType(PrintAttributes)
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()

n2 = Node("PrintAttributes")
sourceFolderNode2 = n2.attribute("relativePathInput").getEvalValue()
assert sourceFolderNode2 == Path(getfile(meshroom.core.nodesDesc["PrintAttributes"])).parent.resolve().as_posix() + "/input.txt"
assert Path(sourceFolderNode2).parent.resolve().as_posix() == n2.rootFolder["NODE_SOURCECODE_FOLDER"]

assert sourceFolderNode1 is not sourceFolderNode2
unregisterNodeType(PrintAttributes)


def test_reloadGraphWithDifferentNodeLocations():
"""
Save a Graph with a node description registered at a specific location, unregister that node type, and register the
same description from a different location.
"""
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()

graph = Graph('')
node = graph.addNewNode("PrintAttributes")
name = node.name

# Save graph in a file
filename = mktemp()

Check warning on line 86 in tests/test_nodeRootFolders.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_nodeRootFolders.py#L86

Use of insecure and deprecated function (mktemp). (B306)
graph.save(filename)

sourceCodeFolderNode = node.attribute("relativePathInput").getEvalValue()
assert sourceCodeFolderNode == node.rootFolder["NODE_SOURCECODE_FOLDER"] + "/input.txt"

cacheFolderNode = node.attribute("output").value # output attribute, already evaluated upon the node's creation
node._buildCmdVars()
assert desc.Node.internalFolder == node.rootFolder["NODE_CACHE_FOLDER"]
assert cacheFolderNode == desc.Node.internalFolder.format(**node._cmdVars) + "file.out"

# Delete the current graph
del graph

# Unregister that node and replace it with the one from this file
unregisterNodeType(meshroom.core.nodesDesc["PrintAttributes"])
assert "PrintAttributes" not in meshroom.core.nodesDesc.keys()

registerNodeType(PrintAttributes)
assert "PrintAttributes" in meshroom.core.nodesDesc.keys()

# Reload the graph
graph = loadGraph(filename)
assert graph
node = graph.node(name)
assert node.nodeType == "PrintAttributes"

# Check that the relative path is different for the input
assert node.attribute("relativePathInput").getEvalValue() != sourceCodeFolderNode

# Check that it is the same for the cache
assert node.attribute("output").value == cacheFolderNode

os.remove(filename)
unregisterNodeType(PrintAttributes)


def test_updateRootFolders():
"""
Check that root folders can be added and removed.
"""
loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes"))
assert "PrintAttributes" in meshroom.core.nodesDesc

node = Node("PrintAttributes")
assert len(node.rootFolder) == 2

# Add a new element in the list of root folders
node.rootFolder.update({"NODE_TEST_FOLDER": "/tmp/"})

Check warning on line 134 in tests/test_nodeRootFolders.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_nodeRootFolders.py#L134

Probable insecure usage of temp file/directory. (B108)
assert len(node.rootFolder) == 3

attr = node.attribute("relativePathInput")

sourceCodeFolder = attr.getEvalValue()
attr.value = "${NODE_TEST_FOLDER}" + "input.txt"
assert attr.getEvalValue() == "/tmp/input.txt"

Check warning on line 141 in tests/test_nodeRootFolders.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_nodeRootFolders.py#L141

Probable insecure usage of temp file/directory. (B108)
assert attr.getEvalValue() != sourceCodeFolder

# Remove the extra element in the list of root folders
node.rootFolder.pop("NODE_TEST_FOLDER", None)
assert len(node.rootFolder) == 2

assert attr.getEvalValue() != "/tmp/input.txt"

Check warning on line 148 in tests/test_nodeRootFolders.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_nodeRootFolders.py#L148

Probable insecure usage of temp file/directory. (B108)
assert attr.getEvalValue() == attr.value

attr.value = attr.defaultValue()
assert attr.getEvalValue() == sourceCodeFolder

0 comments on commit 34e1495

Please sign in to comment.