Mercurial > libervia-backend
annotate tests/e2e/libervia-cli/conftest.py @ 4340:ea72364131d5 default tip @
doc (components): Update Email Gateway documentation:
A section has been added to explain how attachments are handled.
fix 453
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 03 Dec 2024 00:53:18 +0100 |
parents | 4cd4922de876 |
children |
rev | line source |
---|---|
3415 | 1 #!/usr/bin/env python3 |
2 | |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
3 # Libervia: an XMPP client |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
3415 | 5 |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 import sys | |
20 import os | |
21 import tempfile | |
22 from pathlib import Path | |
23 from textwrap import dedent | |
24 import json | |
25 import pytest | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
26 from sh import li |
3415 | 27 |
28 | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
29 class LiberviaCliJson: |
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
30 """li like commands parsing result as JSON""" |
3415 | 31 |
32 def __init__(self): | |
33 self.subcommands = [] | |
34 | |
35 def __call__(self, *args, **kwargs): | |
36 args = self.subcommands + list(args) | |
37 self.subcommands.clear() | |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
38 kwargs["output"] = "json-raw" |
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
39 kwargs["_tty_out"] = False |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
40 cmd = li(*args, **kwargs) |
4227 | 41 return json.loads(cmd) |
3415 | 42 |
43 def __getattr__(self, name): | |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
44 if name.startswith("_"): |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
45 # no li subcommand starts with a "_", |
3415 | 46 # and pytest uses some attributes with this name scheme |
47 return super().__getattr__(name) | |
48 self.subcommands.append(name) | |
49 return self | |
50 | |
51 | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
52 class LiberviaCliElt(LiberviaCliJson): |
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
53 """li like commands parsing result as domishElement""" |
3415 | 54 |
55 def __init__(self): | |
56 super().__init__() | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3659
diff
changeset
|
57 from libervia.backend.tools.xml_tools import ElementParser |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
58 |
3415 | 59 self.parser = ElementParser() |
60 | |
61 def __call__(self, *args, **kwargs): | |
62 args = self.subcommands + list(args) | |
63 self.subcommands.clear() | |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
64 kwargs["output"] = "xml-raw" |
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
65 kwargs["_tty_out"] = False |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
66 cmd = li(*args, **kwargs) |
4227 | 67 return self.parser(cmd.strip()) |
3415 | 68 |
69 | |
70 class Editor: | |
71 | |
72 def __init__(self): | |
73 # temporary directory will be deleted Automatically when this object will be | |
74 # destroyed | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
75 self.tmp_dir_obj = tempfile.TemporaryDirectory(prefix="libervia_e2e_test_editor_") |
3415 | 76 self.tmp_dir_path = Path(self.tmp_dir_obj.name) |
77 if not sys.executable: | |
78 raise Exception("Can't find python executable") | |
79 self.editor_set = False | |
80 self.editor_path = self.tmp_dir_path / "editor.py" | |
81 self.ori_content_path = self.tmp_dir_path / "original_content" | |
82 self.new_content_path = self.tmp_dir_path / "new_content" | |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
83 self.base_script = dedent( |
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
84 f"""\ |
3415 | 85 #!{sys.executable} |
86 import sys | |
87 | |
88 def content_filter(content): | |
89 return {{content_filter}} | |
90 | |
91 with open(sys.argv[1], 'r+') as f: | |
92 original_content = f.read() | |
93 f.seek(0) | |
94 new_content = content_filter(original_content) | |
95 f.write(new_content) | |
96 f.truncate() | |
97 | |
98 with open("{self.ori_content_path}", "w") as f: | |
99 f.write(original_content) | |
100 | |
101 with open("{self.new_content_path}", "w") as f: | |
102 f.write(new_content) | |
103 """ | |
104 ) | |
105 self._env = os.environ.copy() | |
106 self._env["EDITOR"] = str(self.editor_path) | |
107 | |
108 def set_filter(self, content_filter: str = "content"): | |
109 """Python code to modify original content | |
110 | |
111 The code will be applied to content received by editor. | |
112 The original content received by editor is in the "content" variable. | |
113 If filter_ is not specified, original content is written unmodified. | |
114 Code must be on a single line. | |
115 """ | |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
116 if "\n" in content_filter: |
3415 | 117 raise ValueError("new lines can't be used in filter_") |
4305
4cd4922de876
tests: reformat tests using black.
Goffi <goffi@goffi.org>
parents:
4227
diff
changeset
|
118 with self.editor_path.open("w") as f: |
3415 | 119 f.write(self.base_script.format(content_filter=content_filter)) |
120 self.editor_path.chmod(0o700) | |
121 self.editor_set = True | |
122 | |
123 @property | |
124 def env(self): | |
125 """Get environment variable with the editor set""" | |
126 if not self.editor_set: | |
127 self.set_filter() | |
128 return self._env | |
129 | |
130 @property | |
131 def original_content(self): | |
132 """Last content received by editor, before any modification | |
133 | |
134 returns None if editor has not yet been called | |
135 """ | |
136 try: | |
137 with self.ori_content_path.open() as f: | |
138 return f.read() | |
139 except FileNotFoundError: | |
140 return None | |
141 | |
142 @property | |
143 def new_content(self): | |
144 """Last content writen by editor | |
145 | |
146 This is the final content, after filter has been applied to original content | |
147 returns None if editor has not yet been called | |
148 """ | |
149 try: | |
150 with self.new_content_path.open() as f: | |
151 return f.read() | |
152 except FileNotFoundError: | |
153 return None | |
154 | |
155 | |
156 @pytest.fixture(scope="session") | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
157 def li_json(): |
3659
d4b8f1e8f0b4
tests (e2e/cli): update JSON and XML outputs following changes in Libervia CLI
Goffi <goffi@goffi.org>
parents:
3498
diff
changeset
|
158 """Run li with "json-raw" output, and returns the parsed value""" |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
159 return LiberviaCliJson() |
3415 | 160 |
161 | |
162 @pytest.fixture(scope="session") | |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
163 def li_elt(): |
3659
d4b8f1e8f0b4
tests (e2e/cli): update JSON and XML outputs following changes in Libervia CLI
Goffi <goffi@goffi.org>
parents:
3498
diff
changeset
|
164 """Run li with "xml-raw" output, and returns the parsed value""" |
3498
d78b5eae912a
tests: update following names change
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
165 return LiberviaCliElt() |
3415 | 166 |
167 | |
168 @pytest.fixture() | |
169 def editor(): | |
170 """Create a fake editor to automatise edition from CLI""" | |
171 return Editor() |