Add is_file_in_dir helper function

This commit is contained in:
Matthias
2023-07-25 20:19:23 +02:00
parent e39af17207
commit 1d39cc18bf
2 changed files with 26 additions and 1 deletions

View File

@@ -116,6 +116,13 @@ def file_load_json(file: Path):
return pairdata
def is_file_in_dir(file: Path, directory: Path) -> bool:
"""
Helper function to check if file is in directory.
"""
return file.is_file() and file.parent.samefile(directory)
def pair_to_filename(pair: str) -> str:
for ch in ['/', ' ', '.', '@', '$', '+', ':']:
pair = pair.replace(ch, '_')

View File

@@ -8,7 +8,7 @@ import pandas as pd
import pytest
from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json,
file_load_json, json_to_dataframe, pair_to_filename,
file_load_json, is_file_in_dir, json_to_dataframe, pair_to_filename,
parse_db_uri_for_logging, plural, render_template,
render_template_with_fallback, round_coin_value, safe_value_fallback,
safe_value_fallback2)
@@ -64,6 +64,24 @@ def test_file_load_json(mocker, testdatadir) -> None:
assert ret
def test_is_file_in_dir(tmp_path):
# Create a temporary directory and file
dir_path = tmp_path / "subdir"
dir_path.mkdir()
file_path = dir_path / "test.txt"
file_path.touch()
# Test that the function returns True when the file is in the directory
assert is_file_in_dir(file_path, dir_path) is True
# Test that the function returns False when the file is not in the directory
assert is_file_in_dir(file_path, tmp_path) is False
file_path2 = tmp_path / "../../test2.txt"
assert is_file_in_dir(file_path2, tmp_path) is False
@pytest.mark.parametrize("pair,expected_result", [
("ETH/BTC", 'ETH_BTC'),
("ETH/USDT", 'ETH_USDT'),