Skip to content

Commit

Permalink
feat: Back-sync seems to be fulling working e2e with test
Browse files Browse the repository at this point in the history
  • Loading branch information
nickderobertis committed Jul 17, 2022
1 parent f2fccca commit 69125a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion flexlate_dev/server/back_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def _get_content_from_file_added_diff(diff: PatchedFile) -> str:


def _apply_patch(diff: Union[PatchedFile, PatchSet, str], project_path: Path) -> None:
# TODO: for some reason the patch library will add a new line to the end of the file
# even when it is not supposed to have one
patch_set = patch.fromstring(str(diff).encode("utf-8"))
with change_directory_to(project_path):
patch_set.apply()
Expand Down Expand Up @@ -176,7 +178,7 @@ def sync(self, new_commit: Optional[str] = None):
print_styled(f"Back-syncing to commit {new_commit}", INFO_STYLE)
with pause_sync(self.sync_manager):
apply_diff_between_commits_to_separate_project(
self.project_repo, new_commit, self.last_commit, self.template_path
self.project_repo, self.last_commit, new_commit, self.template_path
)
self.last_commit = new_commit
if self.auto_commit:
Expand Down
7 changes: 5 additions & 2 deletions flexlate_dev/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ def __init__(self, handler: ServerEventHandler):
self.observer = Observer()
self.handler = handler

def start(self):
def initial_start(self):
self.handler.sync_output() # do a sync before starting watcher
self.start()

def start(self):
# setting up inotify and specifying path to watch
self.observer.schedule(
self.handler, str(self.handler.template_path), recursive=True
Expand All @@ -138,7 +141,7 @@ def stop(self):
self.observer = Observer()

def __enter__(self) -> "SyncServerManager":
self.start()
self.initial_start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/template_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import shutil
from pathlib import Path

import pytest
from git import Repo

from flexlate_dev.gitutils import stage_and_commit_all
from tests.config import COPIER_ONE_DIR, GENERATED_FILES_DIR


@pytest.fixture
def copier_one_template_repo(copier_one_template_path: Path) -> Repo:
path = copier_one_template_path
repo = Repo.init(path)
stage_and_commit_all(repo, "Initial commit")
yield repo
12 changes: 9 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SEPARATE_PUBLISH_SERVE_CONFIG_PATH,
)
from tests.fixtures.template_path import *
from tests.fixtures.template_repo import copier_one_template_repo
from tests.pathutils import change_directory_to
from tests.waitutils import (
wait_until_file_has_content,
Expand Down Expand Up @@ -293,9 +294,9 @@ def test_server_ignores_changes_to_ignored_files(


def test_server_back_syncs_changes_from_project_to_template_copier(
copier_one_template_path: Path,
copier_one_template_repo: Repo,
):
template_path = copier_one_template_path
template_path = Path(copier_one_template_repo.working_dir)
folder_name = "project"
project_path = GENERATED_FILES_DIR / folder_name
expect_file = project_path / "a1.txt"
Expand All @@ -306,6 +307,8 @@ def test_server_back_syncs_changes_from_project_to_template_copier(
with run_server(
config, None, template_path, GENERATED_FILES_DIR, no_input=True, back_sync=True
):
project_repo = Repo(project_path)

wait_until_path_exists(expect_file)
# Check initial load
assert expect_file.read_text() == "1"
Expand All @@ -320,7 +323,10 @@ def test_server_back_syncs_changes_from_project_to_template_copier(

# Check back sync
wait_until_file_has_content(
non_templated_template_file, non_templated_modified_time, "new content"
# After fixing the TODO about adding new lines, set this back to "new content"
non_templated_template_file,
non_templated_modified_time,
"new content\n",
)

# Cause a reload
Expand Down

0 comments on commit 69125a8

Please sign in to comment.