Skip to content

Commit

Permalink
Test cli?
Browse files Browse the repository at this point in the history
  • Loading branch information
sz3 committed Jun 6, 2022
1 parent d646ec7 commit 2ff867d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ jobs:
working-directory: ${{runner.workspace}}/build
run: make test CTEST_OUTPUT_ON_FAILURE=TRUE

- name: Usage test
working-directory: ${{runner.workspace}}/libcimbar/test/py
run: python3 -m unittest

cppcheck:
runs-on: ubuntu-18.04

Expand Down
Empty file added test/py/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions test/py/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from os.path import join, realpath, dirname
from tempfile import TemporaryDirectory

CIMBAR_SRC = realpath(join(dirname(realpath(__file__)), '..', '..'))
BIN_DIR = join(CIMBAR_SRC, 'dist', 'bin')


class TestDirMixin():
def setUp(self):
self.working_dir = TemporaryDirectory()
super().setUp()

def tearDown(self):
super().tearDown()
with self.working_dir:
pass
88 changes: 88 additions & 0 deletions test/py/test_cimbar_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import subprocess
from os.path import join as path_join, getsize
from unittest import TestCase
from unittest.mock import patch

from helpers import TestDirMixin, CIMBAR_SRC, BIN_DIR


CIMBAR_EXE = path_join(BIN_DIR, 'cimbar')


def _get_command(*args):
cmd = [CIMBAR_EXE]
for arg in args:
if isinstance(arg, str):
cmd += arg.split(' ')
else:
cmd += arg
return cmd

# look in dist/bin/ for executables
# run them, confirm a few boring command results against samples(?)
# at the very least, confirm a roundtrip and cli options....


class CimbarCliTest(TestDirMixin, TestCase):
def test_roundtrip_oneshot(self):
# encode
infile = path_join(CIMBAR_SRC, 'LICENSE')
outprefix = path_join(self.working_dir.name, 'img')
cmd = _get_command('--encode -i', infile, '-o', outprefix)

res = subprocess.run(cmd, capture_output=True)
self.assertEqual(0, res.returncode)

encoded_img = f'{outprefix}_0.png'
self.assertTrue(getsize(encoded_img) > 30000)

# decode
cmd = _get_command('-i', encoded_img, '-o', self.working_dir.name)
res = subprocess.run(cmd, capture_output=True)
self.assertEqual(0, res.returncode)

# filename in stdout
decoded_file = res.stdout.strip().decode('utf-8')
self.assertTrue(self.working_dir.name in decoded_file)

with open(infile) as r:
expected = r.read()
with open(decoded_file) as r:
actual = r.read()

self.assertEqual(expected, actual)

def test_roundtrip_stdin(self):
# encode
infile = path_join(CIMBAR_SRC, 'LICENSE')
outprefix = path_join(self.working_dir.name, 'img')
cmd = _get_command('--encode -o', outprefix)

res = subprocess.run(
cmd, input=infile.encode('utf-8'), capture_output=True
)

self.assertEqual(0, res.returncode)

encoded_img = f'{outprefix}_0.png'
self.assertTrue(getsize(encoded_img) > 30000)

# decode defaults to cwd -- which should be self.working_dir
cmd = _get_command()
res = subprocess.run(
cmd, input=encoded_img.encode('utf-8'), capture_output=True,
cwd=self.working_dir.name,
)
self.assertEqual(0, res.returncode)

# filename in stdout
decoded_file = res.stdout.strip().decode('utf-8')
self.assertTrue(self.working_dir.name in decoded_file)

with open(infile) as r:
expected = r.read()
with open(decoded_file) as r:
actual = r.read()

self.assertEqual(expected, actual)

0 comments on commit 2ff867d

Please sign in to comment.