Commit 160ae59f authored by Jeff Yoon's avatar Jeff Yoon Committed by Commit Bot

Refactoring to be able to unit test parsing logic

The existing setup does not allow for unit testing the logic
contained for arg parsing. Refactoring the existing setup, such
that we can unit test those components in isolation.

* Encapsulate argument parsing and test runner invocation in a
Runner class.
* Have __main__ create a Runner object, and call main(), which is
the central coordinating method.
test_args
* Unit tests for the argument parsing logic.

Change-Id: I3ca576f99aa7dc36b38579dbce40cd7ac4e67c36
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1975302
Commit-Queue: Jeff Yoon <jeffyoon@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729527}
parent 146a84d0
This diff is collapsed.
#!/usr/bin/python
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for run.py."""
import json
import re
import unittest
import run
class UnitTest(unittest.TestCase):
def test_parse_args_ok(self):
cmd = [
'--app',
'./foo-Runner.app',
'--host-app',
'./bar.app',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
runner = run.Runner()
runner.parse_args(cmd)
self.assertTrue(runner.args.app == './foo-Runner.app')
def test_parse_args_iossim_platform_version(self):
"""
iossim, platforma and version should all be set.
missing iossim
"""
test_cases = [
{
'error':
2,
'cmd': [
'--platform',
'iPhone X',
'--version',
'13.2.2',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
],
},
{
'error':
2,
'cmd': [
'--iossim',
'path/to/iossim',
'--version',
'13.2.2',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
],
},
{
'error':
2,
'cmd': [
'--iossim',
'path/to/iossim',
'--platform',
'iPhone X',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
],
},
]
runner = run.Runner()
for test_case in test_cases:
with self.assertRaises(SystemExit) as ctx:
runner.parse_args(test_case['cmd'])
self.assertTrue(re.match('must specify all or none of *', ctx.message))
self.assertEqual(ctx.exception.code, test_case['error'])
def test_parse_args_xcode_parallelization_requirements(self):
"""
xcode parallelization set requires both platform and version
"""
test_cases = [
{
'error':
2,
'cmd': [
'--xcode-parallelization',
'--platform',
'iPhone X',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
},
{
'error':
2,
'cmd': [
'--xcode-parallelization',
'--version',
'13.2.2',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
}
]
runner = run.Runner()
for test_case in test_cases:
with self.assertRaises(SystemExit) as ctx:
runner.parse_args(test_case['cmd'])
self.assertTrue(
re.match('--xcode-parallelization also requires both *',
ctx.message))
self.assertEqual(ctx.exception.code, test_case['error'])
def test_parse_args_xcodebuild_device_runner_requirements(self):
"""
xcodebuild_device_runner requires both platform and version
"""
test_cases = [
{
'error':
2,
'cmd': [
'--xcodebuild-device-runner',
'--platform',
'iPhone X',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
},
{
'error':
2,
'cmd': [
'--xcodebuild-device-runner',
'--version',
'13.2.2',
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
}
]
runner = run.Runner()
for test_case in test_cases:
with self.assertRaises(SystemExit) as ctx:
runner.parse_args(test_case['cmd'])
self.assertTrue(
re.match('--xcodebuild-device-runner also requires '
'both *', ctx.message))
self.assertEqual(ctx.exception.code, test_case['error'])
def test_parse_args_from_json(self):
json_args = {
'test_cases': ['test1'],
'restart': 'true',
'xcode_parallelization': True,
'shards': 2
}
cmd = [
'--shards',
'1',
'--platform',
'iPhone X',
'--version',
'13.2.2',
'--args-json',
json.dumps(json_args),
# Required
'--xcode-build-version',
'123abc',
'--out-dir',
'some/dir'
]
# shards should be 2, since json arg takes precedence over cmd line
runner = run.Runner()
runner.parse_args(cmd)
# Empty array
self.assertEquals(len(runner.args.env_var), 0)
self.assertTrue(runner.args.xcode_parallelization)
self.assertTrue(runner.args.restart)
self.assertEquals(runner.args.shards, 2)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment