Skip to content

Commit

Permalink
spag_cli testing expansion and corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
rrozansk committed Mar 30, 2019
1 parent 5cf9512 commit 3aa4948
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
SPaG (Scanner, Parser, and Generator)
================================================================================
[![MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/rrozansk/SPaG/blob/master/LICENSE.txt) ![COV](https://img.shields.io/badge/Coverage-99%-green.svg) ![VER](https://img.shields.io/badge/Version-1.0.0a0-yellow.svg)
[![MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/rrozansk/SPaG/blob/master/LICENSE.txt) ![COV](https://img.shields.io/badge/Coverage-97%-green.svg) ![VER](https://img.shields.io/badge/Version-1.0.0a0-yellow.svg)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
11 changes: 4 additions & 7 deletions spag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 5,7 @@
for the generator(s) of interest.
"""
from argparse import ArgumentParser, Action
from configparser import RawConfigParser
from configparser import SafeConfigParser
from enum import IntEnum, unique
from json import loads
from os.path import isfile
Expand Down Expand Up @@ -96,8 96,8 @@ def bool(string):
raise ValueError('invalid boolean input value')

def __call__(self, parser, namespace, values, option_string=None):
configuration = RawConfigParser()
configuration.read(values)
configuration = SafeConfigParser()
configuration.read_file(values)

if not configuration.has_section('SPaG'):
raise ValueError('missing runtime configuration section \'SPaG\'')
Expand Down Expand Up @@ -145,7 145,6 @@ class GenerateConfiguration(Action):
def __call__(self, parser, namespace, values, option_string=None):
with open(values, 'w') as rcfile:
rcfile.write('''[SPaG]
# Path to the runtime configuration file.
# NOTE: Ignored and only present to mirror the command line option.
configuration={0}
Expand All @@ -159,9 158,7 @@ def __call__(self, parser, namespace, values, option_string=None):
force=True
# List any language(s) targeted for generation.
generate=c,
go,
python
generate=c
# Base filename to derive the generated output filename(s).
output=out
Expand Down
140 changes: 91 additions & 49 deletions tests/test_spag_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 2,24 @@
Testing for SPaG CLI script located in spag/__main__.py
"""
import pytest
#from spag.__main__ import main
from pkg_resources import resource_string


class TestSPaGCLI:
"""
A test suite for testing the SPaG CLI script.
"""

@staticmethod
def test_liscence_distro():
"""
Ensure the LISCENCE is in the distribution.
"""
pkg_license_output = resource_string('spag', '../LICENSE.txt').decode('ascii')
with open('LICENSE.txt') as fd:
git_license_output = fd.read()
assert git_license_output == pkg_license_output

@staticmethod
def test_version(script_runner):
"""
Expand All @@ -21,74 31,106 @@ def test_version(script_runner):
assert ret.stderr == ''

@staticmethod
def test_default_help():
def test_help_non_empty(script_runner):
"""
Ensure help is the default behavior.
Ensure help message output is non-empty.
"""
return 0
ret_help = script_runner.run('spag_cli', '--help')
assert ret_help.success
assert ret_help.stderr == ''
assert ret_help.stdout != ''

@staticmethod
def test_help_non_empty():
@pytest.mark.script_launch_mode('subprocess')
def test_default_help(script_runner):
"""
Ensure help message output is non-empty.
Ensure help is the default behavior.
"""
return 0
ret_help = script_runner.run('spag_cli', '-h')
assert ret_help.success
assert ret_help.stderr == ''
help_output = ret_help.stdout

ret_default = script_runner.run('spag_cli')
assert ret_default.success
assert ret_default.stderr == ''
default_output = ret_default.stdout

assert help_output == default_output

@staticmethod
def test_rcfile_generation():
def test_rcfile_generation(script_runner):
"""
Ensure valid configuration file (INI) generation.
"""
return 0
ret_help = script_runner.run('spag_cli', '-G')
assert ret_help.success
assert ret_help.stderr == ''
assert ret_help.stdout == ''
with open('.spagrc') as fd:
configuration = fd.read()
assert configuration != ''

@staticmethod
def test_rcfile_input():
def test_rcfile_input(script_runner):
"""
Ensure the default configuration file has sane defaults.
"""
return 0
ret_help = script_runner.run('spag_cli', '-c', '.spagrc')
assert ret_help.returncode == 0
assert ret_help.stderr == ''
assert ret_help.stdout == ''

@staticmethod
def test_scanner_examples():
"""
Ensure all scanners under the examples directory are sucessfully run through SPaG.
"""
return 0

@staticmethod
def test_parser_examples():
"""
Ensure all parsers under the examples directory are sucessfully run through SPaG.
"""
return 0
@pytest.mark.parametrize("specification", [
"examples/INI/scanner.json",
"examples/JSON/scanner.json",
"examples/Lisp/scanner.json",
pytest.param("examples/Foobar/hukarz.json", marks=pytest.mark.xfail),
])
def test_scanner_examples(script_runner, specification):
"""
Ensure all scanners under the examples directory are sucessfully run
through SPaG.
"""
ret = script_runner.run('spag_cli', '-s', specification, '-v', '-t')
assert ret.returncode == 0
assert ret.stderr == ''
assert ret.stdout == ''

@staticmethod
def test_liscence_distro():
"""
Ensure the LISCENCE is in distribution.
"""
# import pkg_resources
return 0
@pytest.mark.parametrize("specification", [
"examples/INI/parser.json",
"examples/JSON/parser.json",
"examples/Lisp/parser.json",
pytest.param("examples/Foobar/hukarz.json", marks=pytest.mark.xfail),
])
def test_parser_examples(script_runner, specification):
"""
Ensure all parsers under the examples directory are sucessfully run
through SPaG.
"""
ret = script_runner.run('spag_cli', '-p', specification, '-v', '-t')
assert ret.returncode == 0
assert ret.stderr == ''
assert ret.stdout == ''

@staticmethod
@pytest.mark.xfail(
reason='...',
raises=ValueError,
)
def test_invalid_language():
"""
Ensure invalid languages produce an error.
"""
raise ValueError('...')
@pytest.mark.parametrize("language", [
"c",
"go",
"python",
pytest.param("hukarz", marks=pytest.mark.xfail),
])
def test_generator_examples(script_runner, language):
"""
Ensure all supported generators are sucessful when imported.
"""
ret = script_runner.run('spag_cli', '-g', language, '-f')
assert ret.returncode == 4
assert ret.stderr == ''
assert ret.stdout == ''

# NOTE: test other flags as well
# - 'force' successfully overwrites files
# - 'force' fails if not True or False
# - 'output' is contained in the filename
# - 'output' fails if not string
# - 'encoding'
# - 'generate'
# - 'parsers'
# - 'scanners'
# - 'time'
# - 'verbose'
# NOTE 1: expand tests to ensure other flags behave as expected.
# NOTE 2: when spag_cli is run in process i dont get any expected output!?
# NOTE 3: need to update how LICENSE is distributed.

0 comments on commit 3aa4948

Please sign in to comment.