Skip to content

Commit

Permalink
Fix for #453 - error writing diff output to file. Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
olsen232 committed Jul 7, 2021
1 parent 887d604 commit 6bdc2b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 14 additions & 1 deletion kart/base_diff_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import logging
from pathlib import Path
import re
import sys

Expand Down Expand Up @@ -83,7 +84,9 @@ def __init__(

self.all_ds_paths = sorted(list(all_ds_paths))

self.output_path = self._check_output_path(repo, output_path)
self.output_path = self._check_output_path(
repo, self._normalize_output_path(output_path)
)

self.json_style = json_style
self.target_crs = target_crs
Expand All @@ -97,6 +100,16 @@ def include_target_commit_as_header(self):
"""
self.commit = self.target_rs.commit

@classmethod
def _normalize_output_path(cls, output_path):
if not output_path or output_path == "-":
return output_path
if isinstance(output_path, str):
output_path = Path(output_path)
if isinstance(output_path, Path):
output_path = output_path.expanduser()
return output_path

@classmethod
def _check_output_path(cls, repo, output_path):
"""Make sure the given output_path is valid for this implementation (ie, are directories supported)."""
Expand Down
8 changes: 6 additions & 2 deletions kart/output_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ def resolve_output_path(output_path):
* a file-like object
* the string '-' or None (both will return sys.stdout)
"""
if (not output_path) or output_path == "-":
return sys.stdout

if isinstance(output_path, str):
output_path = Path(output_path)
if isinstance(output_path, Path):
output_path = output_path.expanduser()

Expand All @@ -241,8 +246,7 @@ def resolve_output_path(output_path):
# but in some circumstances it can be something else.
# e.g. click on windows may wrap it with a colorama.ansitowin32.StreamWrapper.
return output_path
elif (not output_path) or output_path == "-":
return sys.stdout

else:
return output_path.open("w")

Expand Down
12 changes: 12 additions & 0 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,3 +1638,15 @@ def override_get_feature(self, *args, **kwargs):
expected_calls += 1
assert old.get_feature_calls == expected_calls
assert new.get_feature_calls == expected_calls


@pytest.mark.parametrize(
"output_format", [o for o in DIFF_OUTPUT_FORMATS if o not in {"html", "quiet"}]
)
def test_diff_output_to_file(output_format, data_archive, cli_runner):
with data_archive("points") as repo_path:
r = cli_runner.invoke(
["show", f"--output-format={output_format}", "--output=out"]
)
assert r.exit_code == 0, r
assert (repo_path / "out").exists()

0 comments on commit 6bdc2b2

Please sign in to comment.