Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Read from STDIN #56

Merged
merged 13 commits into from
Oct 14, 2024
16 changes: 15 additions & 1 deletion svgcheck/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import optparse
import os
import shutil
import tempfile
import lxml.etree
from svgcheck.checksvg import checkTree
from svgcheck.__init__ import __version__
Expand Down Expand Up @@ -107,12 +108,25 @@ def main():
wp.color_threshold = options.grey_level

if len(args) < 1:
source = os.getcwd() + "/stdin"
source = None
try:
with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as tmp_file:
data = sys.stdin.buffer.read()
tmp_file.write(data)
tmp_file.close()
source = tmp_file.name
process_svg(options, source)
finally:
if os.path.exists(source):
kesara marked this conversation as resolved.
Show resolved Hide resolved
os.remove(source)
else:
source = args[0]
if not os.path.exists(source):
sys.exit('No such file: ' + source)
process_svg(options, source)


def process_svg(options, source):
# Setup warnings module
# rfclint.log.warn_error = options.warn_error and True or False
log.quiet = options.quiet and True or False
Expand Down
16 changes: 16 additions & 0 deletions svgcheck/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ def test_clear_cache(self):
None, None)
self.assertFalse(os.path.exists('Temp/cache/reference.RFC.1847.xml'))

def test_stdin(self):
process = subprocess.Popen([sys.executable, test_program],
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
_, stderr_data = process.communicate(input=b'<svg viewBox="0 0 1 1"/>')
self.assertEqual(stderr_data.decode("utf-8").strip(),
"INFO: File conforms to SVG requirements.")

def test_no_such_file(self):
file = "this_file_does_not_exist.svg"
process = subprocess.Popen([sys.executable, test_program, file],
stderr=subprocess.PIPE)
_, stderr_data = process.communicate()
self.assertEqual(stderr_data.decode("utf-8").strip(),
f"No such file: {file}")


class TestParserMethods(unittest.TestCase):

Expand Down
Loading