Skip to content

Commit

Permalink
Fix ci?
Browse files Browse the repository at this point in the history
  • Loading branch information
sz3 committed Feb 17, 2023
1 parent ddc5dd5 commit c7d6e2f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: [3.6, 3.7, 3.8, pypy3]
python-version: ["3.7", "3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand All @@ -23,4 +23,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
coveralls
coveralls --service=github
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -10,7 +10,7 @@
name='steganosort',
license='GPLv3',
url='https://github.com/sz3/steganosort',
version='0.1',
version='0.1.0',

entry_points={
'console_scripts': [
Expand Down
23 changes: 22 additions & 1 deletion steganosort/__main__.py
@@ -1,6 +1,27 @@
import argparse
import sys
from .cli import main

from argparse import RawTextHelpFormatter


USAGE='''
Examples:
cat lines.txt | steganosort "message" > encoded.txt
cat lines.txt | steganosort @msg.txt > encoded.txt
cat json.txt | steganosort "message" --json > encodedjson.txt
cat json.txt | steganosort @msg.txt --json > encodedjson.txt
cat encoded.txt | steganosort > decodedmsg.txt
cat encodedjson.txt | steganosort > decodedmsg.txt
'''


if __name__ == '__main__':
main(sys.argv)
parser = argparse.ArgumentParser('steganosort',
description='Embed messages in the sort order.',
epilog=USAGE,
formatter_class=RawTextHelpFormatter)
parser.add_argument('message', nargs='?', default=None)
parser.add_argument('--json', action='store_true')
args = parser.parse_args()
main(message=args.message, json=args.json)
27 changes: 3 additions & 24 deletions steganosort/cli.py
@@ -1,22 +1,9 @@
import argparse
import json
import sys
from argparse import RawTextHelpFormatter

from .util import encode, decode


USAGE='''
Examples:
cat lines.txt | steganosort "message" > encoded.txt
cat lines.txt | steganosort @msg.txt > encoded.txt
cat json.txt | steganosort "message" --json > encodedjson.txt
cat json.txt | steganosort @msg.txt --json > encodedjson.txt
cat encoded.txt | steganosort > decodedmsg.txt
cat encodedjson.txt | steganosort > decodedmsg.txt
'''


def enc_json(src, dst, msg):
data = json.load(src)
res = encode(data, msg)
Expand All @@ -41,16 +28,8 @@ def dec_lines(src, dst):
dst.write(res.decode('utf-8'))


def main(args):
parser = argparse.ArgumentParser('steganosort',
description='Embed messages in the sort order.',
epilog=USAGE,
formatter_class=RawTextHelpFormatter)
parser.add_argument('message', nargs='?', default=None)
parser.add_argument('--json', action='store_true')
args = parser.parse_args()

msg = args.message
def main(**kwargs):
msg = kwargs.get('message')
if not msg:
pass
elif msg.startswith('@'):
Expand All @@ -60,7 +39,7 @@ def main(args):
msg = bytes(msg, 'utf-8')

mode = 'newlines'
if args.json:
if kwargs.get('json'):
mode = 'json'

enc = {
Expand Down
41 changes: 33 additions & 8 deletions tests/test_cli.py
Expand Up @@ -5,7 +5,7 @@
from unittest import TestCase
from unittest.mock import patch

from steganosort.cli import enc, dec, main
from steganosort.cli import enc_json, dec_json, main


SAMPLE_JSON = {
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_encoding(self):

dst_path = path.join(self.tempdir.name, 'outfile')
with open(src_path, 'rb') as fi, open(dst_path, 'wt') as fo:
enc(fi, fo, b'hello!')
enc_json(fi, fo, b'hello!')

# validate json is the "same"
with open(dst_path, 'rb') as fo:
Expand All @@ -70,17 +70,42 @@ def test_encoding(self):
# validate decode
decode_path = path.join(self.tempdir.name, 'decoded')
with open(dst_path, 'rb') as fo, open(decode_path, 'wt') as fdec:
dec(fo, fdec)
dec_json(fo, fdec)

with open(decode_path, 'rb') as fdec:
self.assertEqual(b'hello!\x00\x00\x00\x00', fdec.read())

def test_main(self):
sample = list(range(0,32))

# encode
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO('\n'.join([f'{i}' for i in sample]) + '\n')
mock_sys.stdout = StringIO()
main(message='greetings!')

mock_sys.stdout.seek(0)
encoded = mock_sys.stdout.read()
asints = [int(i) for i in encoded.strip().split('\n')]
sorted_out = sorted(asints)
self.assertEqual(sample, sorted_out)

# decode
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO(encoded)
mock_sys.stdout = StringIO()
main()

mock_sys.stdout.seek(0)
msg = mock_sys.stdout.read()
self.assertEqual('greetings!', msg)

def test_main_json(self):
# encode
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO(json.dumps(SAMPLE_JSON))
mock_sys.stdout = StringIO()
main(['steg', 'greetings!'])
main(message='greetings!', json=True)

mock_sys.stdout.seek(0)
encoded = mock_sys.stdout.read()
Expand All @@ -90,7 +115,7 @@ def test_main(self):
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO(encoded)
mock_sys.stdout = StringIO()
main(['steg'])
main(json=True)

mock_sys.stdout.seek(0)
msg = mock_sys.stdout.read()
Expand All @@ -106,7 +131,7 @@ def test_main_msg_from_file(self):
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO(json.dumps(SAMPLE_JSON))
mock_sys.stdout = StringIO()
main(['steg', f'@{src_path}'])
main(message=f'@{src_path}', json=True)

mock_sys.stdout.seek(0)
encoded = mock_sys.stdout.read()
Expand All @@ -116,8 +141,8 @@ def test_main_msg_from_file(self):
with patch("steganosort.cli.sys") as mock_sys:
mock_sys.stdin = StringIO(encoded)
mock_sys.stdout = StringIO()
main(['steg'])
main(json=True)

mock_sys.stdout.seek(0)
msg = mock_sys.stdout.read()
self.assertEqual('0123456789', msg)
self.assertEqual('0123456789', msg)

0 comments on commit c7d6e2f

Please sign in to comment.