Skip to content

Commit

Permalink
Fixed len calculation when taring script, unicode issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicz committed Feb 15, 2018
1 parent 27d1b75 commit 68d973a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
4 changes: 3 additions & 1 deletion arca/backend/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,11 @@ def tar_script(self, name, script):
tar = tarfile.TarFile(fileobj=tarstream, mode='w')
tarinfo = tarfile.TarInfo(name=name)

script = script.encode("utf-8")

tarinfo.size = len(script)
tarinfo.mtime = time.time()
tar.addfile(tarinfo, BytesIO(script.encode("utf-8")))
tar.addfile(tarinfo, BytesIO(script))
tar.close()

return tarstream.getvalue()
Expand Down
38 changes: 20 additions & 18 deletions arca/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from pathlib import Path
from pprint import pformat
from typing import Optional, Tuple, Any, Dict, Iterable

from .exceptions import TaskMisconfigured
Expand All @@ -28,27 +29,27 @@ def __repr__(self):
return f"Task({self.function_call})"

def build_imports(self):
return "\n".join([f" import {x}" for x in self.imports])
return "\r\n".join([f" import {x}" for x in self.imports])

def build_from_imports(self):
return "\n".join([f" from {x[0]} import {x[1]}" for x in self.from_imports])
return "\r\n".join([f" from {x[0]} import {x[1]}" for x in self.from_imports])

def build_function_call(self):
if len(self.args) and len(self.kwargs):
return "{}(*{}, **{})".format(
self.function_call,
self.args,
self.kwargs
pformat(self.args),
pformat(self.kwargs)
)
elif len(self.args):
return "{}(*{})".format(
self.function_call,
self.args
pformat(self.args)
)
elif len(self.kwargs):
return "{}(**{})".format(
self.function_call,
self.kwargs
pformat(self.kwargs)
)
else:
return f"{self.function_call}()"
Expand All @@ -57,19 +58,20 @@ def build_script(self, venv_path: Path=None) -> str:
result = ""

if venv_path is not None:
result += "#!" + str(venv_path.resolve() / "bin" / "python3") + "\n\n"
result += "#!" + str(venv_path.resolve() / "bin" / "python3") + "\r\n\r\n"
else:
result += "#!python3\n\n"

result += "import json\n"
result += "import traceback\n"
result += "import sys\n"
result += "import os\n"
result += "sys.path.insert(1, os.getcwd())\n"
result += "try:\n"

result += self.build_imports() + "\n\n"
result += self.build_from_imports() + "\n\n"
result += "#!python3\r\n"

result += "# encoding=utf-8\r\n\r\n"
result += "import json\r\n"
result += "import traceback\r\n"
result += "import sys\r\n"
result += "import os\r\n"
result += "sys.path.insert(1, os.getcwd())\r\n"
result += "try:\r\n"

result += self.build_imports() + "\r\n"
result += self.build_from_imports() + "\r\n"

result += f"""
res = {self.build_function_call()}
Expand Down
14 changes: 13 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ def return_str_function():
return "Some string"
"""

TEST_UNICODE = "Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a\xa0quickstepu.→"

SECOND_RETURN_STR_FUNCTION = """
def return_str_function():
return "Some other string"
return "Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a\xa0quickstepu.→"
"""

ARG_STR_FUNCTION = """
def return_str_function(arg):
return arg[::-1]
"""

KWARG_STR_FUNCTION = """
def return_str_function(*, kwarg):
return kwarg[::-1]
"""

