one_time_log_fixture.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. Fixture for creating a single-use log to read instead of parsing through the bundled logs
  6. """
  7. import pytest
  8. import os
  9. import subprocess
  10. import time
  11. import ly_test_tools.environment.file_system as fs
  12. class Single_Use_Log:
  13. """
  14. Class used during test, can use create_log_file multiple times during test but will completely overwrite
  15. """
  16. def __init__(self, workspace):
  17. seconds = str(time.time())[-6:]
  18. self.file_name = "Single_Use_Log" + seconds + ".txt"
  19. self.log_path = os.path.join(workspace.paths.bin(), "logs", self.file_name)
  20. def create_log_file(self, commands):
  21. with open(self.log_path, "w") as log_file:
  22. subprocess.check_call(commands, stdout=log_file)
  23. def cleanup(self):
  24. fs.delete([self.log_path], True, False)
  25. @pytest.fixture
  26. def one_time_log_fixture(request, workspace) -> Single_Use_Log:
  27. """
  28. Pytest Fixture for setting up a single use log file
  29. At test conclusion, runs the cleanup to delete the single use text file
  30. :return: Single_Use_Log class
  31. """
  32. log_class = Single_Use_Log(workspace)
  33. request.addfinalizer(log_class.cleanup)
  34. return log_class