Skip to content

Commit

Permalink
fix all tests to use a platform independent way of deleting extraneou…
Browse files Browse the repository at this point in the history
…s, newly created files
  • Loading branch information
moonshoes87 committed Apr 20, 2015
1 parent faf0324 commit 77c3c32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
5 changes: 3 additions & 2 deletions ipmag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,9 +1616,9 @@ def upload_magic(concat=0, dir_path='.'):
CheckSign=['specimen_b_beta']
last=file_names[-1]
methods,first_file=[],1
for file in file_names:
for File in file_names:
# read in the data
Data,file_type=pmag.magic_read(file)
Data,file_type=pmag.magic_read(File)
if file_type!="bad_file":
print "file ",file," successfully read in"
if len(RmKeys)>0:
Expand Down Expand Up @@ -1693,6 +1693,7 @@ def upload_magic(concat=0, dir_path='.'):
f.close()
print file_type, 'written to ',up
else:
print 'File:', File
print file_type, 'is bad or non-existent - skipping '

# write out the methods table
Expand Down
39 changes: 10 additions & 29 deletions unittests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import os
import numpy as np
import pmag
import ipmag
import sio_magic
import CIT_magic
Expand All @@ -19,9 +20,9 @@ def setUp(self):
os.chdir(WD)

def tearDown(self):
meas_file = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'sio_magic', 'sio_af_example.magic')
if os.path.isfile(meas_file):
os.system('rm {}'.format(meas_file))
filelist = ['sio_af_example.magic']
directory = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'sio_magic')
pmag.remove_files(filelist, directory)

def test_SIO_magic_no_files(self):
program_ran, error_message = sio_magic.main(False)
Expand Down Expand Up @@ -85,10 +86,8 @@ def setUp(self):
os.chdir(WD)

def tearDown(self):
for f in ['magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt']:
full_file = os.path.join(WD, f)
if os.path.isfile(full_file):
os.system('rm {}'.format(full_file))
filelist= ['magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt']
pmag.remove_files(filelist, WD)

def test_CIT_with_no_files(self):
program_ran, error_message = CIT_magic.main(False)
Expand Down Expand Up @@ -159,10 +158,6 @@ def setUp(self):

def tearDown(self):
pass
#meas_file = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'sio_magic', 'sio_af_example.magic')
#if os.path.isfile(meas_file):
# os.system('rm {}'.format(meas_file))


def test_IODP_with_no_files(self):
program_ran, error_message = IODP_srm_magic.main(False)
Expand Down Expand Up @@ -194,14 +189,8 @@ def setUp(self):
os.chdir(WD)

def tearDown(self):
for f in ['magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt', 'SRM_318_U1359_B_A.csv.magic']:
full_file = os.path.join(WD, f)
if os.path.isfile(full_file):
os.system('rm {}'.format(full_file))
#meas_file = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'sio_magic', 'sio_af_example.magic')
#if os.path.isfile(meas_file):
# os.system('rm {}'.format(meas_file))

filelist = ['magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt', 'SRM_318_U1359_B_A.csv.magic']
pmag.remove_files(filelist, WD)

def test_old_IODP_with_no_files(self):
program_ran, error_message = old_IODP_srm_magic.main(False)
Expand Down Expand Up @@ -233,12 +222,8 @@ def setUp(self):
os.chdir(WD)

def tearDown(self):
#input_dir = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'IODP_jr6_magic')
files = ['test.magic', 'other_er_samples.txt']
for f in files:
full_file = os.path.join(WD, f)
if os.path.isfile(full_file):
os.system('rm {}'.format(full_file))
pmag.remove_files(files, WD)

def test_IODP_jr6_with_no_files(self):
options = {}
Expand Down Expand Up @@ -299,11 +284,7 @@ def tearDown(self):
#input_dir = os.path.join(WD, 'Datafiles', 'Measurement_Import', 'IODP_jr6_magic')
#files = ['test.magic', 'other_er_samples.txt']
files = ['mn001-1a.magic', 'er_samples.txt', 'er_sites.txt', 'magic_measurements.txt']
for f in files:
full_file = os.path.join(WD, f)
if os.path.isfile(full_file):
print 'rm {}'.format(full_file)
os.system('rm {}'.format(full_file))
pmag.remove_files(files, WD)

def test_2G_with_no_files(self):
options = {}
Expand Down
24 changes: 15 additions & 9 deletions unittests/test_ipmag.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ def test_with_invalid_files(self):
self.assertFalse(outfile)
self.assertEqual(error_message, "file validation has failed. You may run into problems if you try to upload this file.")
directory = os.path.join(self.dir_path, 'my_project_with_errors')
string = directory + '/' + '*.20*.txt'
os.system('rm ' + string)

# delete any upload file that was partially created
import re
pattern = re.compile('\w*[.]\w*[.]\w*[20]\d{2}\w*.txt$')
possible_files = os.listdir(directory)
files = []
for f in possible_files:
if pattern.match(f):
files.append(f)
pmag.remove_files(files, directory)

def test_with_valid_files(self):
print os.path.join(self.dir_path, 'my_project')
Expand All @@ -44,7 +52,7 @@ def test_with_valid_files(self):
self.assertEqual(error_message, '')
assert os.path.isfile(outfile)
directory = os.path.join(self.dir_path, 'my_project_with_errors')
os.system('rm {}'.format(os.path.join(directory, outfile)))
os.remove(os.path.join(directory, outfile))


class TestIODP_samples_magic(unittest.TestCase):
Expand All @@ -54,8 +62,8 @@ def setUp(self):

def tearDown(self):
os.chdir(WD)
if os.path.isfile('./er_samples.txt'):
os.system('rm er_samples.txt')
filelist = ['er_samples.txt']
pmag.remove_files(filelist, WD)

def test_with_wrong_format(self):
infile = os.path.join(self.input_dir, 'GCR_U1359_B_coresummary.csv')
Expand Down Expand Up @@ -89,10 +97,8 @@ def setUp(self):
pass

def tearDown(self):
for f in ['magic_measurements.txt', 'my_magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt', 'rmag_anisotropy.txt', 'my_rmag_anisotropy.txt']:
full_file = os.path.join(WD, f)
if os.path.isfile(full_file):
os.system('rm {}'.format(full_file))
filelist= ['magic_measurements.txt', 'my_magic_measurements.txt', 'er_specimens.txt', 'er_samples.txt', 'er_sites.txt', 'rmag_anisotropy.txt', 'my_rmag_anisotropy.txt']
pmag.remove_files(filelist, WD)

def test_kly4s_without_infile(self):
with self.assertRaises(TypeError):
Expand Down

0 comments on commit 77c3c32

Please sign in to comment.