From a22de88078e60014d6c90555994c66ec69003069 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Fri, 10 Nov 2023 10:43:30 -0700 Subject: [PATCH] handle missing env variable --- py/desitransfer/spacewatch.py | 4 ++-- py/desitransfer/test/test_spacewatch.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/py/desitransfer/spacewatch.py b/py/desitransfer/spacewatch.py index 7de0dcf..f5fd02f 100644 --- a/py/desitransfer/spacewatch.py +++ b/py/desitransfer/spacewatch.py @@ -134,7 +134,7 @@ def _options(): prsr.add_argument('-o', '--overwrite', action='store_true', help='Overwrite any existing files.') prsr.add_argument('-s', '--server', metavar='SERVER', - default=os.environ['SPACEWATCH_SERVER'], + default=os.getenv('SPACEWATCH_SERVER', 'SPACEWATCH_SERVER'), help='Set the Spacwatch server name to SERVER (default "%(default)s").') prsr.add_argument('-t', '--test', action='store_true', help='Do not actually download any files; implies --debug.') @@ -158,7 +158,7 @@ def main(): log = get_logger(DEBUG) else: log = get_logger() - if options.server is None: + if options.server == 'SPACEWATCH_SERVER': log.critical("Spacewatch server name is not set!") return 1 spacewatch_root = f'https://{options.server}/allsky-all/images/cropped/' diff --git a/py/desitransfer/test/test_spacewatch.py b/py/desitransfer/test/test_spacewatch.py index 6c12af1..510310e 100644 --- a/py/desitransfer/test/test_spacewatch.py +++ b/py/desitransfer/test/test_spacewatch.py @@ -36,9 +36,19 @@ def tearDown(self): def test_options(self): """Test command-line arguments. """ + with patch.object(sys, 'argv', ['desi_spacewatch_transfer', '--debug', '/desi/external/spacewatch']): + with patch.dict('os.environ', {'SPACEWATCH_SERVER': 'www.example.com'}): + options = _options() + self.assertTrue(options.debug) + self.assertEqual(options.server, 'www.example.com') + + def test_options_bad_env(self): + """Test command-line arguments with missing env variable. + """ with patch.object(sys, 'argv', ['desi_spacewatch_transfer', '--debug', '/desi/external/spacewatch']): options = _options() - self.assertTrue(options.debug) + self.assertTrue(options.debug) + self.assertEqual(options.server, 'SPACEWATCH_SERVER') @patch('desitransfer.spacewatch.requests') def test_jpg_files(self, mock_requests):