test_filename.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import pathlib
  12. import tempfile
  13. from taipy.gui import Gui
  14. from taipy.gui.utils import _get_non_existent_file_path
  15. def test_empty_file_name(gui: Gui, helpers):
  16. assert _get_non_existent_file_path(pathlib.Path(tempfile.gettempdir()), "").name
  17. def test_non_existent_file(gui: Gui, helpers):
  18. assert not _get_non_existent_file_path(pathlib.Path(tempfile.gettempdir()), "").exists()
  19. def test_existent_file(gui: Gui, helpers):
  20. file_path = _get_non_existent_file_path(pathlib.Path(tempfile.gettempdir()), "")
  21. with open(file_path, "w") as file_handler:
  22. file_handler.write("hello")
  23. assert file_path.exists()
  24. file_stem = file_path.stem.split(".", 1)[0]
  25. file_suffix = file_path.suffixes[-1]
  26. index = int(file_path.suffixes[0][1:]) if len(file_path.suffixes) > 1 else -1
  27. file_path = _get_non_existent_file_path(pathlib.Path(tempfile.gettempdir()), "")
  28. assert file_path.name == f"{file_stem}.{index + 1}{file_suffix}"
  29. with open(file_path, "w") as file_handler:
  30. file_handler.write("hello 2")
  31. assert file_path.exists()
  32. file_path = _get_non_existent_file_path(pathlib.Path(tempfile.gettempdir()), "")
  33. assert file_path.name == f"{file_stem}.{index + 2}{file_suffix}"