RETURN_DJANGO_VERSION_FUNCTION = """
Expand Down
38 changes: 30 additions & 8 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from arca import Arca, VenvBackend, DockerBackend, Task, CurrentEnvironmentBackend
from arca.exceptions import BuildError, FileOutOfRangeError

from common import BASE_DIR, RETURN_DJANGO_VERSION_FUNCTION, RETURN_STR_FUNCTION, SECOND_RETURN_STR_FUNCTION
from common import BASE_DIR, RETURN_DJANGO_VERSION_FUNCTION, RETURN_STR_FUNCTION, SECOND_RETURN_STR_FUNCTION, \
TEST_UNICODE, ARG_STR_FUNCTION, KWARG_STR_FUNCTION


@pytest.mark.parametrize(
Expand Down Expand Up @@ -53,13 +54,14 @@ def test_backends(backend, requirements_location, file_location):
filepath.write_text(RETURN_STR_FUNCTION)
repo.index.add([str(filepath)])
repo.index.commit("Initial")
repo_url = f"file://{git_dir}"

task = Task(
"return_str_function",
from_imports=[("test_file", "return_str_function")]
)

result = arca.run(f"file://{git_dir}", "master", task)
result = arca.run(repo_url, "master", task)

assert result.output == "Some string"

Expand All @@ -71,11 +73,11 @@ def test_backends(backend, requirements_location, file_location):
repo.index.add([str(filepath)])
repo.index.commit("Updated function")

result = arca.run(f"file://{git_dir}", "master", task)
assert result.output == "Some other string"
result = arca.run(repo_url, "master", task)
assert result.output == TEST_UNICODE

# in the other branch there's still the original
result = arca.run(f"file://{git_dir}", "new_branch", task)
result = arca.run(repo_url, "new_branch", task)
assert result.output == "Some string"

repo.branches.master.checkout()
Expand All @@ -94,7 +96,7 @@ def test_backends(backend, requirements_location, file_location):
repo.index.add([str(requirements_path)])
repo.index.commit("Added requirements, changed to version")

result = arca.run(f"file://{git_dir}", "master", task)
result = arca.run(repo_url, "master", task)

assert result.output == "1.11.4"

Expand All @@ -111,15 +113,15 @@ def test_backends(backend, requirements_location, file_location):
repo.index.add([str(requirements_path)])
repo.index.commit("Updated requirements")

result = arca.run(f"file://{git_dir}", "master", task)
result = arca.run(repo_url, "master", task)
assert result.output == "1.11.5"

django_task = Task(
"django.get_version",
imports=["django"]
)

result = arca.run(f"file://{git_dir}", "master", django_task)
result = arca.run(repo_url, "master", django_task)
assert result.output == "1.11.5"

django_task_error = Task(
Expand All @@ -132,6 +134,26 @@ def test_backends(backend, requirements_location, file_location):
if isinstance(backend, CurrentEnvironmentBackend):
backend._uninstall("django")

filepath.write_text(ARG_STR_FUNCTION)
repo.index.add([str(filepath)])
repo.index.commit("Argument function")

assert arca.run(repo_url, "master", Task(
"return_str_function",
from_imports=[("test_file", "return_str_function")],
args=[TEST_UNICODE]
)).output == TEST_UNICODE[::-1]

filepath.write_text(KWARG_STR_FUNCTION)
repo.index.add([str(filepath)])
repo.index.commit("Keyword argument function")

assert arca.run(repo_url, "master", Task(
"return_str_function",
from_imports=[("test_file", "return_str_function")],
kwargs={"kwarg": TEST_UNICODE}
)).output == TEST_UNICODE[::-1]


@pytest.mark.parametrize(
"backend,file_location", list(itertools.product(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_single_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from git import Repo

from arca import Arca, DockerBackend, Task, VenvBackend, CurrentEnvironmentBackend
from common import RETURN_STR_FUNCTION, SECOND_RETURN_STR_FUNCTION, BASE_DIR
from common import RETURN_STR_FUNCTION, SECOND_RETURN_STR_FUNCTION, BASE_DIR, TEST_UNICODE


@pytest.mark.parametrize(
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_single_pull(mocker, backend):
arca.pull_again(repo_url, "master")

result = arca.run(repo_url, "master", task)
assert result.output == "Some other string"
assert result.output == TEST_UNICODE
assert arca._pull.call_count == 2


Expand Down Expand Up @@ -111,9 +111,9 @@ def test_pull_efficiency(mocker, backend):
repo.index.commit("Updated function")

result = arca.run(repo_url, "master", task)
assert result.output == "Some other string"
assert result.output == TEST_UNICODE
assert arca._pull.call_count == 3

result = arca.run(repo_url, "master", task)
assert result.output == "Some other string"
assert result.output == TEST_UNICODE
assert arca._pull.call_count == 4
5 changes: 4 additions & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def _clean_lines(x: str) -> str:
return "\n".join([line for line in x.split("\n") if line.strip()])
return "\n".join([line.replace("\r", "") for line in x.split("\n") if line.replace("\r", "").strip()])


@pytest.mark.parametrize(["imports", "res"], (
Expand All @@ -23,6 +23,7 @@ def _clean_lines(x: str) -> str:
def test_imports(imports, res):
task = Task("func", imports=imports)
assert _clean_lines(task.build_script(ENV_PATH)) == _clean_lines("""#!/usr/bin/python3
# encoding=utf-8
import json
import traceback
import sys
Expand All @@ -48,6 +49,7 @@ def test_imports(imports, res):
def test_from_imports(from_imports, res):
task = Task("func", from_imports=from_imports)
assert _clean_lines(task.build_script(ENV_PATH)) == _clean_lines("""#!/usr/bin/python3
# encoding=utf-8
import json
import traceback
import sys
Expand All @@ -73,6 +75,7 @@ def test_from_imports(from_imports, res):
def test_function_call(args, kwargs, res):
task = Task("func", args=args, kwargs=kwargs)
assert _clean_lines(task.build_script(ENV_PATH)) == _clean_lines("""#!/usr/bin/python3
# encoding=utf-8
import json
import traceback
import sys
Expand Down

0 comments on commit 68d973a

Please sign in to comment.