Example #1
0
 def test_muted_layers_fallbacks(self):
     print("Test that fallback respects muted layers.")
     # Test base without muting anything
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     expanded = os.path.abspath(os.path.dirname(graph))
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/test_mute')
     expected = os.path.join(expanded, 'dummy.txt').replace(os.sep, '/')
     attr_name = 'filepath'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
     # Mute and test again
     self.stage.top_layer.set_muted(True)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/test_mute')
     expanded2 = os.path.abspath("../test/plugins/fallbacks/base")
     expected = os.path.join(expanded2,
                             'biped_base.txt').replace(os.sep, '/')
     attr_name = 'filepath'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
Example #2
0
    def test_relative_node_paths(self):
        os.chdir(os.path.dirname(__file__))
        test_stage = Session().load_file(filepath='RelPathRef.nxt')
        comp_layer = test_stage.build_stage()
        child_1_path = '/parent/child1'
        child_1 = test_stage.top_layer.lookup(child_1_path)

        test_in_exp = {
            'grandchild1': '/parent/child1/grandchild1',
            '..': '/parent',
            '../child2': '/parent/child2',
            'grandchild1/../../child2': '/parent/child2',
            './././././grandchild1/./././././.': '/parent/child1/grandchild1'
        }
        for inp, exp in test_in_exp.items():
            result = nxt_path.expand_relative_node_path(inp, child_1_path)
            self.assertEqual(exp, result)
        print("Test that a relative node path works as an instance path")
        inst_target = comp_layer.lookup('/parent/inst_target')
        inst_path = getattr(inst_target, INTERNAL_ATTRS.INSTANCE_PATH)
        inst_source_node = comp_layer.lookup(inst_path)
        self.assertIsNotNone(inst_source_node)
        start_node = '/some/node'
        not_rel = '/another/node'
        result = nxt_path.expand_relative_node_path(not_rel, start_node)
        self.assertEqual(result, not_rel)
Example #3
0
    def test_sibling_path(self):
        """Test that resolution of a filename only returns a file next to it.

        NOTE: this test depends on the test folder structure. It is using a
        test graph `TokenSiblingTest.nxt` that must be a direct sibling
        to this test file. The contents of that file are also depended on.
        If you find one of these files without the other, something is wrong.
        """
        base_path = __file__.replace(os.path.sep, '/')
        if base_path.endswith('.pyc'):
            base_path = base_path[:-1]
        base_name = os.path.basename(base_path)
        in_val = '${file::' + base_name + '}'
        expected = base_path
        test_attr = 'my_file'
        my_dir = os.path.dirname(base_path)
        os.chdir(my_dir)
        test_stage = Session().load_file(filepath='TokenSiblingTest.nxt')
        comp_layer = test_stage.build_stage()
        test_node = test_stage.top_layer.lookup('/tester')
        test_stage.add_node_attr(test_node, test_attr, {'value': in_val},
                                 test_stage.top_layer)
        result = test_stage.get_node_attr_value(test_node, test_attr,
                                                comp_layer)
        self.assertEqual(expected, result)
