1
0

utils.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import shutil
  16. import tempfile
  17. import unittest
  18. from contextlib import redirect_stdout
  19. from io import StringIO
  20. from typing import List
  21. from synapse.config.homeserver import HomeServerConfig
  22. class ConfigFileTestCase(unittest.TestCase):
  23. def setUp(self) -> None:
  24. self.dir = tempfile.mkdtemp()
  25. self.config_file = os.path.join(self.dir, "homeserver.yaml")
  26. def tearDown(self) -> None:
  27. shutil.rmtree(self.dir)
  28. def generate_config(self) -> None:
  29. with redirect_stdout(StringIO()):
  30. HomeServerConfig.load_or_generate_config(
  31. "",
  32. [
  33. "--generate-config",
  34. "-c",
  35. self.config_file,
  36. "--report-stats=yes",
  37. "-H",
  38. "lemurs.win",
  39. ],
  40. )
  41. def generate_config_and_remove_lines_containing(self, needle: str) -> None:
  42. self.generate_config()
  43. with open(self.config_file) as f:
  44. contents = f.readlines()
  45. contents = [line for line in contents if needle not in line]
  46. with open(self.config_file, "w") as f:
  47. f.write("".join(contents))
  48. def add_lines_to_config(self, lines: List[str]) -> None:
  49. with open(self.config_file, "a") as f:
  50. for line in lines:
  51. f.write(line + "\n")