Skip to content

Commit

Permalink
Fix test_logs_dir_not_a_dir integration-test (#3987)
Browse files Browse the repository at this point in the history
* Add assertion to assure test is set up properly

* Replace `glob()` with `Path.isfile()`

* Fix logs path
  • Loading branch information
MichaelYochpaz committed Jan 28, 2024
1 parent 9679334 commit b3a6f3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ jobs:
source $(poetry env info --path)/bin/activate
cd content
mkdir -p test-pre-commit-command/logs
export DEMISTO_SDK_LOG_FILE_PATH=./test-pre-commit-command/logs/demisto_sdk_debug.log
export DEMISTO_SDK_LOG_FILE_PATH=./test-pre-commit-command/logs
echo "# test" >> Packs/HelloWorld/Integrations/HelloWorld/HelloWorld.yml
echo "# test" >> Packs/CortexXDR/Integrations/CortexXDRIR/CortexXDRIR.yml
echo "# test" >> Packs/QRadar/Integrations/QRadar_v3/QRadar_v3.yml
Expand Down Expand Up @@ -333,7 +333,7 @@ jobs:
source $(poetry env info --path)/bin/activate
cd content
mkdir -p test-graph-commands/logs
export DEMISTO_SDK_LOG_FILE_PATH=./test-graph-commands/logs/demisto_sdk_debug.log
export DEMISTO_SDK_LOG_FILE_PATH=./test-graph-commands/logs
# create content graph from scratch
demisto-sdk create-content-graph
# clean import folder
Expand Down
26 changes: 9 additions & 17 deletions demisto_sdk/tests/integration_tests/logger_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def test_default_logs_dir(mocker):
runner.invoke(main, ["validate", "-a"])

assert logs_dir_replacement.exists() # Assure 'logs' dir is generated by the code
assert list(logs_dir_replacement.glob("*")) == [
logs_dir_replacement / "demisto_sdk_debug.log"
]
assert (logs_dir_replacement / "demisto_sdk_debug.log").is_file()
shutil.rmtree(root_dir)


Expand All @@ -56,9 +54,7 @@ def test_custom_logs_dir_set_by_flag(mocker):
runner = CliRunner(mix_stderr=False)
runner.invoke(main, ["validate", "-a", "--log-file-path", str(custom_logs_dir)])

assert list(custom_logs_dir.glob("*")) == [
custom_logs_dir / "demisto_sdk_debug.log"
]
assert (custom_logs_dir / "demisto_sdk_debug.log").is_file()
assert (
len(list(default_logs_dir.glob("*"))) == 0
) # Assure default logs dir is not used
Expand All @@ -85,9 +81,7 @@ def test_custom_logs_dir_set_by_env_var(mocker, monkeypatch):
runner = CliRunner(mix_stderr=False)
runner.invoke(main, ["validate", "-a", "--log-file-path", str(custom_logs_dir)])

assert list(custom_logs_dir.glob("*")) == [
custom_logs_dir / "demisto_sdk_debug.log"
]
assert (custom_logs_dir / "demisto_sdk_debug.log").is_file()
assert (
len(list(default_logs_dir.glob("*"))) == 0
) # Assure default logs dir is not used
Expand Down Expand Up @@ -121,9 +115,7 @@ def test_custom_logs_dir_does_not_exist(mocker):
logger_warning.call_args_list,
f"Log file path '{custom_logs_dir}' does not exist and will be created.",
)
assert list(custom_logs_dir.glob("*")) == [
custom_logs_dir / "demisto_sdk_debug.log"
]
assert (custom_logs_dir / "demisto_sdk_debug.log").is_file()
assert (
len(list(default_logs_dir.glob("*"))) == 0
) # Assure default logs dir is not used
Expand All @@ -146,24 +138,24 @@ def test_logs_dir_not_a_dir(mocker):
custom_logs_dir = (
Path(mkdtemp(prefix=TEMP_DIRS_PREFIX)).resolve() / "some_random_file"
)
custom_logs_dir_parent = custom_logs_dir.parent
custom_logs_dir.touch()
assert custom_logs_dir.is_file() # Ensure file was created successfully

mocker.patch("demisto_sdk.commands.common.logger.LOGS_DIR", new=default_logs_dir)
logger_warning = mocker.patch.object(logging.getLogger("demisto-sdk"), "warning")

runner = CliRunner(mix_stderr=False)
runner.invoke(main, ["validate", "-a", "--log-file-path", str(custom_logs_dir)])

custom_logs_dir_parent = custom_logs_dir.parent

assert str_in_call_args_list(
logger_warning.call_args_list,
f"Log file path '{custom_logs_dir}' is a file and not a directory. Log file will be created in "
f"parent directory '{custom_logs_dir_parent}'.",
)
assert list(custom_logs_dir_parent.glob("*")) == [
custom_logs_dir, # The random file we created
custom_logs_dir_parent / "demisto_sdk_debug.log",
]

assert (custom_logs_dir_parent / "demisto_sdk_debug.log").is_file()
assert (
len(list(default_logs_dir.glob("*"))) == 0
) # Assure default logs dir is not used
Expand Down

0 comments on commit b3a6f3e

Please sign in to comment.