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

New-py branch #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test install

test:
python3 -m pytest -xv --flake8 --pylint
python3 -m pytest -xv --pylint --mypy -s

install:
python3 -m pip install -r requirements.txt
20 changes: 16 additions & 4 deletions new.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def get_args() -> Args:
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

rc_file = os.path.join(str(Path.home()), '.new.py')
defaults = get_defaults(open(rc_file) if os.path.isfile(rc_file) else None)

defaults = None

if os.path.isfile(rc_file):
with open(rc_file, encoding='utf-8') as f:
defaults = get_defaults(f)
else:
defaults = get_defaults(None)

username = os.getenv('USER') or 'Anonymous'
hostname = os.getenv('HOSTNAME') or 'localhost'

Expand Down Expand Up @@ -98,7 +106,8 @@ def main() -> None:
if not answer.lower().startswith('y'):
sys.exit('Will not overwrite. Bye!')

print(body(args), file=open(program, 'wt'), end='')
with open(program, 'wt', encoding='utf-8') as f:
print(body(args), file=f, end='')

if platform.system() != 'Windows':
subprocess.run(['chmod', '+x', program], check=True)
Expand All @@ -113,7 +122,9 @@ def main() -> None:
if os.path.isfile(test_file):
print(f'Will not overwrite "{test_file}"!')
else:
print(text_test(args.program), file=open(test_file, 'wt'))
with open(test_file, 'wt', encoding='utf-8') as f:
print(text_test(args.program), file=f)


makefile_text = [
'.PHONY: test', '', 'test:',
Expand All @@ -123,7 +134,8 @@ def main() -> None:
if os.path.isfile(makefile):
print(f'Will not overwrite "{makefile}"!')
else:
print('\n'.join(makefile_text), file=open('Makefile', 'wt'))
with open('Makefile', 'wt', encoding='utf-8') as f:
print('\n'.join(makefile_text), file=f)

print(f'Done, see new script "{program}".')

Expand Down
16 changes: 10 additions & 6 deletions new_test.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from new import get_defaults

PRG = './new.py'
PRGR = 'python3 new.py'
PRGRU = 'python3 ../new.py'


# --------------------------------------------------
Expand All @@ -29,15 +31,15 @@ def test_get_defaults():
}
text = io.StringIO('\n'.join(f'{k}={v}' for k, v in expected.items()))
assert get_defaults(text) == expected
assert get_defaults(None) == {}
assert not get_defaults(None)


# --------------------------------------------------
def test_usage():
""" Prints usage """

for flag in ['-h', '--help']:
retval, out = getstatusoutput(f'{PRG} {flag}')
retval, out = getstatusoutput(f'{PRGR} {flag}')
assert retval == 0
assert out.lower().startswith('usage')

Expand All @@ -51,20 +53,22 @@ def test_ok():
abs_new = os.path.abspath(PRG)

if os.path.isdir(dirname):
rmtree(dirname)
rmtree(dirname, ignore_errors=True)

os.makedirs(dirname)
os.chdir(dirname)

try:
basename = random_string()
name = basename + '.py'
retval, out = getstatusoutput(f'{abs_new} -t {name}')
retval, out = getstatusoutput(f'{PRGRU} -t {name}')
print('retval:', retval, 'out:', out)
print('abs_new:', abs_new, 'name: ', name)
assert retval == 0
assert os.path.isfile(name)
assert out == f'Done, see new script "{name}".'

retval2, usage = getstatusoutput(f'./{name} --help')
retval2, usage = getstatusoutput(f'{PRGRU} --help')
assert retval2 == 0
assert usage.lower().startswith('usage:')

Expand All @@ -75,7 +79,7 @@ def test_ok():

finally:
if os.path.isdir(dirname):
rmtree(dirname)
rmtree(dirname, ignore_errors=True)
os.chdir(cwd)


Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ flake8-builtins
flake8-pytest
pylint
pytest-pylint
mypy
pytest-mypy