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
20 changes: 11 additions & 9 deletions src/taskgraph/decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ def set_try_config(parameters, task_config_file):
)


def write_artifact(filename, data):
def write_artifact(filename, data, *, artifact_dir=None):
logger.info(f"writing artifact file `{filename}`")
if not os.path.isdir(ARTIFACTS_DIR):
os.mkdir(ARTIFACTS_DIR)
path = ARTIFACTS_DIR / filename
directory = ARTIFACTS_DIR if artifact_dir is None else Path(artifact_dir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have artifact_dir passed in as a Path rather than converted here; that way both of the possible sources of it are consistent.

(And please do the same for the other functions.)

directory.mkdir(parents=True, exist_ok=True)
path = directory / filename
if filename.endswith(".yml"):
with open(path, "w") as f:
yaml.safe_dump(data, f, allow_unicode=True, default_flow_style=False)
Expand All @@ -313,10 +313,11 @@ def write_artifact(filename, data):
raise TypeError(f"Don't know how to write to {filename}")


def read_artifact(filename):
path = ARTIFACTS_DIR / filename
def read_artifact(filename, *, artifact_dir=None):
directory = ARTIFACTS_DIR if artifact_dir is None else Path(artifact_dir)
path = directory / filename
if filename.endswith(".yml"):
return load_yaml(path, filename)
return load_yaml(path)
elif filename.endswith(".json"):
with open(path) as f:
return json.load(f)
Expand All @@ -329,5 +330,6 @@ def read_artifact(filename):
raise TypeError(f"Don't know how to read {filename}")


def rename_artifact(src, dest):
os.rename(ARTIFACTS_DIR / src, ARTIFACTS_DIR / dest)
def rename_artifact(src, dest, *, artifact_dir=None):
directory = ARTIFACTS_DIR if artifact_dir is None else Path(artifact_dir)
os.rename(directory / src, directory / dest)
20 changes: 20 additions & 0 deletions test/test_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ def test_write_artifact_yml(self):
decision.ARTIFACTS_DIR = Path("artifacts")


@pytest.mark.parametrize("extension", (".json", ".yml"))
@pytest.mark.parametrize(
"artifact_dir", (None, "custom/uploads", Path("custom/uploads"))
)
def test_artifact_round_trip(tmp_path, monkeypatch, extension, artifact_dir):
monkeypatch.chdir(tmp_path)
directory = Path("artifacts") if artifact_dir is None else Path(artifact_dir)
kwargs = {} if artifact_dir is None else {"artifact_dir": artifact_dir}
filename = f"artifact{extension}"
renamed = f"renamed{extension}"
data = {"test": [1, 2]}

decision.write_artifact(filename, data, **kwargs)
assert (directory / filename).is_file()

decision.rename_artifact(filename, renamed, **kwargs)
assert not (directory / filename).exists()
assert decision.read_artifact(renamed, **kwargs) == data


@unittest.mock.patch.object(
GitRepository,
"get_changed_files",
Expand Down