Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tools-tests/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pathlib
import sys
import unittest
import unittest.mock

import password_remover
import pytest
Expand Down Expand Up @@ -78,6 +79,30 @@ def test_add_file_task(tmp_path, tag_userdata):
assert expected in fh.read()


def test_add_file_task_zero_size(tmp_path):
filename = "empty.json"
empty_file = tmp_path / filename
empty_file.write_text("")
task = tasks.add_file_task(
sourcefile_path=empty_file,
output_path=empty_file.name,
)
task.no_header = True
output_dir = tmp_path / "output"
output_dir.mkdir()
runner = tasks.TaskRunner(
verbosity=VERBOSE,
default_name="sg.log",
tmp_dir=output_dir,
)
runner.run(task)
runner.close_all_files()

output_path = pathlib.Path(runner.tmpdir) / filename
assert output_path.exists()
assert os.path.getsize(output_path) == 0


def test_make_curl_task(tmpdir, httpserver):
output = "curltask"
httpserver.expect_request("/").respond_with_json(json.loads(INPUT_CONFIG))
Expand Down Expand Up @@ -107,6 +132,33 @@ def test_make_curl_task(tmpdir, httpserver):
httpserver.check()


def test_make_curl_task_zero_size(tmpdir, httpserver):
output = "curltask_empty"
httpserver.expect_request("/").respond_with_data("")
task = tasks.make_curl_task(
"curltask_empty",
httpserver.url_for("/"),
auth_headers={},
log_file=output,
)
task.no_header = True

output_dir = tmpdir.mkdir("output")
runner = tasks.TaskRunner(
verbosity=VERBOSE,
default_name="sg.log",
tmp_dir=output_dir,
)
runner.run(task)
runner.close_all_files()

output_path = pathlib.Path(runner.tmpdir) / output
assert output_path.exists()
assert os.path.getsize(output_path) == 0

httpserver.check()


def test_task_print_literal(tmp_path):
task = tasks.AllOsTask("test_task", ["notacommand"], literal="literal")
runner = tasks.TaskRunner(tmp_dir=tmp_path)
Expand Down
3 changes: 3 additions & 0 deletions tools/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ def execute(self, fp):
print("log_file: {0}. ".format(self.log_file))
try:
result = self.callable()
if not result:
return 0
Comment on lines +232 to +233

try:
fp.write(result.encode())
except (UnicodeEncodeError, AttributeError):
Expand Down
Loading