Skip to content

Commit

Permalink
Tests for choices, prefixes, parents, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Khodak committed Jun 19, 2016
1 parent 0d06041 commit 6f85899
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 42 deletions.
76 changes: 43 additions & 33 deletions test/test-data/search.cwl
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
- id: search
class: CommandLineTool
baseCommand: python
requirements:
- class: InlineJavascriptRequirement
inputs:
- id: file
type: File
inputBinding:
position: 1
secondaryFiles:
- ".idx1"
- "^.idx2"
- '$(self.path+".idx3")'
- '$({"path": self.path+".idx4", "class": "File"})'
- '${ return self.path+".idx5"; }'
- id: search.py
type: File
default:
class: File
path: search.py
inputBinding:
position: 0
- id: term
type: string
inputBinding:
position: 2
outputs:
- id: result
type: File
outputBinding:
glob: result.txt
stdout: result.txt
#!/usr/bin/env cwl-runner
# This tool description was formed automatically by argparse2cwl ver. 0.2.5
# To form again: $ python --generate_cwl_tool
# Help: $ python --help_arg2cwl

cwlVersion: "cwl:draft-3"

class: CommandLineTool
baseCommand: ['python']

requirements:
- class: InlineJavascriptRequirement

description: |
Toy program to search inverted index and print out each line the term appears

inputs:

- id: search.py
type: File
default:
class: File
path: search.py
inputBinding:
position: 0


- id: mainfile
type: File

description: Text file to be indexed
inputBinding:
position: 1

- id: term
type: string

description: Term for search
inputBinding:
position: 2

outputs:
[]
76 changes: 67 additions & 9 deletions test/test_arg2cwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import shutil
import unittest
from itertools import chain
from unittest import mock
from io import StringIO

Expand All @@ -17,37 +18,39 @@ class GeneralTestCase(unittest.TestCase):
maxDiff = None

@staticmethod
def prepare_argument_parser(name=None):
parser = argparse.ArgumentParser(prog=name, description='test program')
def prepare_argument_parser(name=None, add_help=True):
parser = argparse.ArgumentParser(prog=name, description='test program', add_help=add_help)
parser.add_argument('keyword', metavar='Q', type=str, nargs=1,
help='action keyword')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for \n'
'the accumulator')
parser.add_argument('choices', type=int, choices=range(5, 10))
parser.add_argument('foo', type=str,
help='positional argument with the same dest as optional argument')
parser.add_argument('positional_nargs_asteriks', nargs='*',
help='positional argument with nargs = *')
parser.add_argument('positional_nargs_question_mark', nargs='?',
help='positional argument with nargs = ?')
parser.add_argument('positional_with_choices', choices=['rock', 'paper', 'scissors'])
parser.add_argument('--sum', '-s', dest='accumulate', action='store_const',
const=sum, default=max, help='sum the integers (default: find the max)')
parser.add_argument('--req', required=True, help='required optional')
parser.add_argument('--foo', nargs='?', help='foo help')
# parser.add_argument('--open', type=open, help='argument with type `open`')
parser.add_argument('--open', type=open, help='argument with type `open`')
parser.add_argument('--file', nargs='?', help='foo help', type=argparse.FileType('w'))
parser.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!')
parser.add_argument('--true_p', action='store_true', help='Store a true')
parser.add_argument('--false_p', action='store_false', help='Store a false')
parser.add_argument('--append', action='append', help='Append a value')
parser.add_argument('--str', dest='types', action='append_const', const=str, help='Append a value')
parser.add_argument('--int', dest='types', action='append_const', const=int, help='Append a value')
parser.add_argument('--output-file', help='Output file')

parser.add_argument('--nargs2', nargs=2, help='nargs2')

parser.add_argument('--mode', choices=['rock', 'paper', 'scissors'], default='scissors')

parser.add_argument('--version', action='version', version='2.0')
parser.set_defaults(foo='Lorem Ipsum')
return parser

def test_general(self):
Expand Down Expand Up @@ -109,8 +112,8 @@ def setUp(self):
def tearDown(self):
shutil.rmtree(self.test_dir)

