Skip to content

Commit

Permalink
tests: config generaton allow multiple needles
Browse files Browse the repository at this point in the history
  • Loading branch information
V02460 committed Dec 2, 2024
1 parent 5257e3b commit 9367d18
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions tests/config/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

class ConfigLoadingFileTestCase(ConfigFileTestCase):
def test_load_fails_if_server_name_missing(self) -> None:
self.generate_config_and_remove_lines_containing("server_name")
self.generate_config_and_remove_lines_containing(["server_name"])
with self.assertRaises(ConfigError):
HomeServerConfig.load_config("", ["-c", self.config_file])
with self.assertRaises(ConfigError):
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_generates_and_loads_macaroon_secret_key(self) -> None:
)

def test_load_succeeds_if_macaroon_secret_key_missing(self) -> None:
self.generate_config_and_remove_lines_containing("macaroon")
self.generate_config_and_remove_lines_containing(["macaroon"])
config1 = HomeServerConfig.load_config("", ["-c", self.config_file])
config2 = HomeServerConfig.load_config("", ["-c", self.config_file])
config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_disable_registration(self) -> None:
self.assertTrue(config3.registration.enable_registration)

def test_stats_enabled(self) -> None:
self.generate_config_and_remove_lines_containing("enable_metrics")
self.generate_config_and_remove_lines_containing(["enable_metrics"])
self.add_lines_to_config(["enable_metrics: true"])

# The default Metrics Flags are off by default.
Expand Down Expand Up @@ -164,7 +164,7 @@ def test_secret_files_missing(self, config_str: str) -> None:
def test_secret_files_existing(
self, config_line: str, get_secret: Callable[[RootConfig], str]
) -> None:
self.generate_config_and_remove_lines_containing("registration_shared_secret")
self.generate_config_and_remove_lines_containing(["registration_shared_secret"])
with tempfile.NamedTemporaryFile(buffering=0) as secret_file:
secret_file.write(b"53C237")

Expand Down
5 changes: 3 additions & 2 deletions tests/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def generate_config(self) -> None:
],
)

def generate_config_and_remove_lines_containing(self, needle: str) -> None:
def generate_config_and_remove_lines_containing(self, needles: list[str]) -> None:
self.generate_config()

with open(self.config_file) as f:
contents = f.readlines()
contents = [line for line in contents if needle not in line]
for needle in needles:
contents = [line for line in contents if needle not in line]
with open(self.config_file, "w") as f:
f.write("".join(contents))

Expand Down

0 comments on commit 9367d18

Please sign in to comment.