Skip to content

Commit

Permalink
aheui.aheuis 생기지 않도록
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Apr 7, 2024
1 parent 6510f07 commit 86085fe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
17 changes: 10 additions & 7 deletions aheui/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ def kwarg_or_environ_int(kwargs, environ, arg_key, env_key, default):
return value


def process_options(argv, environ):
def open_r(filename):
return os.open(filename, os.O_RDONLY, 0o777)
def open_input(filename):
return os.open(filename, os.O_RDONLY, 0o777)


def process_options(argv, environ):
kwargs, args = parser.parse_args(argv)
if not args:
raise SystemExit()
Expand All @@ -82,7 +83,7 @@ def open_r(filename):
fp = 0
contents = compile.read(fp)
else:
fp = open_r(filename)
fp = open_input(filename)
contents = compile.read(fp)
os.close(fp)
else:
Expand Down Expand Up @@ -115,10 +116,12 @@ def open_r(filename):

if need_aheuic:
aheuic_output = filename
if aheuic_output.endswith('.aheui'):
aheuic_output += 'c'
if filename.endswith('.aheui'):
aheuic_output = filename + 'c'
elif filename.endswith('.aheuis'):
aheuic_output = filename[:-1] + 'c'
else:
aheuic_output += '.aheuic'
aheuic_output = filename + '.aheuic'
else:
aheuic_output = None

Expand Down
35 changes: 35 additions & 0 deletions tests/test_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from aheui.option import process_options


def test_option_filename(mocker):
mocker.patch('os.open', return_value=0)
mocker.patch('os.close', return_value=None)
mocker.patch('aheui.compile.read', return_value=b'')

assert ('', 'text', b'', '1', 'run', None, False, '-', 3, -1) == process_options(['aheui-c', '-'], {})
assert ('', 'text', b'', '1', 'run', 'x.aheuic', False, '-', 3, -1) == process_options(['aheui-c', 'x'], {})
assert ('', 'text', b'', '1', 'run', 'x.aheuic', False, '-', 3, -1) == process_options(['aheui-c', 'x.aheui'], {})
assert ('', 'asm', b'', '1', 'run', 'x.aheuic', False, '-', 3, -1) == process_options(['aheui-c', 'x.aheuis'], {})
assert ('', 'bytecode', b'', '1', 'run', None, False, '-', 3, -1) == process_options(['aheui-c', 'x.aheuic'], {})

assert ('', 'text', b'', '1', 'run', None, False, '-', 3, -1) == process_options(['aheui-c', '-', '--output=-'], {})
assert ('', 'text', b'', '1', 'run', None, False, 'out', 3, -1) == process_options(['aheui-c', '-', '--output=out'], {})


def test_option_cmd(mocker):
mocker.patch('os.open', return_value=0)
mocker.patch('os.close', return_value=None)
mocker.patch('aheui.compile.read', return_value=b'')

heui = '희'.encode('utf-8')
assert (heui, 'text', heui, '1', 'run', None, False, '-', 3, -1) == process_options(['aheui-c', '-c', '희'], {})
assert (heui, 'text', heui, '1', 'run', None, False, '-', 3, -1) == process_options(['aheui-c', '-c', '희', '--output=-'], {})
assert (heui, 'text', heui, '1', 'run', None, False, 'out', 3, -1) == process_options(['aheui-c', '-c', '희', '--output=out'], {})
# with pytest.raises(SystemExit):
# process_options(['aheui-c', '-c'], {})
with pytest.raises(SystemExit):
process_options(['aheui-c', '-c', '희', 'x'], {})
assert (heui, 'text', heui, '1', 'asm', None, False, '-', 3, -1) == process_options(['aheui-c', '-c', '희', '--target=asm'], {})
assert (heui, 'text', heui, '1', 'asm', None, False, '-', 3, -1) == process_options(['aheui-c', '-c', '희', '--target=asm', '--output=-'], {})
assert (heui, 'text', heui, '1', 'asm', None, False, 'out', 3, -1) == process_options(['aheui-c', '-c', '희', '--target=asm', '--output=out'], {})

0 comments on commit 86085fe

Please sign in to comment.