def get_simple_tool(self, parser_name, testargs=None):
parser = GeneralTestCase.prepare_argument_parser(name=parser_name)
def get_simple_tool(self, parser_name, testargs=None, add_help=True):
parser = GeneralTestCase.prepare_argument_parser(parser_name, add_help)
if not testargs:
testargs = [parser_name, "--generate_cwl_tool", "-d", self.test_dir]
with mock.patch.object(sys, 'argv', testargs):
Expand Down Expand Up @@ -171,17 +174,22 @@ def test_type(self):
arg_type = arg_type[1] # the second is the actual type
# choices
if action.choices:
self.assertEqual(action.choices, arg_type['symbols'])
if type(action.choices) is range:
self.assertEqual(list(action.choices), arg_type['symbols'])
else:
self.assertEqual(action.choices, arg_type['symbols'])
self.assertEqual('enum', arg_type['type'])
if action.type:
if isinstance(action.type, argparse.FileType):
action_type = 'File'
else:
action_type = ac.get_cwl_type(action.type)
if (action.nargs and action.nargs != '?') \
or type(action).__name__ == "_AppendAction":
or type(action).__name__.startswith("_Append"):
self.assertEqual('array', arg_type['type'])
self.assertEqual(action.items_type or 'string', arg_type['items'])
elif action.choices:
self.assertTrue(all(isinstance(x, action.type) for x in arg_type['symbols']))
else:
self.assertEqual(action_type, arg_type)

Expand Down Expand Up @@ -218,6 +226,56 @@ def test_outputs(self):
k = tool.outputs[action.dest+'_out'].id
self.assertTrue(tool.outputs[action.dest+'_out'].id)

def test_parents(self):
mum, mum_tool = self.get_simple_tool('mum.py', add_help=False)
dad = argparse.ArgumentParser(prog='daddy.py', description='test program', add_help=False)
dad.add_argument('pos_arg', type=int)
dad.add_argument('--opt_arg', action='store_true')
kid_name = 'kid.py'
kid = argparse.ArgumentParser(prog=kid_name, parents=[mum, dad])
kid.add_argument('--kids_argument', nargs='*')
testargs = [kid_name, "--generate_cwl_tool", "-d", self.test_dir]
with mock.patch.object(sys, 'argv', testargs):
with self.assertRaises(SystemExit) as result:
kid.parse_args()
self.assertEqual(result.exception.code, 0)
tool = Tool(self.test_dir + kid_name.replace('.py', '.cwl').strip('./'))
actions = list(chain(self._strip_help_version_actions(mum._actions),
self._strip_help_version_actions(dad._actions)))
arguments = [arg.dest for arg in actions]
arguments.append('kids_argument')
for arg in arguments:
self.assertIn(arg, tool.inputs.keys())



def test_prefixes(self):
parser_name = 'test-prefix-chars.py'
parser = argparse.ArgumentParser(prog=parser_name,
prefix_chars='-+',
description='test prefix chars progrma')
parser.add_argument('keyword', type=str, nargs=1,
help='action keyword')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for \n'
'the accumulator')
parser.add_argument('choices', type=int, choices=range(5, 10))
parser.add_argument('foo', type=str,
help='positional argument with the same dest as optional argument')
parser.add_argument('--req', required=True, help='required optional')
parser.add_argument('++foo', nargs='?', help='foo help')
parser.add_argument('+open', type=open, help='argument with type `open`')
parser.add_argument('-file', nargs='?', help='foo help', type=argparse.FileType('w'))
parser.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!')
testargs = [parser_name, "--generate_cwl_tool", "-d", self.test_dir]
with mock.patch.object(sys, 'argv', testargs):
with self.assertRaises(SystemExit) as result:
parser.parse_args()
self.assertEqual(result.exception.code, 0)
tool = Tool(self.test_dir + parser_name.replace('.py', '.cwl').strip('./'))
for optional in self._strip_help_version_actions(parser._optionals._group_actions):
self.assertEqual(tool.inputs[optional.dest].input_binding.prefix, optional.option_strings[0])


if __name__ == '__main__':
unittest.main()

0 comments on commit 6f85899

Please # to comment.