Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hip script input validation #99

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lifeblood/stock_nodes/houdini/nodes/hip_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def process_task(self, context) -> ProcessingResult:
script = 'import os, hou\n'

source_hip = context.param_value('hip path')
if not source_hip:
raise ProcessingError('hip path is empty')
dest_hip = source_hip
if context.param_value('save different hip'):
dest_hip = context.param_value('save hip path')
Expand All @@ -69,7 +71,12 @@ def process_task(self, context) -> ProcessingResult:
'hou.hipFile.addEventCallback(__fix_hip_env__)\n'

script += 'def __main_body__():\n'
script += '\n'.join(f' {line}' for line in context.param_value('script').splitlines())

code_lines = '\n'.join(f' {line}' for line in context.param_value('script').splitlines())
if code_lines.strip() == '':
script += ' pass'
else:
script += code_lines

script += '\n\n' \
f'hou.hipFile.load({repr(source_hip)}, ignore_load_warnings=True)\n' \
Expand Down
60 changes: 60 additions & 0 deletions tests/nodes/test_hip_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import random
import ast
from asyncio import Event
from lifeblood.scheduler import Scheduler
from lifeblood.worker import Worker
from lifeblood.nodethings import ProcessingError
from lifeblood_testing_common.nodes_common import TestCaseBase, PseudoContext

from typing import List


class TestHipScript(TestCaseBase):
async def test_trivial(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '/tmp/some/path/stuff.hip')
node.set_param_value('script', 'do_things()')

res = context.process_task(node, task)
script = res.invocation_job.extra_files()['work_to_do.py']
ast.parse(script)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_script(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '/tmp/some/path/stuff.hip')
node.set_param_value('script', '')

res = context.process_task(node, task)
script = res.invocation_job.extra_files()['work_to_do.py']
ast.parse(script)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_hip_path(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '')
node.set_param_value('script', '')

self.assertRaises(ProcessingError, context.process_task, node, task)

await self._helper_test_node_with_arg_update(
_logic
)
Loading