X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=snaps%2Ftests%2Ffile_utils_tests.py;h=e09a9ccbe0192c904a20d6e8c7bce0c30c326ba1;hb=refs%2Fchanges%2F15%2F64515%2F1;hp=f3a622ae63d1bf88b91c153aa8cfb6824831bc24;hpb=87139670cdbfb7950bea60d8e04333ee63c6f533;p=snaps.git diff --git a/snaps/tests/file_utils_tests.py b/snaps/tests/file_utils_tests.py index f3a622a..e09a9cc 100644 --- a/snaps/tests/file_utils_tests.py +++ b/snaps/tests/file_utils_tests.py @@ -31,18 +31,20 @@ class FileUtilsTests(unittest.TestCase): def setUp(self): guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) - self.tmp_dir = '.tmp/' + self.tmp_dir = 'tmp/' self.test_dir = self.tmp_dir + str(guid) if not os.path.exists(self.test_dir): os.makedirs(self.test_dir) self.tmpFile = self.test_dir + '/bar.txt' - if not os.path.exists(self.tmpFile): - open(self.tmpFile, 'wb') + self.tmp_file_opened = None def tearDown(self): + if self.tmp_file_opened: + self.tmp_file_opened.close() + if os.path.exists(self.test_dir) and os.path.isdir(self.test_dir): - shutil.rmtree(self.tmp_dir) + shutil.rmtree(self.test_dir) def testFileIsDirectory(self): """ @@ -65,6 +67,9 @@ class FileUtilsTests(unittest.TestCase): Ensure the file_utils.fileExists() method returns false with a directory """ + if not os.path.exists(self.tmpFile): + self.tmp_file_opened = open(self.tmpFile, 'wb') + if not os.path.exists(self.tmpFile): os.makedirs(self.tmpFile) @@ -111,3 +116,20 @@ class FileUtilsTests(unittest.TestCase): self.assertEqual('http://foo:5000/v2.0/', os_env_dict['OS_AUTH_URL']) self.assertEqual('admin', os_env_dict['OS_USERNAME']) self.assertEqual('admin', os_env_dict['OS_TENANT_NAME']) + + def test_write_str_to_file(self): + """ + Ensure the file_utils.fileExists() method returns false with a + directory + """ + test_val = 'test string' + + test_file = file_utils.save_string_to_file( + test_val, self.tmpFile) + result1 = file_utils.file_exists(self.tmpFile) + self.assertTrue(result1) + result2 = file_utils.file_exists(test_file.name) + self.assertTrue(result2) + + file_contents = file_utils.read_file(self.tmpFile) + self.assertEqual(test_val, file_contents)