utils.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 synapse.config.homeserver import HomeServerConfig
  21. class ConfigFileTestCase(unittest.TestCase):
  22. def setUp(self):
  23. self.dir = tempfile.mkdtemp()
  24. self.config_file = os.path.join(self.dir, "homeserver.yaml")
  25. def tearDown(self):
  26. shutil.rmtree(self.dir)
  27. def generate_config(self):
  28. with redirect_stdout(StringIO()):
  29. HomeServerConfig.load_or_generate_config(
  30. "",
  31. [
  32. "--generate-config",
  33. "-c",
  34. self.config_file,
  35. "--report-stats=yes",
  36. "-H",
  37. "lemurs.win",
  38. ],
  39. )
  40. def generate_config_and_remove_lines_containing(self, needle):
  41. self.generate_config()
  42. with open(self.config_file) as f:
  43. contents = f.readlines()
  44. contents = [line for line in contents if needle not in line]
  45. with open(self.config_file, "w") as f:
  46. f.write("".join(contents))
  47. def add_lines_to_config(self, lines):
  48. with open(self.config_file, "a") as f:
  49. for line in lines:
  50. f.write(line + "\n")