예제 #1
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)
예제 #2
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)
예제 #3
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)