Example #1
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 #2
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 #3
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 #4
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 #5
0
class SubGraphs(unittest.TestCase):
    def setUp(self):
        os.chdir(os.path.dirname(__file__))
        self.stage = Session().load_file(filepath="test_sub_graph.nxt")

    def test_basic_run(self):
        """Test that a sub graph is run. The node that is run calls a sub-graph
        that creates a file. If that file is there, we can assert the garph has
        run.
        """
        expected_file = os.path.join(os.path.dirname(__file__),
                                     'sub_graph_file.txt')
        # The file must not exist if we are going to believe the graph made it.
        if os.path.isfile(expected_file):
            os.remove(expected_file)
        self.assertFalse(os.path.isfile(expected_file))
        self.stage.execute(start='/basic_file')
        self.assertTrue(os.path.isfile(expected_file))
        # Clean up after the test
        os.remove(expected_file)

    def test_parameters(self):
        """Test that local attributes of a node that calls a sub graph are
        pushed to the sub graph world node, and that resulting attributes of
        the sub graph world node are pulled back into the calling node.
        """
        target_path = '/specific_file'
        comp_layer = self.stage.build_stage()
        specific_node = comp_layer.lookup(target_path)
        expected_file = self.stage.get_node_attr_value(specific_node,
                                                       'location', comp_layer)
        # The file must not exist if we are going to believe the graph made it.
        if os.path.isfile(expected_file):
            os.remove(expected_file)
        self.assertFalse(os.path.isfile(expected_file))
        self.stage.execute(start=target_path)
        self.assertTrue(os.path.isfile(expected_file))
        # Clean up after the test
        os.remove(expected_file)
Example #6
0
class TestFallbacks(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        os.chdir(os.path.dirname(os.path.realpath(__file__)))

    def test_fallbacks(self):
        print("Test that fallbacks 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, '/')
        attr_name = 'filepath'
        actual = self.stage.resolve(node,
                                    getattr(node, attr_name),
                                    comp_layer,
                                    attr_name=attr_name)
        self.assertEqual(expected, actual)

    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)

    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)

    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)

    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)

    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)

    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)

    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)

    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)