Example #4
0
 def test_file_token(self):
     base_path = __file__.replace(os.path.sep, '/')
     if base_path.endswith('.pyc'):
         base_path = base_path[:-1]
     my_dir = os.path.dirname(base_path)
     os.chdir(my_dir)
     test_stage = Session().load_file(filepath='FileTokenSub.nxt')
     comp_layer = test_stage.build_stage()
     fake_path = os.path.join(my_dir, 'notafile.txt')
     fake_path = fake_path.replace(os.path.sep, '/')
     real_path = os.path.join(my_dir, 'real_file.txt')
     real_path = real_path.replace(os.path.sep, '/')
     test_node = test_stage.top_layer.lookup('/tokens')
     file_fake = test_stage.get_node_attr_value(test_node,
                                                'file_fake',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual('', file_fake)
     file_real = test_stage.get_node_attr_value(test_node,
                                                'file_real',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual(real_path, file_real)
     path_fake = test_stage.get_node_attr_value(test_node,
                                                'path_fake',
                                                comp_layer,
                                                resolved=True)
     self.assertEqual(fake_path, path_fake)
     path_real = test_stage.get_node_attr_value(test_node,
                                                'path_real',
                                                test_stage.top_layer,
                                                resolved=True)
     self.assertEqual(real_path, path_real)
Example #5
0
def execute(args):
    """Executes graph

    :param args: Namespace Object
    :return: None"""
    if not hasattr(args, 'parameters'):
        parameter_list = []  # Legacy does not support parameters
    else:
        parameter_list = args.parameters
    if not hasattr(args, 'start'):
        start = None  # Legacy does not support start points
    else:
        start = args.start

    param_arg_count = len(parameter_list)
    if param_arg_count % 2 != 0:
        raise Exception('Invalid parameters supplied, must be in pattern '
                        '-/node.attr value')
    parameters = {}
    i = 0
    for _ in range(int(param_arg_count / 2)):
        key = parameter_list[i]
        if not key.startswith('/'):
            raise Exception('Invalid attr path key {}, must be '
                            'formatted as /node.attr'.format(key))
        val = parameter_list[i + 1]
        parameters[key] = val
        i += 2
    Session().execute_graph(args.path[0], start, parameters, args.context)
    logger.execinfo('Execution finished!')
Example #6
0
 def test_code_file_tokens(self):
     print("Test that fallback give full path in code")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     expanded = os.path.abspath(os.path.dirname(graph))
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init')
     expected = os.path.join(expanded, 'dummy.txt').replace(os.sep, '/')
     actual = self.stage.get_node_code_string(node, comp_layer,
                                              DATA_STATE.RESOLVED)
     self.assertEqual(expected, actual)
Example #7
0
 def test_auto_update(self):
     self.maxDiff = None
     self.stage = Session().load_file("legacy/0.45.0_TopLayer.nxt")
     self.file_data = self.stage.get_layer_save_data(0)
     self.file_data = json.dumps(self.file_data,
                                 indent=4,
                                 sort_keys=False,
                                 separators=(',', ': '))
     file_path = "legacy/0.45.0_to_LATEST.nxt"
     real_path = nxt_path.full_file_expand(file_path)
     self.proof_data = dynamic_version(real_path)
     self.assertEqual(self.proof_data, self.file_data)
Example #8
0
 def spec_test(self):
     stage = Session().load_file(filepath=filename)
     r = LayerReturnTypes.Node
     for input_node in stage.top_layer.descendants(return_type=r):
         # Only concerned with test input nodes
         name = getattr(input_node, INTERNAL_ATTRS.NAME)
         if not name.startswith('test'):
             continue
         if not name.endswith('input'):
             continue
         input_path = stage.top_layer.get_node_path(input_node)
         rt_layer = stage.execute(input_path)
         # Gather expectations
         expected_resolve_path = input_path.replace('input', 'resolved')
         expected_resolve_node = stage.top_layer.lookup(
             expected_resolve_path)
         expected_cache_path = input_path.replace('input', 'cached')
         expected_cache_node = stage.top_layer.lookup(expected_cache_path)
         cached_node = rt_layer.lookup(input_path)
         # Assert computes are equal
         resolved_code = stage.get_node_code_string(
             input_node,
             layer=stage.top_layer,
             data_state=DATA_STATE.RESOLVED)
         expected_resolve_code = stage.get_node_code_string(
             expected_resolve_node,
             layer=stage.top_layer.lookup,
             data_state=DATA_STATE.RAW)
         self.assertEqual(expected_resolve_code, resolved_code)
         cached_code = getattr(cached_node, INTERNAL_ATTRS.CACHED_CODE)
         expected_cached_code = stage.get_node_code_string(
             expected_cache_node, layer=rt_layer, data_state=DATA_STATE.RAW)
         self.assertEqual(expected_cached_code, cached_code)
         # Assert attrs are equal
         for attr_name in stage.get_node_attr_names(input_node):
             resolved_attr_val = stage.get_node_attr_value(input_node,
                                                           attr_name,
                                                           stage.top_layer,
                                                           resolved=True)
             excpected_resolve_attr_val = stage.get_node_attr_value(
                 expected_resolve_node,
                 attr_name,
                 stage.top_layer,
                 resolved=False)
             self.assertEqual(excpected_resolve_attr_val, resolved_attr_val)
             cached_attr_val = getattr(cached_node, attr_name)
             expected_cached_attr_val = stage.get_node_attr_value(
                 expected_cache_node, attr_name, stage.top_layer)
             self.assertEqual(expected_cached_attr_val, cached_attr_val)
Example #9
0
 def test_nested_file_tokens(self):
     print("Test nested file tokens")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     cwd = "../test/plugins/fallbacks/another"
     expanded = os.path.abspath(cwd)
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init/node')
     expected = os.path.join(expanded, 'dummy.txt').replace(os.sep, '/')
     attr_name = 'nested'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
Example #10
0
 def test_token_resolve_to_nothing(self):
     print("Test a token containing failed attr subs returns nothing.")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     cwd = "../test/plugins/fallbacks/base"
     expanded = os.path.abspath(cwd)
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init/node4')
     expected = ''
     attr_name = 'nothing'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
Example #11
0
 def test_instance_fallbacks(self):
     print("Test that fallback work with instances.")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     fb_graph = "../test/plugins/fallbacks/base/fallbacks2.nxt"
     expanded = os.path.abspath(os.path.dirname(fb_graph))
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init2/node')
     expected = os.path.join(expanded,
                             'biped_base.txt').replace(os.sep, '/')
     attr_name = 'filepath'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
Example #12
0
 def test_contents_token(self):
     base_path = __file__.replace(os.path.sep, '/')
     if base_path.endswith('.pyc'):
         base_path = base_path[:-1]
     my_dir = os.path.dirname(base_path)
     os.chdir(my_dir)
     test_stage = Session().load_file(filepath='FileTokenSub.nxt')
     with open('real_file.txt', 'r') as fp:
         expected_code = fp.read()
     top = test_stage.top_layer
     node = top.lookup('/tokens')
     resolved = DATA_STATE.RESOLVED
     found_code = test_stage.get_node_code_string(node=node,
                                                  layer=top,
                                                  data_state=resolved)
     self.assertEqual(expected_code, found_code)
Example #13
0
 def test_multiline_code_resolve(self):
     """When an attr has "real" newline and is subsituted into code,
     resolve that into multiple code lines
     """
     test_graph = get_test_file_path("ResolveNewlines.nxt")
     stage = Session().load_file(test_graph)
     comp_layer = stage.build_stage()
     node=comp_layer.lookup("/test_node")
     # Unresolved has 1 line
     raw_lines = stage.get_node_code_lines(node,
                                           layer=comp_layer,
                                           data_state=DATA_STATE.RAW)
     assert len(raw_lines) == 1
     resolved_lines = stage.get_node_code_lines(node,
                                                layer=comp_layer,
                                                data_state=DATA_STATE.RESOLVED)
     assert len(resolved_lines) == 2
Example #14
0
 def test_multi_file_tokens(self):
     print("Test that an attr with multiple file tokens resolves as "
           "expected (using Stage.resolve_file_token).")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     p1 = os.path.abspath(os.path.dirname(graph)).replace(os.sep, '/')
     p2 = os.path.abspath(os.path.join(p1, '..', 'base',
                                       'biped_base.txt')).replace(
                                           os.sep, '/')
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init/node2')
     expected = "['{}', '', '{}']".format(p1, p2)
     attr_name = 'limitation'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(expected, actual)
Example #15
0
 def test_env_var(self):
     print("Test that NXT_FILE_ROOTS env var works.")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     cwd = "../test/plugins/fallbacks/base"
     expanded = os.path.abspath(cwd)
     os.environ['NXT_FILE_ROOTS'] = expanded
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init/node3')
     expected = os.path.join(expanded,
                             'biped_base.txt').replace(os.sep, '/')
     attr_name = 'env'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     os.environ.pop('NXT_FILE_ROOTS')
     self.assertEqual(expected, actual)
Example #16
0
 def test_file_list(self):
     print("Test file list token works.")
     graph = "../test/plugins/fallbacks/another/fallbacks_top.nxt"
     cwd = "../test/plugins/fallbacks/base"
     expanded = os.path.abspath(cwd)
     self.stage = Session().load_file(graph)
     comp_layer = self.stage.build_stage()
     node = comp_layer.lookup('/init/node')
     expected = [
         os.path.join(expanded, 'biped_base.txt').replace(os.sep, '/'),
         os.path.join(expanded, 'quad_base.txt').replace(os.sep, '/')
     ]
     attr_name = 'file_list'
     actual = self.stage.resolve(node,
                                 getattr(node, attr_name),
                                 comp_layer,
                                 attr_name=attr_name)
     self.assertEqual(str(expected), actual)
Example #17
0
def execute(args):
    """Executes graph

    :param args: Namespace Object
    :return: None"""
    if not hasattr(args, 'parameters'):
        parameter_list = []  # Legacy does not support parameters
    else:
        parameter_list = args.parameters
    if not hasattr(args, 'start'):
        start = None  # Legacy does not support start points
    else:
        start = args.start

    param_arg_count = len(parameter_list)
    if param_arg_count == 1:
        # Read file for parameters
        param_path = parameter_list[0]
        if not os.path.isfile(param_path):
            msg = 'Single parameter passed, expected it to be valid parameters file. However, "{}" does not exist'.format(
                param_path)
            raise IOError(msg)
        with open(param_path, 'r') as fp:
            parameters = json.load(fp)
    elif param_arg_count % 2 != 0:
        raise Exception('Invalid parameters supplied, must be 1 file path or '
                        'pattern: "/node.attr value"')
    else:
        # Parse cli formatted input
        parameters = {}
        i = 0
        for _ in range(int(param_arg_count / 2)):
            key = parameter_list[i]
            if not key.startswith('/'):
                raise Exception('Invalid attr path key {}, must be '
                                'formatted as /node.attr'.format(key))
            val = parameter_list[i + 1]
            parameters[key] = val
            i += 2
    context = getattr(args, 'context', None)
    Session().execute_graph(args.path[0], start, parameters, context)
    logger.execinfo('Execution finished!')
Example #18
0
 def test_python_entry(self):
     my_session = Session()
     rtl = my_session.execute_graph('StageRuntimeScope.nxt')
     self.assertIsNotNone(rtl)
Example #19
0
 def setUpClass(cls):
     os.chdir(os.path.dirname(__file__))
     cls.stage = Session().load_file(filepath="StageInstanceTest.nxt")
     cls.model = stage_model.StageModel(cls.stage)
     cls.comp_layer = cls.model.comp_layer
Example #20
0
 def setUp(self):
     self.session = Session()
     self.test_filename = 'NotAFile.txt'
     self.test_input = '${path::' + self.test_filename + '}'
Example #21
0
 def setUp(self):
     os.chdir(os.path.dirname(__file__))
     self.stage = Session().load_file(filepath="test_sub_graph.nxt")
Example #22
0
 def test_python_entry(self):
     my_session = Session()
     rtl = my_session.execute_graph(self.graph_path)
     self.assertIsNotNone(rtl)