Skip to content

Commit

Permalink
should work on ci now
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemumma committed Jun 18, 2024
1 parent e7a1150 commit 3651fae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
31 changes: 10 additions & 21 deletions snuba/migrations/autogeneration/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os
import subprocess

import yaml


def generate(storage_path: str) -> tuple[str, str]:
storage_path = os.path.abspath(os.path.expanduser(storage_path))
storage_path = os.path.realpath(os.path.abspath(os.path.expanduser(storage_path)))

# get the version of the storage at HEAD
# get the version of the file at HEAD
try:
repo_path = (
subprocess.run(
Expand All @@ -23,27 +21,18 @@ def generate(storage_path: str) -> tuple[str, str]:
.stdout.decode("utf-8")
.strip()
)
if not repo_path.endswith("snuba"):
raise ValueError(
"expected git repo to end with 'snuba' but got: " + repo_path
)
assert storage_path.startswith(repo_path) # should always hold
rel_storage_path = storage_path[len(repo_path) + 1 :]
old_storage = (
subprocess.run(
["git", "show", f"HEAD:{rel_storage_path}"],
capture_output=True,
check=True,
)
.stdout.decode("utf-8")
.strip()
)
repo_rel_path = os.path.relpath(storage_path, repo_path)
old_storage = subprocess.run(
["git", "show", f"HEAD:{repo_rel_path}"],
cwd=repo_path,
capture_output=True,
check=True,
).stdout.decode("utf-8")
except subprocess.CalledProcessError as e:
raise ValueError(e.stderr.decode("utf-8")) from e
old_storage = yaml.safe_load(old_storage)

# get the user-provided (modified) storage
with open(storage_path, "r") as f:
new_storage = yaml.safe_load(f)
new_storage = f.read()

return old_storage, new_storage
43 changes: 35 additions & 8 deletions tests/migrations/test_autogeneration.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import pytest
import os
import subprocess

from snuba.migrations.autogeneration import generate


def test_basic() -> None:
old_storage, new_storage = generate(
"snuba/datasets/configuration/events/storages/errors.yaml"
)
assert old_storage, new_storage
dir = "/tmp/kylesfakerepo987636"
fname = "fakestorage.yaml"

# make a tmp dir with a git repo
if os.path.exists(dir):
subprocess.run(["rm", "-rf", dir], check=True)
os.makedirs(dir)
try:
subprocess.run(["git", "init"], cwd=dir, check=True)
# make a fake storage
with open(os.path.join(dir, fname), "w") as f:
f.write("hello world\n")
subprocess.run(
["git", "add", "."],
cwd=dir,
check=True,
)
subprocess.run(
["git", "commit", "-m", '"blop"'],
cwd=dir,
check=True,
)
except subprocess.CalledProcessError as e:
if not e.stderr:
raise
raise ValueError(e.stderr.decode("utf-8")) from e

def test_error() -> None:
with pytest.raises(ValueError):
generate("~/hello.txt")
# update the fake storage
with open(os.path.join(dir, fname), "a") as f:
f.write("goodbye world")

# make sure HEAD and curr version looks right
old_storage, new_storage = generate(os.path.join(dir, fname))
assert old_storage == "hello world\n"
assert new_storage == "hello world\ngoodbye world"

0 comments on commit 3651fae

Please sign in to comment.