Skip to content

Commit 53930b5

Browse files
authored
replace exit() with sys.exit() (#137)
Description Replace exit() by sys.exit() Motivation and Context sys.exit is better than exit, considered good to use in production code. Ref: https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used
1 parent 1db2484 commit 53930b5

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/swsssdk/sonic_db_dump_load.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ def do_load(options, args):
130130
if action == DUMP:
131131
if len(args) > 0:
132132
parser.print_help()
133-
exit(4)
133+
sys.exit(4)
134134
do_dump(options)
135135
else:
136136
if len(args) > 1:
137137
parser.print_help()
138-
exit(4)
138+
sys.exit(4)
139139
do_load(options, args)

test/sonic_db_dump_load_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import sys
3+
import pytest
4+
from unittest.mock import patch, MagicMock
5+
6+
modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
7+
sys.path.insert(0, os.path.join(modules_path, 'src'))
8+
import swsssdk
9+
10+
class TestSonicDbDumpLoad(object):
11+
def setup(self):
12+
print("SETUP")
13+
14+
@patch('optparse.OptionParser.print_help')
15+
@patch('optparse.OptionParser.parse_args', MagicMock(return_value=('options', ['-p'])))
16+
@patch('sys.argv', ['dump'])
17+
def test_sonic_db_dump_exit(self, mock_print_help):
18+
with pytest.raises(SystemExit) as e:
19+
swsssdk.sonic_db_dump_load()
20+
mock_print_help.assert_called_once()
21+
assert e.value.code == 4
22+
23+
@patch('optparse.OptionParser.print_help')
24+
@patch('optparse.OptionParser.parse_args', MagicMock(return_value=('options', ['-p', '-o'])))
25+
@patch('sys.argv', ['load'])
26+
def test_sonic_db_load_exit(self, mock_print_help):
27+
with pytest.raises(SystemExit) as e:
28+
swsssdk.sonic_db_dump_load()
29+
mock_print_help.assert_called_once()
30+
assert e.value.code == 4
31+
32+
def teardown(self):
33+
print("TEARDOWN")
34+

0 commit comments

Comments
 (0)