From 1ec4e7755e1e391d5240a585e683097bd3a5ec33 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 13 Feb 2015 12:07:35 -0500 Subject: [PATCH 01/13] Start of issue 718, file not existing clean up --- khmer/kfile.py | 13 +++++++++++-- tests/test_scripts.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/khmer/kfile.py b/khmer/kfile.py index 222bf99ee8..3eb3bf9673 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -16,13 +16,22 @@ def check_file_status(file_path, force): """Check the status of the file; if the file is empty or doesn't exist - AND if the file is NOT a fifo/block/named pipe then a warning is printed + AND if the file is NOT a fifo/bloc k/named pipe then a warning is printed and sys.exit(1) is called """ if file_path is '-': return - mode = os.stat(file_path).st_mode + try: + mode = os.stat(file_path).st_mode + + except OSError as error: + print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \ + file_path + + if not force: + sys.exit(1) + # block devices will be nonzero if S_ISBLK(mode) or S_ISFIFO(mode): return diff --git a/tests/test_scripts.py b/tests/test_scripts.py index 1c9c4a89c5..747c27e6d5 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -2814,3 +2814,15 @@ def test_roundtrip_casava_format_2(): r = open(infile).read() r2 = open(outfile).read() assert r == r2, (r, r2) + + +def test_existance_failure(): + expected_output = OSError + + args = [ + + status, out, err = utils.runscript('extract-paired-reads.py', args) + assert status == 0 + + assert expected_output in out + From 717876f57a8f153cefed71655048181f97a87925 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 13 Feb 2015 12:49:18 -0500 Subject: [PATCH 02/13] Added test, undid typo --- khmer/kfile.py | 2 +- tests/test_scripts.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/khmer/kfile.py b/khmer/kfile.py index 3eb3bf9673..2cc3223b63 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -16,7 +16,7 @@ def check_file_status(file_path, force): """Check the status of the file; if the file is empty or doesn't exist - AND if the file is NOT a fifo/bloc k/named pipe then a warning is printed + AND if the file is NOT a fifo/block/named pipe then a warning is printed and sys.exit(1) is called """ diff --git a/tests/test_scripts.py b/tests/test_scripts.py index 747c27e6d5..8890416e6d 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -2817,12 +2817,12 @@ def test_roundtrip_casava_format_2(): def test_existance_failure(): - expected_output = OSError - - args = [ + expected_output = 'ERROR: Input file' - status, out, err = utils.runscript('extract-paired-reads.py', args) - assert status == 0 + args = ['thisfiledoesnotexistatall'] - assert expected_output in out - + status, out, err = utils.runscript( + 'extract-paired-reads.py', args, fail_ok=True) + assert status == 1 + + assert expected_output in err From 6734c5a99fea3dbd0456a276b82291a47e02c654 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 13 Feb 2015 13:03:26 -0500 Subject: [PATCH 03/13] Added ChangeLog --- ChangeLog | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index 3182eb5438..6c0b5e9d2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-03-06 Jessica Mizzi + + * khmer/kfile.py: Added file not existing error for system exit + * tests/test_scripts.py: Added test for check_file_status + 2015-03-06 Titus Brown * sandbox/{collect-reads.py,saturate-by-median.py}: update for 'force' From afee1fdb5a5b530e3ca94c177b46b2a50153b22c Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Wed, 18 Feb 2015 13:55:58 -0500 Subject: [PATCH 04/13] typo and argument correction --- tests/test_scripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_scripts.py b/tests/test_scripts.py index 8890416e6d..9be97aa1ed 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -2819,7 +2819,7 @@ def test_roundtrip_casava_format_2(): def test_existance_failure(): expected_output = 'ERROR: Input file' - args = ['thisfiledoesnotexistatall'] + args = [utils.get_temp_filename('thisfiledoesnotexistatall')] status, out, err = utils.runscript( 'extract-paired-reads.py', args, fail_ok=True) From 3259bcd834c411437bd4da95d6d53a78955c93e7 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 20 Feb 2015 11:39:03 -0500 Subject: [PATCH 05/13] got rid of error argument --- khmer/kfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/khmer/kfile.py b/khmer/kfile.py index 2cc3223b63..298ed26a7c 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -25,7 +25,7 @@ def check_file_status(file_path, force): try: mode = os.stat(file_path).st_mode - except OSError as error: + except OSError: print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \ file_path From 67d96a0a4a7a1bfc2428dbb51a8acd5f59eae9c6 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 20 Feb 2015 12:25:13 -0500 Subject: [PATCH 06/13] got rid of unnecessary line --- khmer/kfile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/khmer/kfile.py b/khmer/kfile.py index 298ed26a7c..217173b797 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -24,7 +24,6 @@ def check_file_status(file_path, force): return try: mode = os.stat(file_path).st_mode - except OSError: print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \ file_path From d40671684d1a75196d4bc1a92535627e6e63e978 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 20 Feb 2015 14:00:10 -0500 Subject: [PATCH 07/13] add kfile test --- tests/test_functions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_functions.py b/tests/test_functions.py index b0cd09c448..48013d6aae 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -299,3 +299,10 @@ def testForceSingleAndMinLength(self): assert x == expected, x assert m == 3, m assert n == 2, n + + +def check_file_status_kfile(): + try: + kfile.check_file_status('thisfiledoesnotexistatall') + except OSError as e: + print >>sys.stder, '...failed to remove {fn}'.format(fn) From 90efff91c6e12d6c24d5de06d08140d99e5a4f9a Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 20 Feb 2015 14:11:47 -0500 Subject: [PATCH 08/13] added check_file_status force test --- tests/test_functions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_functions.py b/tests/test_functions.py index 48013d6aae..82f23a94c3 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -306,3 +306,11 @@ def check_file_status_kfile(): kfile.check_file_status('thisfiledoesnotexistatall') except OSError as e: print >>sys.stder, '...failed to remove {fn}'.format(fn) + + +def check_file_status_kfile_force(): + if force: + try: + kfile.check_file_status('thisfiledoesnotexistatall') + except OSError as e: + print >>sys.stderr, '...failed to remove {fn}'.format(fn) From 412cc336c6b9de16d88141e0e82914c8e31df8b9 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 20 Feb 2015 14:23:38 -0500 Subject: [PATCH 09/13] ChangeLog --- ChangeLog | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 6c0b5e9d2e..72af704521 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,8 @@ 2015-03-06 Jessica Mizzi * khmer/kfile.py: Added file not existing error for system exit - * tests/test_scripts.py: Added test for check_file_status + * tests/{test_scripts,test_functions}.py: Added tests for + check_file_status for file existence and force option 2015-03-06 Titus Brown From 7c02e57f33f06434f2147305195adffde459e004 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Mon, 23 Feb 2015 16:56:13 -0500 Subject: [PATCH 10/13] Fixing test errors --- tests/test_functions.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_functions.py b/tests/test_functions.py index 82f23a94c3..279ff07a94 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -11,6 +11,7 @@ import collections from khmer.utils import (check_is_pair, broken_paired_reader, check_is_left, check_is_right) +from khmer.kfile import check_file_status def test_forward_hash(): @@ -115,6 +116,21 @@ def test_extract_hashbits_info(): print >>sys.stderr, '...failed to remove {fn}'.format(fn) +def test_check_file_status_kfile(): + try: + utils.get_temp_filename('thisfiledoesnotexistatall') + except OSError as e: + print >>sys.stder, '...failed to remove {fn}'.format(fn) + + +def test_check_file_status_kfile_force(): + try: + khmer.kfile('thisfiledoesnotexistatall', force) + assert False + except OSError as e: + print >>sys.stderr, '...failed to remove {fn}'.format(fn) + + FakeFQRead = collections.namedtuple('Read', ['name', 'quality', 'sequence']) FakeFastaRead = collections.namedtuple('Read', ['name', 'sequence']) From a7e0205b68a62bd2e48c928224dc6fb7530b1622 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 27 Feb 2015 12:33:28 -0500 Subject: [PATCH 11/13] writing test --- tests/test_functions.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_functions.py b/tests/test_functions.py index 279ff07a94..4b7732905e 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -117,15 +117,18 @@ def test_extract_hashbits_info(): def test_check_file_status_kfile(): + fn = utils.get_temp_filename('thisfiledoesnotexist') try: - utils.get_temp_filename('thisfiledoesnotexistatall') + check_file_status(fn, False) + assert True except OSError as e: print >>sys.stder, '...failed to remove {fn}'.format(fn) def test_check_file_status_kfile_force(): + fn = utils.get_temp_filename('thisfiledoesnotexist') try: - khmer.kfile('thisfiledoesnotexistatall', force) + check_file_status(fn, True) assert False except OSError as e: print >>sys.stderr, '...failed to remove {fn}'.format(fn) From 0130f44df52f9a76e871fbcd1f1fa0d4585dd9e0 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 6 Mar 2015 12:43:34 -0500 Subject: [PATCH 12/13] Fixing test errors --- khmer/kfile.py | 8 ++++++-- tests/test_functions.py | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/khmer/kfile.py b/khmer/kfile.py index 217173b797..f3bce708b4 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -19,17 +19,21 @@ def check_file_status(file_path, force): AND if the file is NOT a fifo/block/named pipe then a warning is printed and sys.exit(1) is called """ + mode = None if file_path is '-': return try: mode = os.stat(file_path).st_mode except OSError: - print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \ + print >>sys.stderr, "ERROR: Input file %s does not exist" % \ file_path - + if not force: + print >>sys.stderr, "Exiting" sys.exit(1) + else: + return # block devices will be nonzero if S_ISBLK(mode) or S_ISFIFO(mode): diff --git a/tests/test_functions.py b/tests/test_functions.py index 4b7732905e..e267fa3287 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -118,20 +118,20 @@ def test_extract_hashbits_info(): def test_check_file_status_kfile(): fn = utils.get_temp_filename('thisfiledoesnotexist') + check_file_status_exited = False try: check_file_status(fn, False) - assert True - except OSError as e: - print >>sys.stder, '...failed to remove {fn}'.format(fn) + except SystemExit: + check_file_status_exited = True + assert check_file_status_exited def test_check_file_status_kfile_force(): fn = utils.get_temp_filename('thisfiledoesnotexist') try: - check_file_status(fn, True) - assert False + check_file_status(fn, True) except OSError as e: - print >>sys.stderr, '...failed to remove {fn}'.format(fn) + assert False FakeFQRead = collections.namedtuple('Read', ['name', 'quality', 'sequence']) From d0cc0b01819fe90dd30ee8b1d496c60129756873 Mon Sep 17 00:00:00 2001 From: Jessica Mizzi Date: Fri, 6 Mar 2015 13:27:31 -0500 Subject: [PATCH 13/13] clean ups --- ChangeLog | 4 ++-- khmer/kfile.py | 4 ++-- tests/test_functions.py | 19 ++----------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 72af704521..6481dcf039 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,8 @@ 2015-03-06 Jessica Mizzi * khmer/kfile.py: Added file not existing error for system exit - * tests/{test_scripts,test_functions}.py: Added tests for - check_file_status for file existence and force option + * tests/{test_scripts,test_functions}.py: Added tests for + check_file_status for file existence and force option 2015-03-06 Titus Brown diff --git a/khmer/kfile.py b/khmer/kfile.py index f3bce708b4..ebd186a9d7 100644 --- a/khmer/kfile.py +++ b/khmer/kfile.py @@ -19,7 +19,7 @@ def check_file_status(file_path, force): AND if the file is NOT a fifo/block/named pipe then a warning is printed and sys.exit(1) is called """ - mode = None + mode = None if file_path is '-': return @@ -28,7 +28,7 @@ def check_file_status(file_path, force): except OSError: print >>sys.stderr, "ERROR: Input file %s does not exist" % \ file_path - + if not force: print >>sys.stderr, "Exiting" sys.exit(1) diff --git a/tests/test_functions.py b/tests/test_functions.py index e267fa3287..93e2e4c42d 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -121,7 +121,7 @@ def test_check_file_status_kfile(): check_file_status_exited = False try: check_file_status(fn, False) - except SystemExit: + except SystemExit: check_file_status_exited = True assert check_file_status_exited @@ -129,7 +129,7 @@ def test_check_file_status_kfile(): def test_check_file_status_kfile_force(): fn = utils.get_temp_filename('thisfiledoesnotexist') try: - check_file_status(fn, True) + check_file_status(fn, True) except OSError as e: assert False @@ -318,18 +318,3 @@ def testForceSingleAndMinLength(self): assert x == expected, x assert m == 3, m assert n == 2, n - - -def check_file_status_kfile(): - try: - kfile.check_file_status('thisfiledoesnotexistatall') - except OSError as e: - print >>sys.stder, '...failed to remove {fn}'.format(fn) - - -def check_file_status_kfile_force(): - if force: - try: - kfile.check_file_status('thisfiledoesnotexistatall') - except OSError as e: - print >>sys.stderr, '...failed to remove {fn}'.format(fn)