1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
2 | # contributor license agreements. See the NOTICE file distributed with |
||
3 | # this work for additional information regarding copyright ownership. |
||
4 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
5 | # (the "License"); you may not use this file except in compliance with |
||
6 | # the License. You may obtain a copy of the License at |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
10 | # Unless required by applicable law or agreed to in writing, software |
||
11 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | # See the License for the specific language governing permissions and |
||
14 | # limitations under the License. |
||
15 | |||
16 | from __future__ import absolute_import |
||
17 | import os |
||
18 | |||
19 | from st2common.content.loader import ContentPackLoader |
||
20 | from st2common.content.loader import MetaLoader |
||
21 | from st2common.constants.pack import MANIFEST_FILE_NAME |
||
22 | from st2common.util.pack import get_pack_ref_from_metadata |
||
23 | from st2common.exceptions.content import ParseException |
||
24 | from st2common.bootstrap.aliasesregistrar import AliasesRegistrar |
||
25 | from st2common.models.utils.action_alias_utils import extract_parameters_for_action_alias_db |
||
26 | from st2common.models.utils.action_alias_utils import extract_parameters |
||
27 | from st2tests.pack_resource import BasePackResourceTestCase |
||
28 | |||
29 | __all__ = [ |
||
30 | 'BaseActionAliasTestCase' |
||
31 | ] |
||
32 | |||
33 | |||
34 | class BaseActionAliasTestCase(BasePackResourceTestCase): |
||
35 | """ |
||
36 | Base class for testing action aliases. |
||
37 | """ |
||
38 | |||
39 | action_alias_name = None |
||
40 | action_alias_db = None |
||
41 | |||
42 | def setUp(self): |
||
43 | super(BaseActionAliasTestCase, self).setUp() |
||
44 | |||
45 | if not self.action_alias_name: |
||
46 | raise ValueError('"action_alias_name" class attribute needs to be provided') |
||
47 | |||
48 | self.action_alias_db = self._get_action_alias_db_by_name(name=self.action_alias_name) |
||
49 | |||
50 | def assertCommandMatchesExactlyOneFormatString(self, format_strings, command): |
||
51 | """ |
||
52 | Assert that the provided command matches exactly one format string from the provided list. |
||
53 | """ |
||
54 | matched_format_strings = [] |
||
55 | |||
56 | for format_string in format_strings: |
||
57 | try: |
||
58 | extract_parameters(format_str=format_string, |
||
59 | param_stream=command) |
||
60 | except ParseException: |
||
61 | continue |
||
62 | |||
63 | matched_format_strings.append(format_string) |
||
64 | |||
65 | if len(matched_format_strings) == 0: |
||
66 | msg = ('Command "%s" didn\'t match any of the provided format strings' % (command)) |
||
67 | raise AssertionError(msg) |
||
68 | elif len(matched_format_strings) > 1: |
||
69 | msg = ('Command "%s" matched multiple format strings: %s' % |
||
70 | (command, ', '.join(matched_format_strings))) |
||
71 | raise AssertionError(msg) |
||
72 | |||
73 | def assertExtractedParametersMatch(self, format_string, command, parameters): |
||
74 | """ |
||
75 | Assert that the provided command matches the format string. |
||
76 | |||
77 | In addition to that, also assert that the parameters which have been extracted from the |
||
78 | user input (command) also match the provided parameters. |
||
79 | """ |
||
80 | extracted_params = extract_parameters_for_action_alias_db( |
||
81 | action_alias_db=self.action_alias_db, |
||
82 | format_str=format_string, |
||
83 | param_stream=command) |
||
84 | |||
85 | if extracted_params != parameters: |
||
86 | msg = ('Extracted parameters from command string "%s" against format string "%s"' |
||
87 | ' didn\'t match the provided parameters: ' % (command, format_string)) |
||
88 | |||
89 | # Note: We intercept the exception so we can can include diff for the dictionaries |
||
90 | try: |
||
91 | self.assertEqual(extracted_params, parameters) |
||
92 | except AssertionError as e: |
||
93 | msg += str(e) |
||
94 | |||
95 | raise AssertionError(msg) |
||
96 | |||
97 | def _get_action_alias_db_by_name(self, name): |
||
98 | """ |
||
99 | Retrieve ActionAlias DB object for the provided alias name. |
||
100 | """ |
||
101 | base_pack_path = self._get_base_pack_path() |
||
102 | pack_yaml_path = os.path.join(base_pack_path, MANIFEST_FILE_NAME) |
||
103 | |||
104 | if os.path.isfile(pack_yaml_path): |
||
105 | # 1. 1st try to infer pack name from pack metadata file |
||
106 | meta_loader = MetaLoader() |
||
107 | pack_metadata = meta_loader.load(pack_yaml_path) |
||
108 | pack = get_pack_ref_from_metadata(metadata=pack_metadata) |
||
109 | else: |
||
110 | # 2. If pack.yaml is not available, fail back to directory name |
||
111 | # Note: For this to work, directory name needs to match pack name |
||
112 | _, pack = os.path.split(base_pack_path) |
||
113 | |||
114 | pack_loader = ContentPackLoader() |
||
115 | registrar = AliasesRegistrar(use_pack_cache=False) |
||
116 | |||
117 | aliases_path = pack_loader.get_content_from_pack(pack_dir=base_pack_path, |
||
118 | content_type='aliases') |
||
119 | aliases = registrar._get_aliases_from_pack(aliases_dir=aliases_path) |
||
0 ignored issues
–
show
|
|||
120 | for alias_path in aliases: |
||
121 | action_alias_db = registrar._get_action_alias_db(pack=pack, |
||
0 ignored issues
–
show
It seems like
_get_action_alias_db was declared protected and should not be accessed from this context.
Prefixing a member variable class MyParent:
def __init__(self):
self._x = 1;
self.y = 2;
class MyChild(MyParent):
def some_method(self):
return self._x # Ok, since accessed from a child class
class AnotherClass:
def some_method(self, instance_of_my_child):
return instance_of_my_child._x # Would be flagged as AnotherClass is not
# a child class of MyParent
Loading history...
|
|||
122 | action_alias=alias_path) |
||
123 | |||
124 | if action_alias_db.name == name: |
||
125 | return action_alias_db |
||
126 | |||
127 | raise ValueError('Alias with name "%s" not found' % (name)) |
||
128 |
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: