Skip to content

Commit 933e149

Browse files
committed
Made main more testable, simplified unit tests
1 parent 6f2f282 commit 933e149

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

openapi_spec_validator/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
)
1414

1515

16-
def main():
16+
def main(args=None):
1717
parser = argparse.ArgumentParser()
1818
parser.add_argument('filename', help="Absolute or relative path to file")
19-
args = parser.parse_args()
19+
args = parser.parse_args(args)
2020
filename = args.filename
2121
filename = os.path.abspath(filename)
2222
try:

tests/integration/test_main.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
from unittest import mock
1+
import pytest
22

33
from openapi_spec_validator.__main__ import main
44

55

6-
@mock.patch('openapi_spec_validator.__main__.sys')
7-
def test_nonexisting_file(mock_sys):
8-
"""Calling with non-existing file should sys.exit."""
9-
# the first parameter is always the name of the program itself, so
10-
# we can insert a dummy here
11-
testargs = ['progname', 'i_dont_exist.yaml']
12-
with mock.patch('sys.argv', testargs):
13-
main()
14-
assert mock_sys.exit.called
6+
def test_happy_path():
7+
"""No errors when calling with proper file."""
8+
testargs = ['./tests/integration/data/v3.0/petstore.yaml']
9+
main(testargs)
1510

1611

17-
@mock.patch('openapi_spec_validator.__main__.sys')
18-
def test_happy_path(mock_sys):
19-
testargs = ['progname', './tests/integration/data/v3.0/petstore.yaml']
20-
with mock.patch('sys.argv', testargs):
21-
main()
22-
assert not mock_sys.exit.called
12+
def test_nonexisting_file():
13+
"""Calling with non-existing file should sys.exit."""
14+
testargs = ['i_dont_exist.yaml']
15+
with pytest.raises(SystemExit):
16+
main(testargs)

0 commit comments

Comments
 (0)