Skip to content
Merged
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
4 changes: 2 additions & 2 deletions spectator/writer/file_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

from spectator.config import Config
from spectator.writer import Writer
Expand All @@ -20,7 +20,7 @@ def __init__(self, config: Config) -> None:
self._file = sys.stdout
self._owns_file = False
else:
self._file = open(urlparse(config.location).path, "a", encoding="utf-8")
self._file = open(unquote(urlparse(config.location).path), "a", encoding="utf-8")
self._owns_file = True

def write(self, line: str) -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/writer/test_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
from contextlib import closing, redirect_stderr, redirect_stdout
from tempfile import TemporaryDirectory
from unittest.mock import patch
from urllib.parse import quote

from spectator import Config, FileWriter


class FileWriterTest(unittest.TestCase):

def test_file_writer_escaped_path(self):
with TemporaryDirectory() as tmpdir:
tmp_file = os.path.join(tmpdir, "metrics space #%25.txt")
with closing(FileWriter(Config(f"file://{quote(tmp_file)}"))) as writer:
writer.write("foo")
with open(tmp_file, encoding="utf-8") as source:
self.assertEqual("foo\n", source.read())
self.assertEqual(["metrics space #%25.txt"], os.listdir(tmpdir))

def test_file_writer_custom(self):
with TemporaryDirectory() as tmpdir:
tmp_file = os.path.join(tmpdir, "file-writer.txt")
Expand Down
Loading